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)
// re-enable stuct padding for whatever other reason.
void gettph(struct TexturePack*, const char*);
void printtph(struct TexturePack*);
void gettbd(struct TextureBatchDescriptor*,
struct TexturePack *tph,
void printtbd(struct TextureBatchDescriptor*);
void gettbd(struct TextureBatchDescriptor**,
const char*,
unsigned int);

View File

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

View File

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