[ADD} Added fileIO functions from Addon (and ensured all examples still compile)

This commit is contained in:
csBlueChip 2009-08-30 21:44:05 +00:00
parent 159f56e252
commit 9373faaf5e
7 changed files with 120 additions and 66 deletions

View file

@ -24,6 +24,7 @@ THE SOFTWARE.
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include <ogc/conf.h> #include <ogc/conf.h>
#include <fat.h>
#define __GRRLIB_CORE__ #define __GRRLIB_CORE__
#include <grrlib.h> #include <grrlib.h>
@ -39,7 +40,7 @@ static bool is_setup = false; // To control entry and exit
/** /**
* Initialize GRRLIB. Call this at the beginning your code. * 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 * @see GRRLIB_Exit
*/ */
int GRRLIB_Init (void) { int GRRLIB_Init (void) {
@ -143,6 +144,9 @@ int GRRLIB_Init (void) {
is_setup = true; is_setup = true;
atexit(GRRLIB_Exit); atexit(GRRLIB_Exit);
// Initialise the filing system
if (!fatInitDefault()) return -2 ;
return 0; return 0;
} }

View file

@ -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 <grrlib.h>
#include <stdio.h>
/**
* 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;
}

View file

@ -78,6 +78,13 @@ void GRRLIB_Exit (void) ;
void GRRLIB_Circle (const f32 x, const f32 y, const f32 radius, void GRRLIB_Circle (const f32 x, const f32 y, const f32 radius,
const u32 color, const u8 filled) ; 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 :) //! GRRLIB_print.c - Will someome please tell me what these are :)
void GRRLIB_Printf (const f32 xpos, const f32 ypos, void GRRLIB_Printf (const f32 xpos, const f32 ypos,

View file

@ -13,7 +13,7 @@ ELF := $(APP).elf
DOL := $(APP).dol DOL := $(APP).dol
MAP := $(notdir $(ELF)).map MAP := $(notdir $(ELF)).map
LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz LIBS := -lgrrlib -lpngu -lpng -ljpeg -lz -lfat
LIBS += -lwiiuse LIBS += -lwiiuse
#LIBS += -lmodplay -lasnd #LIBS += -lmodplay -lasnd
LIBS += -lbte -logc LIBS += -lbte -logc
@ -73,4 +73,4 @@ run : $(DOL)
.PRECIOUS : %.c .PRECIOUS : %.c
%.c : %.png %.c : %.png
@echo Converting resource: $< @echo Converting resource: $<
@./raw2c.exe $< 2>null @./raw2c.exe $< 2>nul

View file

@ -53,23 +53,6 @@
static u8 CalculateFrameRate(); 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 main() {
int left = 0, top = 0, page = 0, frame = TILE_DOWN + 1; int left = 0, top = 0, page = 0, frame = TILE_DOWN + 1;
unsigned int wait = TILE_DELAY, direction = TILE_DOWN, direction_new = TILE_DOWN; 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) { if(wpadheld & WPAD_BUTTON_1 && wpadheld & WPAD_BUTTON_2) {
WPAD_Rumble(WPAD_CHAN_0, 1); // Rumble on 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 WPAD_Rumble(WPAD_CHAN_0, 0); // Rumble off
} }
} }

View file

@ -16,10 +16,6 @@
#include "GRRLIBfont.h" #include "GRRLIBfont.h"
#include "GRRLIBbutton.h" #include "GRRLIBbutton.h"
extern u32 fb;
extern void *xfb[2];
extern GXRModeObj *rmode;
GRRLIB_texImg *tex_GRRLIBfont; GRRLIB_texImg *tex_GRRLIBfont;
GRRLIB_texImg *tex_GRRLIBbutton; GRRLIB_texImg *tex_GRRLIBbutton;
@ -42,45 +38,6 @@ void GRRLIB_addon_Exit(){
GRRLIB_FreeTexture(tex_GRRLIBbutton); 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. * Easy Button Maker.
* @param indice Index number of your button. * @param indice Index number of your button.

View file

@ -33,9 +33,9 @@ RESH := $(patsubst %.png,%.h,$(RES))
OBJ := $(RESOBJ) $(SRCOBJ) 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 += -lwiiuse
#LIBS += -lmodplay -lasnd #LIBS += -lmodplay -lasnd
LIBS += -lbte -logc LIBS += -lbte -logc