diff --git a/GRRLIB/GRRLIB/GRRLIB_texEdit.c b/GRRLIB/GRRLIB/GRRLIB_texEdit.c index 03bb8c4..e9346c7 100644 --- a/GRRLIB/GRRLIB/GRRLIB_texEdit.c +++ b/GRRLIB/GRRLIB/GRRLIB_texEdit.c @@ -25,46 +25,50 @@ THE SOFTWARE. #include #include #include -#include #include +#define TPL_HDR_VERSION_FIELD 0 +#define TPL_HDR_NTEXTURE_FIELD 4 +#define TPL_HDR_HDRSIZE_FIELD 8 +#define TPL_HDR_DESCR_FIELD 12 + // texture header typedef struct _tplimgheader TPLImgHeader; struct _tplimgheader { - u16 height; - u16 width; - u32 fmt; - void *data; - u32 wraps; - u32 wrapt; - u32 minfilter; - u32 magfilter; - f32 lodbias; - u8 edgelod; - u8 minlod; - u8 maxlod; - u8 unpacked; + u16 height; + u16 width; + u32 fmt; + void *data; + u32 wraps; + u32 wrapt; + u32 minfilter; + u32 magfilter; + f32 lodbias; + u8 edgelod; + u8 minlod; + u8 maxlod; + u8 unpacked; } ATTRIBUTE_PACKED; // texture palette header typedef struct _tplpalheader TPLPalHeader; struct _tplpalheader { - u16 nitems; - u8 unpacked; - u8 pad; - u32 fmt; - void *data; + u16 nitems; + u8 unpacked; + u8 pad; + u32 fmt; + void *data; } ATTRIBUTE_PACKED; // texture descriptor typedef struct _tpldesc TPLDescHeader; struct _tpldesc { - TPLImgHeader *imghead; - TPLPalHeader *palhead; + TPLImgHeader *imghead; + TPLPalHeader *palhead; } ATTRIBUTE_PACKED; /** @@ -188,34 +192,40 @@ GRRLIB_texImg* GRRLIB_CreateEmptyTexture (const u32 width, const u32 height) * @param size Size of the TPL buffer to set. * @return A GRRLIB_texImg structure filled with image information. */ -GRRLIB_texImg* GRRLIB_LoadTextureTPL (u8 *my_tpl, const int size) { - const s32 id = 0; // Only id zero is valid for now - +GRRLIB_texImg* GRRLIB_LoadTextureTPL (const u8 *my_tpl, const int size, u32 id) { if(my_tpl == NULL || size <= 0) { return NULL; } - GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg)); - - if (my_texture == NULL) { + const u32 ntextures = *(u32*)(my_tpl + TPL_HDR_NTEXTURE_FIELD); + if(id > ntextures) { return NULL; } - TPLFile tdf; - if (TPL_OpenTPLFromMemory(&tdf, my_tpl, size) > 0) { - const TPLDescHeader *deschead = (TPLDescHeader*)tdf.texdesc; - my_texture->data = deschead[id].imghead->data; - my_texture->w = deschead[id].imghead->width; - my_texture->h = deschead[id].imghead->height; - my_texture->freedata = true; - GRRLIB_SetHandle( my_texture, 0, 0 ); - GRRLIB_FlushTex( my_texture ); + const TPLDescHeader *deschead = (TPLDescHeader*)(my_tpl + TPL_HDR_DESCR_FIELD); + + for(u32 c = 0; c <= id; ++c) { + u32 pos = (u32)deschead[c].imghead; + const TPLImgHeader *imghead = (TPLImgHeader*)(my_tpl + pos); + + if(c == id) { + GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg)); + if (my_texture == NULL) { + return NULL; + } + pos = (u32)imghead->data; + my_texture->data = (char*)(my_tpl + pos); + my_texture->w = imghead->width; + my_texture->h = imghead->height; + my_texture->freedata = true; + GRRLIB_SetHandle( my_texture, 0, 0 ); + GRRLIB_FlushTex( my_texture ); + + return my_texture; + } } - else { - free(my_texture); - return NULL; - } - return my_texture; + + return NULL; } /** diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index e24dca0..874fd30 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -142,7 +142,7 @@ GRRLIB_texImg* GRRLIB_LoadTexturePNG (const u8 *my_png); GRRLIB_texImg* GRRLIB_LoadTextureJPG (const u8 *my_jpg); GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const u32 my_size); GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp); -GRRLIB_texImg* GRRLIB_LoadTextureTPL (u8 *my_tpl, const int size); +GRRLIB_texImg* GRRLIB_LoadTextureTPL (const u8 *my_tpl, const int size, u32 id); //------------------------------------------------------------------------------ // GRRLIB_gecko.c - USB_Gecko output facilities diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h index ed53988..20c25dc 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h @@ -61,7 +61,7 @@ void GRRLIB_FreeTexture (GRRLIB_texImg *tex) { */ INLINE void GRRLIB_ClearTex(GRRLIB_texImg* tex) { - if(tex == NULL) { + if(tex == NULL || tex->freedata == false) { return; } memset(tex->data, 0, GX_GetTexBufferSize(tex->w, tex->h, tex->format, 0, 0)); diff --git a/examples/basic_drawing/source/main.c b/examples/basic_drawing/source/main.c index 768a4dd..b3ed0d9 100644 --- a/examples/basic_drawing/source/main.c +++ b/examples/basic_drawing/source/main.c @@ -94,7 +94,8 @@ int main() { GRRLIB_texImg *tex_BMfont5 = GRRLIB_LoadTexture(BMfont5_png); GRRLIB_InitTileSet(tex_BMfont5, 8, 16, 0); - GRRLIB_texImg *tex_test_tpl = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size); + GRRLIB_texImg *tex_test_tpl1 = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size, ballsprites); + GRRLIB_texImg *tex_test_tpl2 = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size, pirate); while(1) { WPAD_SetVRes(0, 640, 480); @@ -114,7 +115,8 @@ int main() { GRRLIB_DrawImg(10, 50, tex_test_jpg, 0, 1, 1, GRRLIB_WHITE); // Draw a jpeg GRRLIB_DrawImg(350, 50, tex_test_bmp, 0, 4, 4, GRRLIB_WHITE); // Draw a bitmap - GRRLIB_DrawImg(400, 200, tex_test_tpl, 0, 2, 2, GRRLIB_WHITE); // Draw a TPL + GRRLIB_DrawImg(400, 200, tex_test_tpl1, 0, 2, 2, GRRLIB_WHITE); // Draw a TPL + GRRLIB_DrawImg(400, 300, tex_test_tpl2, 0, 1, 1, GRRLIB_WHITE); // Draw a TPL // Draw a sprite GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee diff --git a/examples/basic_drawing/textures/pirate.png b/examples/basic_drawing/textures/pirate.png new file mode 100644 index 0000000..6f88a3b Binary files /dev/null and b/examples/basic_drawing/textures/pirate.png differ diff --git a/examples/basic_drawing/textures/textures.scf b/examples/basic_drawing/textures/textures.scf index 7c3ded1..8e31cea 100644 --- a/examples/basic_drawing/textures/textures.scf +++ b/examples/basic_drawing/textures/textures.scf @@ -1 +1,2 @@ +