From 7f10ad40977217caf5b22e62c168e18597d2b8d2 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 2 Nov 2014 18:58:37 +0100 Subject: [PATCH] Add aptMainLoop() for handling APT events in main() --- examples/gpu/source/main.c | 28 +++++------- examples/mic/source/main.c | 70 +++++++++++++----------------- libctru/include/3ds/services/apt.h | 1 + libctru/source/services/apt.c | 19 ++++++++ 4 files changed, 61 insertions(+), 57 deletions(-) diff --git a/examples/gpu/source/main.c b/examples/gpu/source/main.c index 1c06bd5..c059540 100644 --- a/examples/gpu/source/main.c +++ b/examples/gpu/source/main.c @@ -177,26 +177,22 @@ int main() gspWaitForPSC0(); gfxSwapBuffersGpu(); - APP_STATUS status; - while((status=aptGetStatus())!=APP_EXITING) + while(aptMainLoop()) { - if(status==APP_RUNNING) - { - demoControls(); + demoControls(); - GX_SetMemoryFill(gxCmdBuf, (u32*)gpuOut, 0x404040FF, (u32*)&gpuOut[0x2EE00], 0x201, (u32*)gpuDOut, 0x00000000, (u32*)&gpuDOut[0x2EE00], 0x201); - gspWaitForPSC0(); + GX_SetMemoryFill(gxCmdBuf, (u32*)gpuOut, 0x404040FF, (u32*)&gpuOut[0x2EE00], 0x201, (u32*)gpuDOut, 0x00000000, (u32*)&gpuDOut[0x2EE00], 0x201); + gspWaitForPSC0(); - GPUCMD_SetBuffer(gpuCmd, gpuCmdSize, 0); - doFrame1(); - GPUCMD_Finalize(); - GPUCMD_Run(gxCmdBuf); - gspWaitForP3D(); + GPUCMD_SetBuffer(gpuCmd, gpuCmdSize, 0); + doFrame1(); + GPUCMD_Finalize(); + GPUCMD_Run(gxCmdBuf); + gspWaitForP3D(); - gfxSwapBuffersGpu(); - GX_SetDisplayTransfer(gxCmdBuf, (u32*)gpuOut, 0x019001E0, (u32*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), 0x019001E0, 0x01001000); - gspWaitForPPF(); - } + gfxSwapBuffersGpu(); + GX_SetDisplayTransfer(gxCmdBuf, (u32*)gpuOut, 0x019001E0, (u32*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), 0x019001E0, 0x01001000); + gspWaitForPPF(); gspWaitForVBlank(); } diff --git a/examples/mic/source/main.c b/examples/mic/source/main.c index 90205b5..8e6fc11 100644 --- a/examples/mic/source/main.c +++ b/examples/mic/source/main.c @@ -24,60 +24,48 @@ int main() MIC_Initialize(sharedmem, sharedmem_size, control, 0, 3, 1, 1);//See mic.h. - APP_STATUS status; - while((status=aptGetStatus())!=APP_EXITING) + while(aptMainLoop()) { - if(status==APP_RUNNING) + framebuf = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); + hidScanInput(); + + if(hidKeysDown() & KEY_A) { - framebuf = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); - hidScanInput(); + audiobuf_pos = 0; - if(hidKeysDown() & KEY_A) - { - audiobuf_pos = 0; + CSND_setchannel_playbackstate(0x8, 0);//Stop audio playback. + CSND_sharedmemtype0_cmdupdatestate(0); - CSND_setchannel_playbackstate(0x8, 0);//Stop audio playback. - CSND_sharedmemtype0_cmdupdatestate(0); + MIC_SetRecording(1); - MIC_SetRecording(1); + memset(framebuf, 0x20, 0x46500); + } - memset(framebuf, 0x20, 0x46500); - } + if((hidKeysHeld() & KEY_A) && audiobuf_pos < audiobuf_size) + { + audiobuf_pos+= MIC_ReadAudioData(&audiobuf[audiobuf_pos], audiobuf_size-audiobuf_pos, 1); + if(audiobuf_pos > audiobuf_size)audiobuf_pos = audiobuf_size; - if((hidKeysHeld() & KEY_A) && audiobuf_pos < audiobuf_size) - { - audiobuf_pos+= MIC_ReadAudioData(&audiobuf[audiobuf_pos], audiobuf_size-audiobuf_pos, 1); - if(audiobuf_pos > audiobuf_size)audiobuf_pos = audiobuf_size; + memset(framebuf, 0x60, 0x46500); + } - memset(framebuf, 0x60, 0x46500); - } + if(hidKeysUp() & KEY_A) + { + MIC_SetRecording(0); + GSPGPU_FlushDataCache(NULL, audiobuf, audiobuf_pos); + CSND_playsound(0x8, CSND_LOOP_DISABLE, CSND_ENCODING_PCM16, 16000, (u32*)audiobuf, NULL, audiobuf_pos, 2, 0); - if(hidKeysUp() & KEY_A) - { - MIC_SetRecording(0); - GSPGPU_FlushDataCache(NULL, audiobuf, audiobuf_pos); - CSND_playsound(0x8, CSND_LOOP_DISABLE, CSND_ENCODING_PCM16, 16000, (u32*)audiobuf, NULL, audiobuf_pos, 2, 0); - - memset(framebuf, 0xe0, 0x46500); - - gfxFlushBuffers(); - gfxSwapBuffers(); - - framebuf = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); - memset(framebuf, 0xe0, 0x46500); - } + memset(framebuf, 0xe0, 0x46500); gfxFlushBuffers(); gfxSwapBuffers(); + + framebuf = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); + memset(framebuf, 0xe0, 0x46500); } - else if(status == APP_SUSPENDING) - { - aptReturnToMenu(); - } - else if(status == APP_SLEEPMODE) - { - aptWaitStatusEvent(); - } + + gfxFlushBuffers(); + gfxSwapBuffers(); gspWaitForVBlank(); } diff --git a/libctru/include/3ds/services/apt.h b/libctru/include/3ds/services/apt.h index bec85f7..a0bfc87 100644 --- a/libctru/include/3ds/services/apt.h +++ b/libctru/include/3ds/services/apt.h @@ -49,6 +49,7 @@ void aptReturnToMenu();//This should be called by the user application when aptG void aptWaitStatusEvent(); void aptSignalReadyForSleep(); NS_APPID aptGetMenuAppID(); +bool aptMainLoop(); // Use like this in your main(): while (aptMainLoop()) { your code here... } Result APT_GetLockHandle(Handle* handle, u16 flags, Handle* lockHandle); Result APT_Initialize(Handle* handle, NS_APPID appId, Handle* eventHandle1, Handle* eventHandle2); diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 9294f1a..b060ad8 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -394,6 +394,25 @@ void aptExit() svcCloseHandle(aptStatusEvent); } +bool aptMainLoop() +{ + for (;;) switch (aptGetStatus()) + { + case APP_RUNNING: + return true; + case APP_SUSPENDING: + aptReturnToMenu(); + break; + case APP_SLEEPMODE: + aptWaitStatusEvent(); + break; + case APP_EXITING: + return false; + default: + break; + } +} + void aptAppStarted() { u8 buf1[4], buf2[4];