Update console examples to use the new console API. Improved documentation of application template.

This commit is contained in:
fincs 2018-10-01 13:08:43 +02:00
parent 4c0e6cf201
commit 4894fcf472
18 changed files with 110 additions and 150 deletions

View File

@ -17,7 +17,6 @@ int main(int argc, char **argv)
char username[0x21];
gfxInitDefault();
consoleInit(NULL);
memset(&userdata, 0, sizeof(userdata));
@ -82,11 +81,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -17,7 +17,6 @@ int main(int argc, char **argv)
NacpLanguageEntry *langentry = NULL;
char name[0x201];
gfxInitDefault();
consoleInit(NULL);
buf = (NsApplicationControlData*)malloc(sizeof(NsApplicationControlData));
@ -78,11 +77,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -11,7 +11,6 @@
int main(void)
{
gfxInitDefault();
consoleInit(NULL);
printf("Simple audren demonstration program\n");
@ -78,7 +77,6 @@ int main(void)
// Main loop
while (appletMainLoop())
{
gfxSwapBuffers();
hidScanInput();
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
@ -102,7 +100,7 @@ int main(void)
printf("sample count = %" PRIu32 "\n", audrvVoiceGetPlayedSampleCount(&drv, 0));
}
gfxFlushBuffers();
consoleUpdate(NULL);
}
if (initedDriver)
@ -110,6 +108,6 @@ int main(void)
if (initedAudren)
audrenExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -18,23 +18,21 @@
int main(int argc, char **argv)
{
Result rc = 0;
gfxInitDefault();
// Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL);
AudioInBuffer audin_buf;
AudioOutBuffer audout_buf;
AudioInBuffer *released_in_buffer;
AudioOutBuffer *released_out_buffer;
u32 released_in_count;
u32 released_out_count;
// Make sure the sample buffer size is aligned to 0x1000 bytes.
u32 data_size = (SAMPLECOUNT * CHANNELCOUNT * BYTESPERSAMPLE);
u32 buffer_size = (data_size + 0xfff) & ~0xfff;
// Allocate the buffers.
u8* in_buf_data = memalign(0x1000, buffer_size);
u8* out_buf_data = memalign(0x1000, buffer_size);
@ -45,65 +43,65 @@ int main(int argc, char **argv)
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
printf("Failed to allocate sample data buffers\n");
}
if (R_SUCCEEDED(rc))
{
memset(in_buf_data, 0, buffer_size);
memset(out_buf_data, 0, buffer_size);
}
if (R_SUCCEEDED(rc))
{
// Initialize the default audio input device.
rc = audinInitialize();
printf("audinInitialize() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Initialize the default audio output device.
rc = audoutInitialize();
printf("audoutInitialize() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Start audio capture.
rc = audinStartAudioIn();
printf("audinStartAudioIn() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Start audio playback.
rc = audoutStartAudioOut();
printf("audoutStartAudioOut() returned 0x%x\n", rc);
}
// Prepare the input buffer.
audin_buf.next = NULL;
audin_buf.buffer = in_buf_data;
audin_buf.buffer_size = buffer_size;
audin_buf.data_size = data_size;
audin_buf.data_offset = 0;
// Prepare the output buffer.
audout_buf.next = NULL;
audout_buf.buffer = out_buf_data;
audout_buf.buffer_size = buffer_size;
audout_buf.data_size = data_size;
audout_buf.data_offset = 0;
// Prepare pointers and counters for released buffers.
released_in_buffer = NULL;
released_out_buffer = NULL;
released_in_count = 0;
released_out_count = 0;
// Append the initial input buffer.
rc = audinAppendAudioInBuffer(&audin_buf);
printf("audinAppendAudioInBuffer() returned 0x%x\n", rc);
// Append the initial output buffer.
rc = audoutAppendAudioOutBuffer(&audout_buf);
printf("audoutAppendAudioOutBuffer() returned 0x%x\n", rc);
@ -117,21 +115,20 @@ int main(int argc, char **argv)
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
// Wait for audio capture and playback to finish.
audinWaitCaptureFinish(&released_in_buffer, &released_in_count, U64_MAX);
audoutWaitPlayFinish(&released_out_buffer, &released_out_count, U64_MAX);
// Copy the captured audio data into the playback buffer.
if ((released_in_buffer != NULL) && (released_out_buffer != NULL))
memcpy(released_out_buffer->buffer, released_in_buffer->buffer, released_in_buffer->data_size);
// Append the released buffers again.
audinAppendAudioInBuffer(released_in_buffer);
audoutAppendAudioOutBuffer(released_out_buffer);
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
// Stop audio capture.
@ -146,6 +143,6 @@ int main(int argc, char **argv)
audinExit();
audoutExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -27,61 +27,59 @@ void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequ
int main(int argc, char **argv)
{
Result rc = 0;
int notefreq[] = {
220,
440, 880, 1760, 3520, 7040,
14080,
7040, 3520, 1760, 880, 440
};
gfxInitDefault();
// Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL);
AudioOutBuffer audout_buf;
AudioOutBuffer *audout_released_buf;
// Make sure the sample buffer size is aligned to 0x1000 bytes.
u32 data_size = (SAMPLECOUNT * CHANNELCOUNT * BYTESPERSAMPLE);
u32 buffer_size = (data_size + 0xfff) & ~0xfff;
// Allocate the buffer.
u8* out_buf_data = memalign(0x1000, buffer_size);
// Ensure buffers were properly allocated.
if (out_buf_data == NULL)
{
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
printf("Failed to allocate sample data buffers\n");
}
if (R_SUCCEEDED(rc))
memset(out_buf_data, 0, buffer_size);
if (R_SUCCEEDED(rc))
{
// Initialize the default audio output device.
rc = audoutInitialize();
printf("audoutInitialize() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
printf("Sample rate: 0x%x\n", audoutGetSampleRate());
printf("Channel count: 0x%x\n", audoutGetChannelCount());
printf("PCM format: 0x%x\n", audoutGetPcmFormat());
printf("Device state: 0x%x\n", audoutGetDeviceState());
// Start audio playback.
rc = audoutStartAudioOut();
printf("audoutStartAudioOut() returned 0x%x\n", rc);
}
bool play_tone = false;
printf("Press A, B, Y, X, Left, Up, Right, Down, L, R, ZL or ZR to play a different tone.\n");
while (appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
@ -91,79 +89,79 @@ int main(int argc, char **argv)
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
if (kDown & KEY_A)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[0]);
play_tone = true;
}
if (kDown & KEY_B)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[1]);
play_tone = true;
}
if (kDown & KEY_Y)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[2]);
play_tone = true;
}
if (kDown & KEY_X)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[3]);
play_tone = true;
}
if (kDown & KEY_DLEFT)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[4]);
play_tone = true;
}
if (kDown & KEY_DUP)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[5]);
play_tone = true;
}
if (kDown & KEY_DRIGHT)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[6]);
play_tone = true;
}
if (kDown & KEY_DDOWN)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[7]);
play_tone = true;
}
if (kDown & KEY_L)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[8]);
play_tone = true;
}
if (kDown & KEY_R)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[9]);
play_tone = true;
}
if (kDown & KEY_ZL)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[10]);
play_tone = true;
}
if (kDown & KEY_ZR)
{
fill_audio_buffer(out_buf_data, 0, data_size, notefreq[11]);
play_tone = true;
}
if (R_SUCCEEDED(rc) && play_tone)
{
// Prepare the audio data source buffer.
@ -172,30 +170,29 @@ int main(int argc, char **argv)
audout_buf.buffer_size = buffer_size;
audout_buf.data_size = data_size;
audout_buf.data_offset = 0;
// Prepare pointer for the released buffer.
audout_released_buf = NULL;
// Play the buffer.
rc = audoutPlayBuffer(&audout_buf, &audout_released_buf);
if (R_FAILED(rc))
printf("audoutPlayBuffer() returned 0x%x\n", rc);
play_tone = false;
}
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
// Stop audio playback.
rc = audoutStopAudioOut();
printf("audoutStopAudioOut() returned 0x%x\n", rc);
// Terminate the default audio output device.
audoutExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -30,7 +30,6 @@ void printfile(const char* path)
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
Result rc = romfsInit();
@ -55,12 +54,10 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
romfsExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -51,7 +51,6 @@ int main(int argc, char **argv)
bool account_selected=0;
u64 titleID=0x01007ef00011e000;//titleID of the save to mount, in this case BOTW.
gfxInitDefault();
consoleInit(NULL);
//Get the userID for save mounting. To mount common savedata, use FS_SAVEDATA_USERID_COMMONSAVE.
@ -136,11 +135,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -9,7 +9,6 @@
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
DIR* dir;
@ -42,11 +41,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -5,8 +5,6 @@
int main(int argc, char **argv)
{
gfxInitDefault();
//Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL);
@ -25,11 +23,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -5,10 +5,8 @@
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
// clear screen and home cursor
printf( CONSOLE_ESC(2J) );
@ -73,11 +71,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -19,7 +19,6 @@ int main(int argc, char **argv)
"", "", "", ""
};
gfxInitDefault();
consoleInit(NULL);
u32 kDownOld = 0, kHeldOld = 0, kUpOld = 0; //In these variables there will be information about keys detected in the previous frame
@ -81,11 +80,9 @@ int main(int argc, char **argv)
printf("\x1b[3;1H%04d; %04d", pos_left.dx, pos_left.dy);
printf("\x1b[5;1H%04d; %04d", pos_right.dx, pos_right.dy);
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -9,7 +9,6 @@ int main(int argc, char **argv)
{
u32 prev_touchcount=0;
gfxInitDefault();
consoleInit(NULL);
printf("\x1b[1;1HPress PLUS to exit.");
@ -52,11 +51,9 @@ int main(int argc, char **argv)
printf("[point_id=%d] px=%03d, py=%03d, dx=%03d, dy=%03d, angle=%03d\n", i, touch.px, touch.py, touch.dx, touch.dy, touch.angle);
}
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -16,7 +16,6 @@ int main(int argc, char **argv)
HidVibrationValue VibrationValue_stop;
HidVibrationValue VibrationValues[2];
gfxInitDefault();
consoleInit(NULL);
printf("Press PLUS to exit.\n");
@ -90,11 +89,9 @@ int main(int argc, char **argv)
if (R_FAILED(rc2)) printf("hidSendVibrationValues() for stop other device returned: 0x%x\n", rc2);
}
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -13,7 +13,6 @@ int main(int argc, char **argv)
u64 (*funcptr)(void);
u32 testcode[2] = {0xd2800000 | (0x7<<5), 0xd65f03c0};//"mov x0, #0x7" "ret"
gfxInitDefault();
consoleInit(NULL);
rc = jitCreate(&j, 0x100000);//Adjust size as needed.
@ -58,11 +57,9 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -16,9 +16,8 @@
#include <switch.h>
int main(int argc, char **argv) {
gfxInitDefault();
int main(int argc, char **argv)
{
consoleInit(NULL);
// Initialise sockets
@ -62,11 +61,10 @@ int main(int argc, char **argv) {
printf("B Pressed\n");
}
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
socketExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -10,7 +10,6 @@ int main(int argc, char **argv)
u64 LanguageCode=0;
s32 Language=0;
gfxInitDefault();
consoleInit(NULL);
Result rc = setInitialize();
@ -58,12 +57,10 @@ int main(int argc, char **argv)
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
setExit();
gfxExit();
consoleExit(NULL);
return 0;
}

View File

@ -1,33 +1,44 @@
#include <string.h>
// Include the most common headers from the C standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include the main libnx system header, for Switch development
#include <switch.h>
int main(int argc, char **argv)
// Main program entrypoint
int main(int argc, char* argv[])
{
gfxInitDefault();
// This example uses a text console, as a simple way to output text to the screen.
// If you want to write a software-rendered graphics application,
// take a look at the graphics/simplegfx example, which uses the libnx gfx API instead.
// If on the other hand you want to write an OpenGL based application,
// take a look at the graphics/opengl set of examples, which uses EGL instead.
consoleInit(NULL);
printf("Hello World!");
// Other initialization goes here. As a demonstration, we print hello world.
printf("Hello World!\n");
// Main loop
while(appletMainLoop())
while (appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
// Scan all the inputs. This should be done once for each frame
hidScanInput();
// hidKeysDown returns information about which buttons have been
// just pressed in this frame compared to the previous one
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
break; // break in order to return to hbmenu
// Your code goes here
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
}
gfxExit();
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL);
return 0;
}

View File

@ -10,7 +10,6 @@ const char* const weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
printf("\x1b[16;16HPress PLUS to exit.");
@ -41,11 +40,9 @@ int main(int argc, char **argv)
printf("\x1b[1;1H%02i:%02i:%02i", hours, minutes, seconds);
printf("\n%s %s %i %i", weekDays[wday], months[month], day, year);
gfxFlushBuffers();
gfxSwapBuffers();
consoleUpdate(NULL);
}
gfxExit();
consoleExit(NULL);
return 0;
}