Skip to content

Commit

Permalink
Enable opening TPLs from a file handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Apr 9, 2022
1 parent c0cfd2b commit 4f373b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions gc/ogc/tpl.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#ifndef __TPL_H__
#define __TPL_H__

#include <stdio.h>
#include "gx.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef void* FHANDLE;

// tdf file
typedef struct _tplfile {
int type;
int ntextures;
void *texdesc;
FHANDLE tpl_file;
FILE *tpl_file;
} TPLFile;

s32 TPL_OpenTPLFromFile(TPLFile* tdf, const char* file_name);
s32 TPL_OpenTPLFromHandle(TPLFile* tdf, FILE *handle);
s32 TPL_OpenTPLFromMemory(TPLFile* tdf, void *memory,u32 len);
s32 TPL_GetTexture(TPLFile *tdf,s32 id,GXTexObj *texObj);
s32 TPL_GetTextureCI(TPLFile *tdf,s32 id,GXTexObj *texObj,GXTlutObj *tlutObj,u8 tluts);
Expand Down
25 changes: 15 additions & 10 deletions libogc/tpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,24 @@ static u32 TPL_GetTextureSize(u32 width,u32 height,u32 fmt)
}

s32 TPL_OpenTPLFromFile(TPLFile* tdf, const char* file_name)
{
if(!file_name) return 0;
return TPL_OpenTPLFromHandle(tdf,fopen(file_name,"rb"));
}

s32 TPL_OpenTPLFromHandle(TPLFile* tdf, FILE *handle)
{
u32 c;
u32 version;
FILE *f = NULL;
FILE *f = handle;
TPLDescHeader *deschead = NULL;
TPLImgHeader *imghead = NULL;
TPLPalHeader *palhead = NULL;

if(!file_name) return 0;

f = fopen(file_name,"rb");
if(!f) return -1;
if(!handle) return -1;

tdf->type = TPL_FILE_TYPE_DISC;
tdf->tpl_file = (FHANDLE)f;
tdf->tpl_file = f;

fread(&version,sizeof(u32),1,f);
fread(&tdf->ntextures,sizeof(u32),1,f);
Expand Down Expand Up @@ -155,7 +158,7 @@ s32 TPL_OpenTPLFromMemory(TPLFile* tdf, void *memory,u32 len)
if(!memory || !len) return -1; //TPL_ERR_INVALID

tdf->type = TPL_FILE_TYPE_MEM;
tdf->tpl_file = (FHANDLE)NULL;
tdf->tpl_file = NULL;

//version = *(u32*)(p + TPL_HDR_VERSION_FIELD);
tdf->ntextures = *(u32*)(p + TPL_HDR_NTEXTURE_FIELD);
Expand Down Expand Up @@ -231,7 +234,8 @@ s32 TPL_GetTexture(TPLFile *tdf,s32 id,GXTexObj *texObj)

size = TPL_GetTextureSize(imghead->width,imghead->height,imghead->fmt);
if(tdf->type==TPL_FILE_TYPE_DISC) {
f = (FILE*)tdf->tpl_file;
f = tdf->tpl_file;

pos = (s32)imghead->data;
imghead->data = memalign(PPC_CACHE_ALIGNMENT,size);
imghead->unpacked = TRUE;
Expand Down Expand Up @@ -279,7 +283,8 @@ s32 TPL_GetTextureCI(TPLFile *tdf,s32 id,GXTexObj *texObj,GXTlutObj *tlutObj,u8

size = TPL_GetTextureSize(imghead->width,imghead->height,imghead->fmt);
if(tdf->type==TPL_FILE_TYPE_DISC) {
f = (FILE*)tdf->tpl_file;
f = tdf->tpl_file;

pos = (s32)imghead->data;
imghead->data = memalign(PPC_CACHE_ALIGNMENT,size);
imghead->unpacked = TRUE;
Expand Down Expand Up @@ -324,7 +329,7 @@ void TPL_CloseTPLFile(TPLFile *tdf)
if(!tdf) return;

if(tdf->type==TPL_FILE_TYPE_DISC) {
f = (FILE*)tdf->tpl_file;
f = tdf->tpl_file;
if(f) fclose(f);

deschead = (TPLDescHeader*)tdf->texdesc;
Expand Down

0 comments on commit 4f373b3

Please sign in to comment.