From 4894fcf4722f60953b1d93605a7ed9451c947969 Mon Sep 17 00:00:00 2001 From: fincs Date: Mon, 1 Oct 2018 13:08:43 +0200 Subject: [PATCH] Update console examples to use the new console API. Improved documentation of application template. --- account/source/main.c | 7 +-- app_controldata/source/main.c | 7 +-- audio/audren-simple/source/main.c | 6 +- audio/echo/source/main.c | 41 ++++++------ audio/playtone/source/main.c | 69 ++++++++++----------- fs/romfs/source/main.c | 7 +-- fs/save/source/main.c | 7 +-- fs/sdmc/source/main.c | 7 +-- graphics/printing/hello-world/source/main.c | 8 +-- graphics/printing/vt52-demo/source/main.c | 8 +-- hid/read-controls/source/main.c | 7 +-- hid/touch-screen/source/main.c | 7 +-- hid/vibration/source/main.c | 7 +-- jit/source/main.c | 7 +-- network/nxlink_stdio/source/main.c | 10 ++- settings/get_system_language/source/main.c | 7 +-- templates/application/source/main.c | 41 +++++++----- time/source/main.c | 7 +-- 18 files changed, 110 insertions(+), 150 deletions(-) diff --git a/account/source/main.c b/account/source/main.c index 9bc37b2..e53a974 100644 --- a/account/source/main.c +++ b/account/source/main.c @@ -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; } - diff --git a/app_controldata/source/main.c b/app_controldata/source/main.c index 14f455a..ad1b90b 100644 --- a/app_controldata/source/main.c +++ b/app_controldata/source/main.c @@ -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; } - diff --git a/audio/audren-simple/source/main.c b/audio/audren-simple/source/main.c index f7bd03b..8647d1a 100644 --- a/audio/audren-simple/source/main.c +++ b/audio/audren-simple/source/main.c @@ -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; } diff --git a/audio/echo/source/main.c b/audio/echo/source/main.c index cd7f490..1d80718 100644 --- a/audio/echo/source/main.c +++ b/audio/echo/source/main.c @@ -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; } diff --git a/audio/playtone/source/main.c b/audio/playtone/source/main.c index 5decd4b..c0a8814 100644 --- a/audio/playtone/source/main.c +++ b/audio/playtone/source/main.c @@ -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; } diff --git a/fs/romfs/source/main.c b/fs/romfs/source/main.c index e588793..5d2f64f 100644 --- a/fs/romfs/source/main.c +++ b/fs/romfs/source/main.c @@ -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; } - diff --git a/fs/save/source/main.c b/fs/save/source/main.c index 9f4ac93..776fd66 100644 --- a/fs/save/source/main.c +++ b/fs/save/source/main.c @@ -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; } - diff --git a/fs/sdmc/source/main.c b/fs/sdmc/source/main.c index 8567ae8..0c4da0d 100644 --- a/fs/sdmc/source/main.c +++ b/fs/sdmc/source/main.c @@ -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; } - diff --git a/graphics/printing/hello-world/source/main.c b/graphics/printing/hello-world/source/main.c index 874b04a..488701b 100644 --- a/graphics/printing/hello-world/source/main.c +++ b/graphics/printing/hello-world/source/main.c @@ -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; } - diff --git a/graphics/printing/vt52-demo/source/main.c b/graphics/printing/vt52-demo/source/main.c index 118bb4f..dd70da9 100644 --- a/graphics/printing/vt52-demo/source/main.c +++ b/graphics/printing/vt52-demo/source/main.c @@ -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; } - diff --git a/hid/read-controls/source/main.c b/hid/read-controls/source/main.c index 96ec7d5..43f1a7d 100644 --- a/hid/read-controls/source/main.c +++ b/hid/read-controls/source/main.c @@ -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; } - diff --git a/hid/touch-screen/source/main.c b/hid/touch-screen/source/main.c index 37aa357..b8b3dd9 100644 --- a/hid/touch-screen/source/main.c +++ b/hid/touch-screen/source/main.c @@ -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; } - diff --git a/hid/vibration/source/main.c b/hid/vibration/source/main.c index 8bc3b82..90dd088 100644 --- a/hid/vibration/source/main.c +++ b/hid/vibration/source/main.c @@ -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; } - diff --git a/jit/source/main.c b/jit/source/main.c index c2e6b85..d30fae9 100644 --- a/jit/source/main.c +++ b/jit/source/main.c @@ -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; } - diff --git a/network/nxlink_stdio/source/main.c b/network/nxlink_stdio/source/main.c index 5b3fc71..e474d9d 100644 --- a/network/nxlink_stdio/source/main.c +++ b/network/nxlink_stdio/source/main.c @@ -16,9 +16,8 @@ #include -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; } diff --git a/settings/get_system_language/source/main.c b/settings/get_system_language/source/main.c index 712fedc..526f3f2 100644 --- a/settings/get_system_language/source/main.c +++ b/settings/get_system_language/source/main.c @@ -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; } - diff --git a/templates/application/source/main.c b/templates/application/source/main.c index 7f655fb..44efb83 100644 --- a/templates/application/source/main.c +++ b/templates/application/source/main.c @@ -1,33 +1,44 @@ -#include +// Include the most common headers from the C standard library #include +#include +#include +// Include the main libnx system header, for Switch development #include -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; } - diff --git a/time/source/main.c b/time/source/main.c index 21bb572..dc07f0e 100644 --- a/time/source/main.c +++ b/time/source/main.c @@ -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; } -