diff --git a/test/main.c b/test/main.c index b86d540..2e0f017 100644 --- a/test/main.c +++ b/test/main.c @@ -108,14 +108,31 @@ bool unpackpld (const char *filedata, strcat(fmt, "%d.dds"); } */ +void write(const char *filename, + unsigned int id, + struct Texture *t, + unsigned int size) { + if (filename == NULL) { + return; + } + unsigned int written = 0; + FILE *out = fopen(filename, "wb"); + if (out != NULL) { + written = fwrite(t, sizeof(unsigned char), size, out); + fclose(out); + if (written == 0) { + perror("texture write error"); + } + } +} + void unpacktextures(const char *filedata, - unsigned int filesize) { + unsigned int filesize, + const char *filename) { struct TexturePack *tp = (struct TexturePack*)filedata; - - struct TextureBatch **batches = NULL; - batches = (struct TextureBatch**)malloc(tp -> batchNumber); - + struct TextureBatch **batches = NULL; struct TextureBatchDescriptor **descripts = NULL; + batches = (struct TextureBatch**)malloc(tp -> batchNumber); descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); gettbd(descripts, filedata, filesize); // array of pointers - texture data exists in memory in filedata. @@ -124,9 +141,19 @@ void unpacktextures(const char *filedata, filedata, filesize); struct Texture **textures = NULL; - textures = (struct Texture**)malloc( - sizeof(struct Texture**) * texturecount); + textures = (struct Texture**) + malloc(sizeof(struct Texture**) * texturecount); gettextures(textures, tp, batches, descripts); + // add 3 to buffer for digits, & 4 for file extension + char *fmt = (char*)malloc(strlen(filename) + 3 + 4); + unsigned int texsize = 0; + unsigned int i; + for (i = 0; i < texturecount; i++) { + texsize = descripts[i/(tp -> batchNumber)] -> textureSize; + sprintf(fmt, "test_%d.dds", i); + write(fmt, i, textures[i], texsize); + } + free(fmt); free(textures); free(descripts); free(batches); @@ -137,8 +164,7 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); -// unpacktextures(buffer, bufsize); - unpacktextures(buffer, bufsize); + unpacktextures(buffer, bufsize, f); free(buffer); return 0; }