From d67e646e67428af5ab0bb1daa271f65cd9b9e02f Mon Sep 17 00:00:00 2001 From: Crayon2000 Date: Mon, 4 Mar 2024 17:46:34 -0500 Subject: [PATCH] Use less memory allocation --- GRRLIB/GRRLIB/GRRLIB_texEdit.c | 26 ++++++++++++++------------ GRRLIB/GRRLIB/grrlib.h | 3 +-- GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h | 6 +----- examples/basic_drawing/source/main.c | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_texEdit.c b/GRRLIB/GRRLIB/GRRLIB_texEdit.c index 261ca41..03bb8c4 100644 --- a/GRRLIB/GRRLIB/GRRLIB_texEdit.c +++ b/GRRLIB/GRRLIB/GRRLIB_texEdit.c @@ -25,6 +25,7 @@ THE SOFTWARE. #include #include #include +#include #include @@ -188,24 +189,25 @@ GRRLIB_texImg* GRRLIB_CreateEmptyTexture (const u32 width, const u32 height) * @return A GRRLIB_texImg structure filled with image information. */ GRRLIB_texImg* GRRLIB_LoadTextureTPL (u8 *my_tpl, const int size) { - u16 width = 0; - u16 height = 0; const s32 id = 0; // Only id zero is valid for now - GRRLIB_texImg *my_texture = NULL; - if(my_tpl == NULL || size <= 0 || - (my_texture = calloc(1, sizeof(GRRLIB_texImg))) == NULL) { + if(my_tpl == NULL || size <= 0) { return NULL; } - TPLFile *tdf = calloc(1, sizeof(TPLFile)); - if (tdf && TPL_OpenTPLFromMemory(tdf, my_tpl, size) == 1) { - TPL_GetTextureInfo(tdf, id, NULL, &width, &height); - TPLDescHeader *deschead = (TPLDescHeader*)tdf->texdesc; + GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg)); + + if (my_texture == NULL) { + 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 = width; - my_texture->h = height; - my_texture->tdf = tdf; + 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 ); } diff --git a/GRRLIB/GRRLIB/grrlib.h b/GRRLIB/GRRLIB/grrlib.h index 0a2bfd8..cee9fd4 100644 --- a/GRRLIB/GRRLIB/grrlib.h +++ b/GRRLIB/GRRLIB/grrlib.h @@ -43,7 +43,6 @@ THE SOFTWARE. // Includes //============================================================================== #include -#include //============================================================================== //============================================================================== @@ -124,7 +123,7 @@ typedef struct GRRLIB_texImg { f32 ofnormaltexy;/**< Offset of normalized texture on y. */ void *data; /**< Pointer to the texture data. */ - TPLFile *tdf; /**< Pointer to the a TPL file structure. */ + bool freedata; /**< Should the data be freed. */ } GRRLIB_texImg; //------------------------------------------------------------------------------ diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h index 3fb59ed..ed53988 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h @@ -49,11 +49,7 @@ void GRRLIB_FreeTexture (GRRLIB_texImg *tex) { if(tex == NULL) { return; } - if (tex->tdf != NULL) { - TPL_CloseTPLFile(tex->tdf); - free(tex->tdf); - } - else if (tex->data != NULL) { + if (tex->data != NULL && tex->freedata == true) { free(tex->data); } free(tex); diff --git a/examples/basic_drawing/source/main.c b/examples/basic_drawing/source/main.c index e665498..768a4dd 100644 --- a/examples/basic_drawing/source/main.c +++ b/examples/basic_drawing/source/main.c @@ -114,7 +114,7 @@ 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, 150, tex_test_tpl, 0, 2, 2, GRRLIB_WHITE); // Draw a TPL + GRRLIB_DrawImg(400, 200, tex_test_tpl, 0, 2, 2, GRRLIB_WHITE); // Draw a TPL // Draw a sprite GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee