From 24d4f920dcdc343e874131bd5a5044d0756f19ed Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 21:49:32 -0700 Subject: [PATCH] Unpack textures has correct offsets for textures --- include/devil1tex.h | 4 +++- src/devil1tex.c | 19 +++++++++++++------ test/main.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 7b1678a..46183ef 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -46,7 +46,9 @@ void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); -void gettexturebatch(struct TextureBatch**, +unsigned int gettexturebatch( + struct TextureBatch**, + struct TextureBatchDescriptor**, const char*, unsigned int); #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 002b372..0b5f91e 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -56,23 +56,30 @@ void gettbd (struct TextureBatchDescriptor **descriptors, return; } -void gettexturebatch(struct TextureBatch **tb, +// returns total count of textures in the file. +unsigned int gettexturebatch( + struct TextureBatch **tb, + struct TextureBatchDescriptor **bds, const char *filedata, unsigned int filesize) { struct TexturePack *tp = (struct TexturePack*)filedata; - struct TextureBatchDescriptor **bds = NULL; - bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); - gettbd(bds, filedata, filesize); +// struct TextureBatchDescriptor **bds = NULL; +// bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); +// gettbd(bds, filedata, filesize); unsigned int offset = tp -> firstBatchOffset; unsigned int i; unsigned int j; + unsigned int totaltextures = 0; // TextureBatch shares a starting address with the first texture // and shares the ending address with the last texture. for (i = 0; i < tp -> batchNumber; i++) { printf("%x \n", offset); tb[i] = (struct TextureBatch*)filedata + offset; // the next texture batch is TextureSize * number of textures away. - offset += bds[i] -> textureSize * (bds[i] -> texNumber); + offset += bds[i] -> textureSize * (bds[i] -> texNumber); + totaltextures += bds[i] -> texNumber; } - free(bds); +// free(bds); + return totaltextures; } + diff --git a/test/main.c b/test/main.c index 2f70e06..ba8f4a9 100644 --- a/test/main.c +++ b/test/main.c @@ -102,18 +102,55 @@ bool unpackpld (const char *filedata, } free(bds); return; + + const char *fmt = (const char*)malloc(strlen(filename) + 3 + 3); + strcat(fmt, filename); + strcat(fmt, "%d.dds"); } */ +void unpacktextures(const char *filedata, + unsigned int filesize) { + struct TexturePack *tp = (struct TexturePack*)filedata; + + struct TextureBatch **batches = NULL; + batches = (struct TextureBatch**)malloc(tp -> batchNumber); + + struct TextureBatchDescriptor **descripts = NULL; + descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); + gettbd(descripts, filedata, filesize); + // array of pointers - texture data exists in memory in filedata. + unsigned int texturecount = gettexturebatch(batches, + descripts, + filedata, + filesize); + struct Texture **textures = NULL; + textures = (struct Texture**)malloc(texturecount); + + unsigned int i; + unsigned int j; + // k is access to index of textures. + unsigned int k = 0; + unsigned int offset = 0; + for (i = 0; i < tp -> batchNumber; i++) { + for (j = 0; j < descripts[i] -> texNumber; j++) { + printf("texture offsets %x\n", offset + tp -> firstBatchOffset); + textures[k] = (struct Texture*)batches + offset; + offset += descripts[i] -> textureSize; + k++; + } + } + free(textures); + free(descripts); + free(batches); +} + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); // unpacktextures(buffer, bufsize); - struct TexturePack *tp = (struct TexturePack*)buffer; - struct TextureBatch **batches = NULL; - batches = (struct TextureBatch**)malloc(tp -> batchNumber); - gettexturebatch(batches, buffer, bufsize); + unpacktextures(buffer, bufsize); free(buffer); return 0; }