From ccb6ec7cf5996afbe9befdb10cc6689f20cfe9cf Mon Sep 17 00:00:00 2001 From: csBlueChip Date: Fri, 28 Aug 2009 15:04:43 +0000 Subject: [PATCH] [CHG] Ensure Init and Exit do not call twice - also return something useful from Init if it fails --- GRRLIB/GRRLIB/GRRLIB_core.c | 51 +++++++++++++++++------------- GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h | 2 +- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_core.c b/GRRLIB/GRRLIB/GRRLIB_core.c index 49eba09..8f63c5d 100644 --- a/GRRLIB/GRRLIB/GRRLIB_core.c +++ b/GRRLIB/GRRLIB/GRRLIB_core.c @@ -20,6 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------*/ +#include #include #include #include @@ -34,19 +35,23 @@ Mtx GXmodelView2D; static void *gp_fifo = NULL; +static bool is_setup = false; // To control entry and exit + /** * Initialize GRRLIB. Call this at the beginning your code. + * @return int 0=OK; -1=NoMemory * @see GRRLIB_Exit */ -void GRRLIB_Init (void) { +int GRRLIB_Init (void) { f32 yscale; u32 xfbHeight; Mtx44 perspective; + // Ensure this function is only ever called once + if (is_setup) return 0 ; + VIDEO_Init(); - rmode = VIDEO_GetPreferredMode(NULL); - if (rmode == NULL) - return; + if ( !(rmode = VIDEO_GetPreferredMode(NULL)) ) return -1 ; // Video Mode Correction switch (rmode->viTVMode) { @@ -63,22 +68,16 @@ void GRRLIB_Init (void) { } VIDEO_Configure(rmode); - xfb[0] = (u32 *)MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); - xfb[1] = (u32 *)MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); - if (xfb[0] == NULL || xfb[1] == NULL) - return; + if ( !(xfb[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode))) ) return -1 ; + if ( !(xfb[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode))) ) return -1 ; VIDEO_SetNextFramebuffer(xfb[fb]); VIDEO_SetBlack(true); VIDEO_Flush(); VIDEO_WaitVSync(); - if (rmode->viTVMode & VI_NON_INTERLACE) { - VIDEO_WaitVSync(); - } + if (rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync() ; - gp_fifo = (u8 *) memalign(32, DEFAULT_FIFO_SIZE); - if (gp_fifo == NULL) - return; + if ( !(gp_fifo = memalign(32, DEFAULT_FIFO_SIZE)) ) return -1 ; memset(gp_fifo, 0, DEFAULT_FIFO_SIZE); GX_Init(gp_fifo, DEFAULT_FIFO_SIZE); @@ -93,13 +92,8 @@ void GRRLIB_Init (void) { GX_SetCopyFilter(rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter); GX_SetFieldMode(rmode->field_rendering, ((rmode->viHeight == 2 * rmode->xfbHeight) ? GX_ENABLE : GX_DISABLE)); - if (rmode->aa) { - // Set 16 bit RGB565 - GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR); - } else { - // Set 24 bit Z24 - GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR); - } + if (rmode->aa) GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR) ; // Set 16 bit RGB565 + else GX_SetPixelFmt(GX_PF_RGB8_Z24 , GX_ZC_LINEAR) ; // Set 24 bit Z24 GX_SetDispCopyGamma(GX_GM_1_0); @@ -143,13 +137,26 @@ void GRRLIB_Init (void) { // Default settings GRRLIB_Settings.antialias = true; - GRRLIB_Settings.blend = GRRLIB_BLEND_ALPHA; + GRRLIB_Settings.blend = GRRLIB_BLEND_ALPHA; + + // Schedule cleanup for when program exits + is_setup = true; + atexit(GRRLIB_Exit); + + return 0; } /** * Call this before exiting your application. */ void GRRLIB_Exit (void) { + + // Ensure this function is only ever called once + // ...and only if the setup function has been called + static bool done = false; + if (done || !is_setup) return ; + else done = true ; + // Allow write access to the full screen GX_SetClipMode( GX_CLIP_DISABLE ); GX_SetScissor( 0, 0, rmode->fbWidth, rmode->efbHeight ); diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index a8b5c92..5948ff0 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -70,7 +70,7 @@ void GRRLIB_BMFX_Pixelate (const GRRLIB_texImg *texsrc, //------------------------------------------------------------------------------ // GRRLIB_core.c - GRRLIB core functions -void GRRLIB_Init (void) ; +int GRRLIB_Init (void) ; void GRRLIB_Exit (void) ; //------------------------------------------------------------------------------