From 9bfc1251585f41cd61db8dd966bbf1f0b8a964d0 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 19:37:59 -0700 Subject: [PATCH 1/4] Added namespace structs for pld's --- include/devil1pld.h | 15 +++++++++++---- src/devil1pld.c | 8 +++++--- test/main.c | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index d57bfd7..f79e496 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -15,14 +15,21 @@ struct PldHeader { // input: pointer to a struct, contents of the .pld file. // * = pass by reference of a struct PldHeader -bool getpldh(struct PldHeader*, const char*); +static bool getpldh(struct PldHeader*, const char*); // input: pointer to header, index of offset, filesize // * = pass by reference of a struct PldHeader -int sizeofpldstruct(struct PldHeader*, unsigned int, unsigned int); +static int sizeofpldstruct(struct PldHeader*, unsigned int, unsigned int); // input: a pld header struct. // * = pass by reference of a struct PldHeader -void printpldh(struct PldHeader*); +static void printpldh(struct PldHeader*); -#endif \ No newline at end of file +typedef struct { + bool (* const getheader) (struct PldHeader*, const char*); + int (* const sizeofsector)(struct PldHeader*, unsigned int, unsigned int); + void (* const printheader) (struct PldHeader*); +} namespace_struct; +extern namespace_struct const DEVIL1PLD; + +#endif diff --git a/src/devil1pld.c b/src/devil1pld.c index bc0e378..148e323 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -1,7 +1,7 @@ #include "devil1pld.h" #include -bool getpldh(struct PldHeader *ph, const char *filedata) { +static bool getpldh(struct PldHeader *ph, const char *filedata) { bool good = false; if (ph != NULL && filedata != NULL) { ph -> numOffset = (int32_t)filedata[0]; @@ -12,7 +12,7 @@ bool getpldh(struct PldHeader *ph, const char *filedata) { } // determine the size of the i-th pld structure -int sizeofpldstruct(struct PldHeader *ph, unsigned int i, unsigned int max) { +static int sizeofpldstruct(struct PldHeader *ph, unsigned int i, unsigned int max) { unsigned int size = -1; if (ph == NULL) { fprintf(stderr, "Error: given null pointer\n"); @@ -29,7 +29,7 @@ int sizeofpldstruct(struct PldHeader *ph, unsigned int i, unsigned int max) { return size; } -void printpldh(struct PldHeader *ph) { +static void printpldh(struct PldHeader *ph) { if (ph == NULL) { return; } @@ -40,3 +40,5 @@ void printpldh(struct PldHeader *ph) { printf("offset %i = %x\n", i, ph -> offsets[i]); } } + +namespace_struct const DEVIL1PLD = {getpldh, sizeofpldstruct, printpldh}; diff --git a/test/main.c b/test/main.c index 881b260..0b685da 100644 --- a/test/main.c +++ b/test/main.c @@ -67,7 +67,7 @@ bool unpackpld(const char *filedata, return false; } struct PldHeader h; - getpldh(&h, filedata); + DEVIL1PLD.getheader(&h, filedata); char *fn = NULL; fn = (char*)malloc(strlen(filename) + 3 + 4); int size = 0; @@ -75,7 +75,7 @@ bool unpackpld(const char *filedata, char textureid[TYPE_ID_LENGTH] = {'\0', '2', '3', 'T'}; for (i = 0; i < h.numOffset; i++) { const char * currentfile = filedata + h.offsets[i]; - size = sizeofpldstruct(&h, i, filesize); + size = DEVIL1PLD.sizeofsector(&h, i, filesize); if (strncmp( currentfile, textureid, TYPE_ID_LENGTH ) == 0) sprintf(fn, "%s_%d.txp", filename, i); else From 40db88d4ebb955d2b267322f14bf30ea6243d3f2 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 19:42:28 -0700 Subject: [PATCH 2/4] Minor re-ordering --- include/devil1pld.h | 13 +++++++------ src/devil1pld.c | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index f79e496..5de204f 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -13,6 +13,13 @@ struct PldHeader { #pragma pack(pop) +typedef struct { + bool (* const getheader) (struct PldHeader*, const char*); + int (* const sizeofsector)(struct PldHeader*, unsigned int, unsigned int); + void (* const printheader) (struct PldHeader*); +} namespace_struct; +extern namespace_struct const DEVIL1PLD; + // input: pointer to a struct, contents of the .pld file. // * = pass by reference of a struct PldHeader static bool getpldh(struct PldHeader*, const char*); @@ -25,11 +32,5 @@ static int sizeofpldstruct(struct PldHeader*, unsigned int, unsigned int); // * = pass by reference of a struct PldHeader static void printpldh(struct PldHeader*); -typedef struct { - bool (* const getheader) (struct PldHeader*, const char*); - int (* const sizeofsector)(struct PldHeader*, unsigned int, unsigned int); - void (* const printheader) (struct PldHeader*); -} namespace_struct; -extern namespace_struct const DEVIL1PLD; #endif diff --git a/src/devil1pld.c b/src/devil1pld.c index 148e323..d29aafe 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -1,6 +1,8 @@ #include "devil1pld.h" #include +namespace_struct const DEVIL1PLD = {getpldh, sizeofpldstruct, printpldh}; + static bool getpldh(struct PldHeader *ph, const char *filedata) { bool good = false; if (ph != NULL && filedata != NULL) { @@ -40,5 +42,3 @@ static void printpldh(struct PldHeader *ph) { printf("offset %i = %x\n", i, ph -> offsets[i]); } } - -namespace_struct const DEVIL1PLD = {getpldh, sizeofpldstruct, printpldh}; From 48f255eb36677949d14f0f4dc6fa73a3ffa11cd7 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 20:03:06 -0700 Subject: [PATCH 3/4] Added namespace structs for textures --- include/devil1pld.h | 4 ++-- include/devil1tex.h | 28 +++++++++++++++++++++++----- src/devil1pld.c | 2 +- src/devil1tex.c | 16 +++++++++++----- test/main.c | 8 ++++---- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/include/devil1pld.h b/include/devil1pld.h index 5de204f..ef6abac 100644 --- a/include/devil1pld.h +++ b/include/devil1pld.h @@ -17,8 +17,8 @@ typedef struct { bool (* const getheader) (struct PldHeader*, const char*); int (* const sizeofsector)(struct PldHeader*, unsigned int, unsigned int); void (* const printheader) (struct PldHeader*); -} namespace_struct; -extern namespace_struct const DEVIL1PLD; +} fn_devil1pld; +extern fn_devil1pld const DEVIL1PLD; // input: pointer to a struct, contents of the .pld file. // * = pass by reference of a struct PldHeader diff --git a/include/devil1tex.h b/include/devil1tex.h index db19345..bc37674 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -38,32 +38,50 @@ struct TextureBatch { #pragma pack(pop) +typedef struct { + void (* const printheader) (struct TexturePack*); + void (* const printbatchdesc)(struct TextureBatchDescriptor*); + bool (* const getbatchdesc) (struct TextureBatchDescriptor**, + unsigned int, + const char *, + unsigned int); + bool (* const getbatch) (struct TextureBatch**, + unsigned int, + const char*, + unsigned int); + bool (* const gettextures) (struct Texture*, + unsigned int, + const char*, + const unsigned int); +} fn_devil1tex; +extern fn_devil1tex const DEVIL1TEX; + // -------------------------------------------------------+ // Functions // -------------------------------------------------------+ // Print Texture Pack Header. -void printtph(struct TexturePack*); +static void printtph(struct TexturePack*); // Print Texture Batch Descriptor. -void printtbd(struct TextureBatchDescriptor*); +static void printtbd(struct TextureBatchDescriptor*); // Get Texture Batch Descriptor. // ** = 'pass by reference' of a pointer to struct -bool gettexdescriptor(struct TextureBatchDescriptor**, +static bool gettexdescriptor(struct TextureBatchDescriptor**, unsigned int, const char *, unsigned int); // Get Texture Batch. // ** = 'pass by reference' of a pointer to struct -bool gettexbatch(struct TextureBatch**, +static bool gettexbatch(struct TextureBatch**, unsigned int, const char*, unsigned int); // Unpack Texture Batch -bool unpacktexbatch(struct Texture*, +static bool unpacktexbatch(struct Texture*, unsigned int, const char*, const unsigned int); diff --git a/src/devil1pld.c b/src/devil1pld.c index d29aafe..3e3d96f 100644 --- a/src/devil1pld.c +++ b/src/devil1pld.c @@ -1,7 +1,7 @@ #include "devil1pld.h" #include -namespace_struct const DEVIL1PLD = {getpldh, sizeofpldstruct, printpldh}; +fn_devil1pld const DEVIL1PLD = {getpldh, sizeofpldstruct, printpldh}; static bool getpldh(struct PldHeader *ph, const char *filedata) { bool good = false; diff --git a/src/devil1tex.c b/src/devil1tex.c index 486f068..ed1417d 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -2,7 +2,13 @@ #include #include -void printtph (struct TexturePack *tp) { +fn_devil1tex const DEVIL1TEX = {printtph, + printtbd, + gettexdescriptor, + gettexbatch, + unpacktexbatch}; + +static void printtph (struct TexturePack *tp) { if (tp != NULL) { printf("id: \t %c %c %c %c \n", tp -> id[3], tp -> id[2], @@ -15,7 +21,7 @@ void printtph (struct TexturePack *tp) { return; } -void printtbd(struct TextureBatchDescriptor *bd) { +static void printtbd(struct TextureBatchDescriptor *bd) { if (bd != NULL) { printf("batch index: \t %i \n", bd -> batchIdx); printf("hash: \t %u \n", bd -> hash); @@ -23,7 +29,7 @@ void printtbd(struct TextureBatchDescriptor *bd) { } } -bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, +static bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, unsigned int i, const char *filedata, unsigned int filesize) { @@ -38,7 +44,7 @@ bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, return done; } -bool gettexbatch(struct TextureBatch **batch, +static bool gettexbatch(struct TextureBatch **batch, unsigned int i, const char *filedata, unsigned int filesize) { @@ -67,7 +73,7 @@ bool gettexbatch(struct TextureBatch **batch, return true; } -bool unpacktexbatch(struct Texture *t, +static bool unpacktexbatch(struct Texture *t, unsigned int i, const char *filedata, const unsigned int filesize) { diff --git a/test/main.c b/test/main.c index 0b685da..ddef757 100644 --- a/test/main.c +++ b/test/main.c @@ -102,10 +102,10 @@ void exporttextures(const char *filedata, unsigned int j; unsigned int id = 0; for (i = 0; i < p -> batchNumber; i++) { - gettexdescriptor(&d, i, filedata, filesize); + DEVIL1TEX.getbatchdesc(&d, i, filedata, filesize); t = (struct Texture*) malloc(sizeof(struct Texture) * (d -> texNumber)); - unpacktexbatch(t, i, filedata, filesize); + DEVIL1TEX.gettextures(t, i, filedata, filesize); for (j = 0; j < d -> texNumber; j++) { sprintf(fmt, "%s_%d.dds", filename, id); write(fmt, t[j].data, d -> textureSize); @@ -144,8 +144,8 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); -// exporttextures(buffer, bufsize, f); - extractmeshes(buffer, bufsize, f); + exporttextures(buffer, bufsize, f); +// extractmeshes(buffer, bufsize, f); free(buffer); return 0; } From 6babd59d56400830ad5becb8ede38f59ed50cb6e Mon Sep 17 00:00:00 2001 From: _ <_> Date: Tue, 17 Apr 2018 20:14:45 -0700 Subject: [PATCH 4/4] Add namespace structs for geometry --- include/devil1geo.h | 34 +++++++++++++++++++++++++++------- src/devil1geo.c | 22 +++++++++++++++------- test/main.c | 8 ++++---- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index 048b5e1..00375ad 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -72,20 +72,40 @@ struct Mesh { #pragma pack(pop) -void printgheader(struct Header*); +typedef struct { + void (* const printheader) (struct Header*); + void (* const printmeshheader)(struct MeshHeader*); + void (* const printbatch) (struct Batch*); + void (* const printcoordinate)(struct Coordinate*, unsigned int); -void printmeshheader(struct MeshHeader*); + bool (* const getmeshheader) (struct MeshHeader**, + unsigned int i, + const char * const); -void printmeshbatch(struct Batch*); + bool (* const getbatch) (struct Batch*, + unsigned int offset, + const char * const); -void printcoordinate(struct Coordinate*, unsigned int); + bool (* const getmesh) (struct Mesh*, + unsigned int i, + const char* filename); +} fn_devil1geo; +extern fn_devil1geo const DEVIL1GEO; + +static void printgheader(struct Header*); + +static void printmeshheader(struct MeshHeader*); + +static void printmeshbatch(struct Batch*); + +static void printcoordinate(struct Coordinate*, unsigned int); // ** = 'pass by reference' of a pointer to struct -bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); +static bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); -bool getmeshbatch(struct Batch*, unsigned int offset, const char * const); +static bool getmeshbatch(struct Batch*, unsigned int offset, const char * const); -bool getmesh(struct Mesh*, unsigned int i, const char* filename); +static bool getmesh(struct Mesh*, unsigned int i, const char* filename); #endif diff --git a/src/devil1geo.c b/src/devil1geo.c index 401eafe..bf3cfb2 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -1,7 +1,15 @@ #include "devil1geo.h" #include -void printgheader(struct Header *gh) { +fn_devil1geo const DEVIL1GEO = {printgheader, + printmeshheader, + printmeshbatch, + printcoordinate, + getmeshheader, + getmeshbatch, + getmesh}; + +static void printgheader(struct Header *gh) { if (gh != NULL) { printf("pointer %x\n", gh); printf("number of meshes %x\n", gh -> numMesh); @@ -13,7 +21,7 @@ void printgheader(struct Header *gh) { } } -void printmeshheader(struct MeshHeader *mh) { +static void printmeshheader(struct MeshHeader *mh) { if (mh == NULL) { return; } @@ -24,7 +32,7 @@ void printmeshheader(struct MeshHeader *mh) { printf("flags %x\n\n", mh -> flags); } -void printmeshbatch(struct Batch *b) { +static void printmeshbatch(struct Batch *b) { if (b == NULL) { return; } @@ -41,7 +49,7 @@ void printmeshbatch(struct Batch *b) { printcoordinate(b -> vd.positions, 3); } -void printcoordinate(struct Coordinate *p, unsigned int count) { +static void printcoordinate(struct Coordinate *p, unsigned int count) { if (p == NULL) { return; } @@ -51,7 +59,7 @@ void printcoordinate(struct Coordinate *p, unsigned int count) { } } -bool getmeshheader(struct MeshHeader **hs, +static bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { bool done = false; @@ -68,7 +76,7 @@ bool getmeshheader(struct MeshHeader **hs, return done; } -bool getmeshbatch(struct Batch *b, +static bool getmeshbatch(struct Batch *b, unsigned int offset, const char * const filedata) { bool done = false; @@ -90,7 +98,7 @@ bool getmeshbatch(struct Batch *b, } // assume client has allocated memory for mesh -bool getmesh(struct Mesh *m, +static bool getmesh(struct Mesh *m, unsigned int i, const char * const filedata) { bool done = false; diff --git a/test/main.c b/test/main.c index ddef757..ab0cb5d 100644 --- a/test/main.c +++ b/test/main.c @@ -129,10 +129,10 @@ void extractmeshes(const char *filedata, m.b = NULL; unsigned int i; for (i = 0; i < h -> numMesh; i++) { - getmeshheader(&mh, i, filedata); + DEVIL1GEO.getmeshheader(&mh, i, filedata); m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch)); if (m.b != NULL) { - getmesh(&m, i, filedata); + DEVIL1GEO.getmesh(&m, i, filedata); // do something with mesh e.g write to file. free(m.b); } @@ -144,8 +144,8 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); - exporttextures(buffer, bufsize, f); -// extractmeshes(buffer, bufsize, f); +// exporttextures(buffer, bufsize, f); + extractmeshes(buffer, bufsize, f); free(buffer); return 0; }