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

@@ -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,
const char *filedata,
unsigned int filesize) {
if (tbd == NULL || tph == NULL || filedata == NULL) {
// any of these being null will cause segmentation fault.
void gettbd (struct TextureBatchDescriptor **descriptors,
const char *filedata,
unsigned int filesize) {
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;
}