From 9373faaf5e1065fb758c44b7c7d5e7f34eb7cf63 Mon Sep 17 00:00:00 2001 From: csBlueChip Date: Sun, 30 Aug 2009 21:44:05 +0000 Subject: [PATCH] [ADD} Added fileIO functions from Addon (and ensured all examples still compile) --- GRRLIB/GRRLIB/GRRLIB_core.c | 6 +- GRRLIB/GRRLIB/GRRLIB_fileIO.c | 103 ++++++++++++++++++ GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h | 7 ++ examples/composition/makefile | 4 +- examples/lesson1/source/main.c | 19 +--- .../nonameno03/GRRLIB_addon/GRRLIB_addon.c | 43 -------- examples/template/Makefile | 4 +- 7 files changed, 120 insertions(+), 66 deletions(-) create mode 100644 GRRLIB/GRRLIB/GRRLIB_fileIO.c diff --git a/GRRLIB/GRRLIB/GRRLIB_core.c b/GRRLIB/GRRLIB/GRRLIB_core.c index 1be830d..7481a8a 100644 --- a/GRRLIB/GRRLIB/GRRLIB_core.c +++ b/GRRLIB/GRRLIB/GRRLIB_core.c @@ -24,6 +24,7 @@ THE SOFTWARE. #include #include #include +#include #define __GRRLIB_CORE__ #include @@ -39,7 +40,7 @@ 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 + * @return int 0=OK; -1=NoMemory; -2=NoFilingSystem * @see GRRLIB_Exit */ int GRRLIB_Init (void) { @@ -143,6 +144,9 @@ int GRRLIB_Init (void) { is_setup = true; atexit(GRRLIB_Exit); + // Initialise the filing system + if (!fatInitDefault()) return -2 ; + return 0; } diff --git a/GRRLIB/GRRLIB/GRRLIB_fileIO.c b/GRRLIB/GRRLIB/GRRLIB_fileIO.c new file mode 100644 index 0000000..4f4d688 --- /dev/null +++ b/GRRLIB/GRRLIB/GRRLIB_fileIO.c @@ -0,0 +1,103 @@ +/*------------------------------------------------------------------------------ +Copyright (c) 2009 The GRRLIB Team + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +------------------------------------------------------------------------------*/ +#include +#include + +/** + * Load a file to memory. The 2nd parameter is a pointer-to-your-pointer! + * Ie. { u8 *data; load("file",&data); }. It is your responsibility to free the + * memory allocated by this function. + * @param filename name of the file to be loaded. + * @return int 0:EmptyFile, -1:FileNotFound, -2:OutOfMemory, -3:FileReadError, + * >0 -> FileLength. + */ +int GRRLIB_LoadFile(const char* filename, unsigned char* *data) { + int len; + FILE *fd; + + // Open the file + if ( !(fd = fopen(filename, "rb")) ) return -1 ; + + // Get file length + fseek(fd, 0, SEEK_END); + if ( !(len = ftell(fd)) ) { + *data = NULL; + return 0; + } + fseek(fd, 0, SEEK_SET); + + // Grab some memory in which to store the file + if ( !(*data = malloc(len)) ) { + fclose(fd); + return -2; + } + + if ( fread(*data, 1, len, fd) != len) { + fclose(fd); + free(*data); *data = NULL; + return -3; + } + + fclose(fd); + return len; +} + + +/** + * Load a texture from a file. + * @param filename The JPEG or PNG filename to load. + * @return A GRRLIB_texImg structure filled with image informations. + */ +GRRLIB_texImg* GRRLIB_LoadTextureFromFile(const char *filename) { + GRRLIB_texImg *tex; + unsigned char *data; + + // return NULL it load fails + if (GRRLIB_LoadFile(filename, &data) <= 0) return NULL ; + + // Convert to texture + tex = GRRLIB_LoadTexture(data); + + // Free up the buffer + free(data); + + return tex; +} + +/** + * Make a PNG screenshot on the SD card. + * libfat is required to use the function. + * @param File name of the file to write. + * @return bool true=everything worked, false=problems occurred. + */ +bool GRRLIB_ScrShot(const char* filename) { + IMGCTX pngContext; + int ret = -1; + + if ( (pngContext = PNGU_SelectImageFromDevice(filename)) ) { + ret = PNGU_EncodeFromYCbYCr( pngContext, + rmode->fbWidth, rmode->efbHeight, + xfb[fb], 0 ); + PNGU_ReleaseImageContext(pngContext); + } + return !ret; +} diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index 5948ff0..b19030e 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -78,6 +78,13 @@ void GRRLIB_Exit (void) ; void GRRLIB_Circle (const f32 x, const f32 y, const f32 radius, const u32 color, const u8 filled) ; +//------------------------------------------------------------------------------ +// GRRLIB_fileIO - File I/O (SD Card) +int GRRLIB_LoadFile (const char* filename, + unsigned char* *data) ; +GRRLIB_texImg* GRRLIB_LoadTextureFromFile (const char* filename) ; +bool GRRLIB_ScrShot (const char* filename) ; + //------------------------------------------------------------------------------ //! GRRLIB_print.c - Will someome please tell me what these are :) void GRRLIB_Printf (const f32 xpos, const f32 ypos, diff --git a/examples/composition/makefile b/examples/composition/makefile index d92543d..881a631 100644 --- a/examples/composition/makefile +++ b/examples/composition/makefile @@ -13,7 +13,7 @@ ELF := $(APP).elf DOL := $(APP).dol MAP := $(notdir $(ELF)).map -LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz +LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz -lfat LIBS += -lwiiuse #LIBS += -lmodplay -lasnd LIBS += -lbte -logc @@ -73,4 +73,4 @@ run : $(DOL) .PRECIOUS : %.c %.c : %.png @echo Converting resource: $< - @./raw2c.exe $< 2>null + @./raw2c.exe $< 2>nul diff --git a/examples/lesson1/source/main.c b/examples/lesson1/source/main.c index dfc85ae..7aabaea 100644 --- a/examples/lesson1/source/main.c +++ b/examples/lesson1/source/main.c @@ -53,23 +53,6 @@ static u8 CalculateFrameRate(); -/** - * Make a PNG screenshot on the SD card. - * libfat is required to use the function. - * @param File name of the file to write. - * @return true if every thing worked, false otherwise. - */ -bool ScrShot(const char* File) { - int ErrorCode = -1; - IMGCTX pngContext; - - if(fatInitDefault() && (pngContext = PNGU_SelectImageFromDevice(File))) { - ErrorCode = PNGU_EncodeFromYCbYCr(pngContext, rmode->fbWidth, rmode->efbHeight, xfb[fb], 0); - PNGU_ReleaseImageContext(pngContext); - } - return !ErrorCode; -} - int main() { int left = 0, top = 0, page = 0, frame = TILE_DOWN + 1; unsigned int wait = TILE_DELAY, direction = TILE_DOWN, direction_new = TILE_DOWN; @@ -230,7 +213,7 @@ int main() { } if(wpadheld & WPAD_BUTTON_1 && wpadheld & WPAD_BUTTON_2) { WPAD_Rumble(WPAD_CHAN_0, 1); // Rumble on - ScrShot("sd:/grrlib.png"); + GRRLIB_ScrShot("sd:/grrlib.png"); WPAD_Rumble(WPAD_CHAN_0, 0); // Rumble off } } diff --git a/examples/nonameno03/GRRLIB_addon/GRRLIB_addon.c b/examples/nonameno03/GRRLIB_addon/GRRLIB_addon.c index f0ed574..63c631f 100644 --- a/examples/nonameno03/GRRLIB_addon/GRRLIB_addon.c +++ b/examples/nonameno03/GRRLIB_addon/GRRLIB_addon.c @@ -16,10 +16,6 @@ #include "GRRLIBfont.h" #include "GRRLIBbutton.h" -extern u32 fb; -extern void *xfb[2]; -extern GXRModeObj *rmode; - GRRLIB_texImg *tex_GRRLIBfont; GRRLIB_texImg *tex_GRRLIBbutton; @@ -42,45 +38,6 @@ void GRRLIB_addon_Exit(){ GRRLIB_FreeTexture(tex_GRRLIBbutton); } -/** - * Load a texture from a file. - * @param filename The JPEG or PNG filename to load. - * @return A GRRLIB_texImg structure filled with image informations. - */ -GRRLIB_texImg *GRRLIB_LoadTextureFromFile(const char *filename) { - fatInitDefault(); - FILE *fd = fopen(filename, "rb"); - - fseek(fd, 0, SEEK_END); - long lsize = ftell(fd); - rewind(fd); - - unsigned char *buffer = (unsigned char*) malloc (sizeof(unsigned char)*lsize); - fread (buffer, 1, lsize, fd); - GRRLIB_texImg *tex = GRRLIB_LoadTexture(buffer); - free(buffer); - - fclose(fd); - return tex; -} - -/** - * Make a PNG screenshot on the SD card. - * libfat is required to use the function. - * @param File name of the file to write. - * @return true if every thing worked, false otherwise. - */ -bool GRRLIB_ScrShot(const char* File) { - int ErrorCode = -1; - IMGCTX pngContext; - - if(fatInitDefault() && (pngContext = PNGU_SelectImageFromDevice(File))) { - ErrorCode = PNGU_EncodeFromYCbYCr(pngContext, rmode->fbWidth, rmode->efbHeight, xfb[fb], 0); - PNGU_ReleaseImageContext(pngContext); - } - return !ErrorCode; -} - /** * Easy Button Maker. * @param indice Index number of your button. diff --git a/examples/template/Makefile b/examples/template/Makefile index dae8b8e..c54c4fa 100644 --- a/examples/template/Makefile +++ b/examples/template/Makefile @@ -33,9 +33,9 @@ RESH := $(patsubst %.png,%.h,$(RES)) OBJ := $(RESOBJ) $(SRCOBJ) #------------------------------------------------------------------------------- -# Include required libraries +# Include required libraries ...the order can-be/is critical! #------------------------------------------------------------------------------- -LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz +LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz -lfat LIBS += -lwiiuse #LIBS += -lmodplay -lasnd LIBS += -lbte -logc