Add id parameter to GRRLIB_LoadTextureTPL

This commit is contained in:
Crayon2000 2024-03-06 02:02:14 -05:00
parent d67e646e67
commit d65a16765b
6 changed files with 58 additions and 45 deletions

View file

@ -25,10 +25,14 @@ THE SOFTWARE.
#include <stdio.h>
#include <jpeglib.h>
#include <string.h>
#include <ogc/tpl.h>
#include <grrlib.h>
#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;
@ -188,35 +192,41 @@ 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));
const u32 ntextures = *(u32*)(my_tpl + TPL_HDR_NTEXTURE_FIELD);
if(id > ntextures) {
return NULL;
}
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;
}
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;
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 );
}
else {
free(my_texture);
return NULL;
}
return my_texture;
}
}
return NULL;
}
/**
* Load a texture from a buffer.

View file

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

View file

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

View file

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View file

@ -1 +1,2 @@
<filepath="ballsprites.png" id="ballsprites" colfmt=6 />
<filepath="pirate.png" id="pirate" colfmt=6 />