Use less memory allocation

This commit is contained in:
Crayon2000 2024-03-04 17:46:34 -05:00
parent f80bf532ff
commit 1510e5326b
4 changed files with 17 additions and 20 deletions

View file

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <stdio.h> #include <stdio.h>
#include <jpeglib.h> #include <jpeglib.h>
#include <string.h> #include <string.h>
#include <ogc/tpl.h>
#include <grrlib.h> #include <grrlib.h>
@ -188,24 +189,25 @@ GRRLIB_texImg* GRRLIB_CreateEmptyTexture (const u32 width, const u32 height)
* @return A GRRLIB_texImg structure filled with image information. * @return A GRRLIB_texImg structure filled with image information.
*/ */
GRRLIB_texImg* GRRLIB_LoadTextureTPL (u8 *my_tpl, const int size) { 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 const s32 id = 0; // Only id zero is valid for now
GRRLIB_texImg *my_texture = NULL;
if(my_tpl == NULL || size <= 0 || if(my_tpl == NULL || size <= 0) {
(my_texture = calloc(1, sizeof(GRRLIB_texImg))) == NULL) {
return NULL; return NULL;
} }
TPLFile *tdf = calloc(1, sizeof(TPLFile)); GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg));
if (tdf && TPL_OpenTPLFromMemory(tdf, my_tpl, size) == 1) {
TPL_GetTextureInfo(tdf, id, NULL, &width, &height); if (my_texture == NULL) {
TPLDescHeader *deschead = (TPLDescHeader*)tdf->texdesc; 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->data = deschead[id].imghead->data;
my_texture->w = width; my_texture->w = deschead[id].imghead->width;
my_texture->h = height; my_texture->h = deschead[id].imghead->height;
my_texture->tdf = tdf; my_texture->freedata = true;
GRRLIB_SetHandle( my_texture, 0, 0 ); GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture ); GRRLIB_FlushTex( my_texture );
} }

View file

@ -43,7 +43,6 @@ THE SOFTWARE.
// Includes // Includes
//============================================================================== //==============================================================================
#include <gccore.h> #include <gccore.h>
#include <ogc/tpl.h>
//============================================================================== //==============================================================================
//============================================================================== //==============================================================================
@ -124,7 +123,7 @@ typedef struct GRRLIB_texImg {
f32 ofnormaltexy;/**< Offset of normalized texture on y. */ f32 ofnormaltexy;/**< Offset of normalized texture on y. */
void *data; /**< Pointer to the texture data. */ 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; } GRRLIB_texImg;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View file

@ -49,11 +49,7 @@ void GRRLIB_FreeTexture (GRRLIB_texImg *tex) {
if(tex == NULL) { if(tex == NULL) {
return; return;
} }
if (tex->tdf != NULL) { if (tex->data != NULL && tex->freedata == true) {
TPL_CloseTPLFile(tex->tdf);
free(tex->tdf);
}
else if (tex->data != NULL) {
free(tex->data); free(tex->data);
} }
free(tex); free(tex);

View file

@ -114,7 +114,7 @@ int main() {
GRRLIB_DrawImg(10, 50, tex_test_jpg, 0, 1, 1, GRRLIB_WHITE); // Draw a jpeg 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(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 // Draw a sprite
GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee