Use less memory allocation

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

View file

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <stdio.h>
#include <jpeglib.h>
#include <string.h>
#include <ogc/tpl.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.
*/
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 );
}

View file

@ -43,7 +43,6 @@ THE SOFTWARE.
// Includes
//==============================================================================
#include <gccore.h>
#include <ogc/tpl.h>
//==============================================================================
//==============================================================================
@ -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;
//------------------------------------------------------------------------------

View file

@ -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);

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(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