mirror of
https://notabug.org/scuti/lib3ddevil1
synced 2024-11-22 13:53:02 +05:30
Added texture pack headers correctly read in
This commit is contained in:
parent
a188a19d77
commit
756f28cd41
5
Makefile
5
Makefile
@ -4,11 +4,14 @@ CFLAGS= -I"include"
|
||||
|
||||
all: main
|
||||
|
||||
main: devil1pld.o
|
||||
main: devil1pld.o devil1tex.o
|
||||
$(CC) $^ test/main.c $(CFLAGS) -o $(EX)
|
||||
|
||||
devil1pld.o: src/devil1pld.c
|
||||
$(CC) -c $^ $(CFLAGS)
|
||||
|
||||
devil1tex.o: src/devil1tex.c
|
||||
$(CC) -c $^ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm *.o $(EX)
|
||||
|
50
include/devil1tex.h
Normal file
50
include/devil1tex.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef DEVIL1TEX_H
|
||||
#define DEVIL1TEX_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
// disable struct padding
|
||||
// to easily impose struct on plain data.
|
||||
|
||||
struct TexturePack {
|
||||
char id[4];
|
||||
int32_t batchNumber;
|
||||
uint32_t firstBatchOffset; // <format=hex>
|
||||
uint32_t unknownA;
|
||||
};
|
||||
|
||||
struct TextureBatchDescriptor{
|
||||
int32_t batchIdx;
|
||||
uint32_t hash; // <format=hex>
|
||||
uint32_t texNumber;
|
||||
uint32_t unknownA[8]; // <format=hex>
|
||||
uint32_t textureSize; // <format=hex>
|
||||
uint32_t unknownB[30];
|
||||
};
|
||||
|
||||
struct Texture {
|
||||
// size of array is defined by descriptor
|
||||
// textureSize
|
||||
unsigned char *data;
|
||||
};
|
||||
|
||||
struct TextureBatch {
|
||||
// quantity of textures are defined by descriptor
|
||||
// texNumber
|
||||
struct Texture *batch;
|
||||
};
|
||||
|
||||
#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,
|
||||
const char*,
|
||||
unsigned int);
|
||||
|
||||
#endif
|
54
src/devil1tex.c
Normal file
54
src/devil1tex.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include "devil1tex.h"
|
||||
#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],
|
||||
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;
|
||||
}
|
||||
|
||||
// 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.
|
||||
return;
|
||||
}
|
||||
// do some checks regarding the header
|
||||
if (tph -> firstBatchOffset >= filesize ||
|
||||
tph -> firstBatchOffset <= 0) {
|
||||
return;
|
||||
}
|
||||
unsigned int index = tph -> firstBatchOffset;
|
||||
// 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);
|
||||
printf("batch descriptor start: %x\n", index);
|
||||
tbd = (struct TextureBatchDescriptor*)(filedata + index);
|
||||
// <do something with the batch descriptor found>
|
||||
} while(i < (tph -> batchNumber));
|
||||
return;
|
||||
}
|
||||
|
19
test/main.c
19
test/main.c
@ -1,9 +1,11 @@
|
||||
#include "devil1pld.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "devil1pld.h"
|
||||
#include "devil1tex.h"
|
||||
|
||||
bool writedata(const char *basename,
|
||||
unsigned int id,
|
||||
const char *data,
|
||||
@ -88,11 +90,24 @@ bool unpackpld (const char *filedata,
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
char *f = argv[1];
|
||||
unsigned int bufsize = 0;
|
||||
char *buffer = loadfile(f, &bufsize);
|
||||
unpackpld(buffer, bufsize, f);
|
||||
// unpackpld(buffer, bufsize, f);
|
||||
struct TexturePack *tp = (struct TexturePack*)malloc
|
||||
(sizeof(struct TexturePack));
|
||||
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]);
|
||||
printtph(tp);
|
||||
|
||||
free(tp);
|
||||
free(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user