lib3ddevil1/src/devil1tex.c

79 lines
2.8 KiB
C
Raw Normal View History

#include "devil1tex.h"
#include <stdio.h>
2018-04-05 08:08:30 +05:30
#include <stdlib.h>
#include <string.h>
void printtph (struct TexturePack *tp) {
if (tp != NULL) {
printf("id: \t %c %c %c %c \n", tp -> id[3],
tp -> id[2],
tp -> id[1],
tp -> id[0]);
printf("batch#: \t %i \n", tp -> batchNumber);
printf("1st batch: \t %i \n", tp -> firstBatchOffset);
printf("unknownA: \t %i\n", tp -> unknownA);
}
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 **descriptors,
const char *filedata,
unsigned int filesize) {
if (descriptors == NULL || filedata == NULL || filesize <= 0) {
return;
}
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 = 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
+ sizeof(struct TexturePack);
printf("batch descriptor start: %x\n", index);
descriptors[i] = (struct TextureBatchDescriptor*)
(filedata + index);
i++;
} while(i < (tp -> batchNumber));
return;
}
2018-04-05 08:08:30 +05:30
void gettexturebatch(struct TextureBatch **tb,
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);
unsigned int offset = tp -> firstBatchOffset;
unsigned int i;
unsigned int j;
// 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);
}
free(bds);
}