Implemented function that correctly acquires TextureBatchDescriptors

This commit is contained in:
_ 2018-04-04 10:12:29 -07:00
parent 756f28cd41
commit a446dd8741
3 changed files with 39 additions and 33 deletions

View File

@ -38,12 +38,11 @@ struct TextureBatch {
#pragma pack(pop) #pragma pack(pop)
// re-enable stuct padding for whatever other reason. // re-enable stuct padding for whatever other reason.
void gettph(struct TexturePack*, const char*);
void printtph(struct TexturePack*); void printtph(struct TexturePack*);
void gettbd(struct TextureBatchDescriptor*, void printtbd(struct TextureBatchDescriptor*);
struct TexturePack *tph,
void gettbd(struct TextureBatchDescriptor**,
const char*, const char*,
unsigned int); unsigned int);

View File

@ -2,15 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// get texture pack header
void gettph (struct TexturePack *tp, const char *filedata) {
// Assume the Texture Pack Header is already allocated memory.
if (tp != NULL && filedata != NULL) {
memcpy(tp, filedata, sizeof(struct TexturePack));
}
return;
}
void printtph (struct TexturePack *tp) { void printtph (struct TexturePack *tp) {
if (tp != NULL) { if (tp != NULL) {
printf("id: \t %c %c %c %c \n", tp -> id[3], printf("id: \t %c %c %c %c \n", tp -> id[3],
@ -24,31 +15,43 @@ void printtph (struct TexturePack *tp) {
return; return;
} }
void printtbd(struct TextureBatchDescriptor *bd) {
if (bd != NULL) {
printf("batch index: \t %i \n", bd -> batchIdx);
printf("hash: \t %u \n", bd -> hash);
printf("textures: \t %i\n", bd -> texNumber);
}
}
// get texture pack descriptors // get texture pack descriptors
void gettbd (struct TextureBatchDescriptor *tbd, void gettbd (struct TextureBatchDescriptor **descriptors,
struct TexturePack* tph, const char *filedata,
const char *filedata, unsigned int filesize) {
unsigned int filesize) { if (descriptors == NULL || filedata == NULL || filesize <= 0) {
if (tbd == NULL || tph == NULL || filedata == NULL) {
// any of these being null will cause segmentation fault.
return; return;
} }
// do some checks regarding the header struct TexturePack *tp = NULL;
if (tph -> firstBatchOffset >= filesize || tp = (struct TexturePack*)filedata;
tph -> firstBatchOffset <= 0) { if (filesize <= (sizeof(struct TextureBatchDescriptor)
* tp -> batchNumber
+ sizeof(struct TexturePack))){
fprintf(stderr,
"File Error: file size to struct sizes ratio.\n");
return; return;
} }
unsigned int index = tph -> firstBatchOffset; unsigned int index = sizeof(struct TexturePack);
// Batch Descriptors are structured one after another. // Batch Descriptors are structured one after another.
// therefore the next one is at // therefore the next one is at
// current index + sizeof(TextureBatchDescriptor) // current index + sizeof(TextureBatchDescriptor)
unsigned int i = 0; unsigned int i = 0;
do { do {
index += (sizeof(struct TextureBatchDescriptor) * i); index = sizeof(struct TextureBatchDescriptor) * i
+ sizeof(struct TexturePack);
printf("batch descriptor start: %x\n", index); printf("batch descriptor start: %x\n", index);
tbd = (struct TextureBatchDescriptor*)(filedata + index); descriptors[i] = (struct TextureBatchDescriptor*)
// <do something with the batch descriptor found> (filedata + index);
} while(i < (tph -> batchNumber)); i++;
} while(i < (tp -> batchNumber));
return; return;
} }

View File

@ -97,17 +97,21 @@ int main(int argc, char ** argv) {
unsigned int bufsize = 0; unsigned int bufsize = 0;
char *buffer = loadfile(f, &bufsize); char *buffer = loadfile(f, &bufsize);
// unpackpld(buffer, bufsize, f); // unpackpld(buffer, bufsize, f);
struct TexturePack *tp = (struct TexturePack*)malloc struct TexturePack *tp = NULL;
(sizeof(struct TexturePack));
printf("%d %d %d %d \n", buffer[0], printf("%d %d %d %d \n", buffer[0],
buffer[1], buffer[1],
buffer[2], buffer[2],
buffer[3]); buffer[3]);
gettph(tp, buffer); tp = (struct TexturePack*)buffer;
printf("%x %x \n", buffer + 0, tp -> id[0]); struct TextureBatchDescriptor **bds = NULL;
bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber);
gettbd(bds, buffer, bufsize);
printtph(tp); printtph(tp);
int i;
free(tp); for (i = 0; i < tp -> batchNumber; i++) {
printtbd(bds[i]);
}
free(bds);
free(buffer); free(buffer);
return 0; return 0;
} }