mirror of
				https://notabug.org/scuti/lib3ddevil1
				synced 2025-05-31 14:11:42 +05:30 
			
		
		
		
	Clean up and reordering of devil1tex component
This commit is contained in:
		@@ -3,9 +3,10 @@
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#pragma pack(push, 1)
 | 
			
		||||
 | 
			
		||||
// disable struct padding
 | 
			
		||||
// to easily impose struct on plain data.
 | 
			
		||||
#pragma pack(push, 1)
 | 
			
		||||
 | 
			
		||||
struct TexturePack {
 | 
			
		||||
    char    id[4];
 | 
			
		||||
@@ -36,12 +37,42 @@ struct TextureBatch {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#pragma pack(pop)
 | 
			
		||||
// re-enable stuct padding for whatever other reason.
 | 
			
		||||
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
//                  Functions
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
// Print Texture Pack Header.
 | 
			
		||||
void printtph(struct TexturePack*);
 | 
			
		||||
 | 
			
		||||
// Print Texture Batch Descriptor.
 | 
			
		||||
void printtbd(struct TextureBatchDescriptor*);
 | 
			
		||||
 | 
			
		||||
// Get Texture Batch Descriptor.
 | 
			
		||||
// ** = 'pass by reference' of a pointer to struct
 | 
			
		||||
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**, 
 | 
			
		||||
                 unsigned int, 
 | 
			
		||||
                 const char*,
 | 
			
		||||
                 unsigned int);
 | 
			
		||||
 | 
			
		||||
// Unpack Texture Batch
 | 
			
		||||
bool unpacktexbatch(struct Texture*,
 | 
			
		||||
                    unsigned int,
 | 
			
		||||
                    const char*, 
 | 
			
		||||
                    const unsigned int);
 | 
			
		||||
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
//                  Retired Functions
 | 
			
		||||
//  These require the client allocate more on the heap.
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
void gettbd(struct TextureBatchDescriptor**,
 | 
			
		||||
            const char*,
 | 
			
		||||
            unsigned int);
 | 
			
		||||
@@ -56,19 +87,4 @@ void locatetextures(unsigned int*,
 | 
			
		||||
                    struct TexturePack*,
 | 
			
		||||
                    struct TextureBatchDescriptor**);
 | 
			
		||||
 | 
			
		||||
bool gettexdescriptor(struct TextureBatchDescriptor**,
 | 
			
		||||
                      unsigned int,
 | 
			
		||||
                      const char *,
 | 
			
		||||
                      unsigned int);
 | 
			
		||||
 | 
			
		||||
bool gettexbatch(struct TextureBatch**, 
 | 
			
		||||
                 unsigned int, 
 | 
			
		||||
                 const char*,
 | 
			
		||||
                 unsigned int);
 | 
			
		||||
 | 
			
		||||
bool unpacktexbatch(struct Texture*,
 | 
			
		||||
                    unsigned int,
 | 
			
		||||
                    const char*, 
 | 
			
		||||
                    const unsigned int);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										143
									
								
								src/devil1tex.c
									
									
									
									
									
								
							
							
						
						
									
										143
									
								
								src/devil1tex.c
									
									
									
									
									
								
							@@ -25,6 +25,76 @@ void printtbd(struct TextureBatchDescriptor *bd) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool gettexdescriptor(struct TextureBatchDescriptor **descriptor,
 | 
			
		||||
                      unsigned int i,
 | 
			
		||||
                      const char *filedata,
 | 
			
		||||
                      unsigned int filesize) {
 | 
			
		||||
    // starts after the pack header.
 | 
			
		||||
    bool done = false;
 | 
			
		||||
    unsigned int offset = sizeof(struct TexturePack);
 | 
			
		||||
    offset += sizeof(struct TextureBatchDescriptor) * i;
 | 
			
		||||
    if (filedata != NULL) {
 | 
			
		||||
        *descriptor = (struct TextureBatchDescriptor*)(filedata + offset);
 | 
			
		||||
        done = true;
 | 
			
		||||
    }
 | 
			
		||||
    return done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool gettexbatch(struct TextureBatch **batch, 
 | 
			
		||||
                 unsigned int i, 
 | 
			
		||||
                 const char *filedata,
 | 
			
		||||
                 unsigned int filesize) {
 | 
			
		||||
    struct TexturePack *p = NULL;
 | 
			
		||||
    if (filedata == NULL) {      // no data to get batch from.
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    p = (struct TexturePack*)filedata;
 | 
			
		||||
    if (i > p -> batchNumber) {  // no such batch in texture pack
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (i == 0) {
 | 
			
		||||
        *batch = (struct TextureBatch*)(filedata + (p -> firstBatchOffset));
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    struct TextureBatchDescriptor *d = NULL;
 | 
			
		||||
    // find how large are previous batches in total
 | 
			
		||||
    // in order to jump to specified texture batch
 | 
			
		||||
    unsigned int pastbatchsize = p -> firstBatchOffset;
 | 
			
		||||
    unsigned int j;
 | 
			
		||||
    for (j = 0; j < i; j++) {
 | 
			
		||||
        gettexdescriptor(&d, j, filedata, filesize);
 | 
			
		||||
        pastbatchsize += (d -> textureSize * d -> texNumber);
 | 
			
		||||
    }
 | 
			
		||||
    *batch = (struct TextureBatch*)(filedata + pastbatchsize);
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool unpacktexbatch(struct Texture *t,
 | 
			
		||||
                    unsigned int i,
 | 
			
		||||
                    const char *filedata, 
 | 
			
		||||
                    const unsigned int filesize) {
 | 
			
		||||
    if (filedata == NULL) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    struct TextureBatch *b           = NULL;
 | 
			
		||||
    struct TextureBatchDescriptor *d = NULL;
 | 
			
		||||
    gettexbatch(&b, i, filedata, filesize);
 | 
			
		||||
    gettexdescriptor(&d, i, filedata, filesize);
 | 
			
		||||
 | 
			
		||||
    // first texture is at the start of the batch
 | 
			
		||||
    // second texture is right after.
 | 
			
		||||
    unsigned int j;
 | 
			
		||||
    for (j = 0; j < d -> texNumber; j++) {
 | 
			
		||||
        t[j].data = (unsigned char*)b + (d -> textureSize * j);
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
//                  Retired Functions
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// get texture pack descriptors
 | 
			
		||||
void gettbd (struct TextureBatchDescriptor **descriptors,
 | 
			
		||||
             const char *filedata,
 | 
			
		||||
@@ -95,79 +165,6 @@ void locatetextures(unsigned int *t,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
//                  Revision Functions
 | 
			
		||||
// -------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
// ** = 'pass by reference' of a pointer to struct
 | 
			
		||||
bool gettexdescriptor(struct TextureBatchDescriptor **descriptor,
 | 
			
		||||
                      unsigned int i,
 | 
			
		||||
                      const char *filedata,
 | 
			
		||||
                      unsigned int filesize) {
 | 
			
		||||
    // starts after the pack header.
 | 
			
		||||
    bool done = false;
 | 
			
		||||
    unsigned int offset = sizeof(struct TexturePack);
 | 
			
		||||
    offset += sizeof(struct TextureBatchDescriptor) * i;
 | 
			
		||||
    if (filedata != NULL) {
 | 
			
		||||
        *descriptor = (struct TextureBatchDescriptor*)(filedata + offset);
 | 
			
		||||
        done = true;
 | 
			
		||||
    }
 | 
			
		||||
//    printf("%x %x", *descriptor, filedata);
 | 
			
		||||
    return done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ** = 'pass by reference' of a pointer to struct
 | 
			
		||||
bool gettexbatch(struct TextureBatch **batch, 
 | 
			
		||||
                 unsigned int i, 
 | 
			
		||||
                 const char *filedata,
 | 
			
		||||
                 unsigned int filesize) {
 | 
			
		||||
    struct TexturePack *p = NULL;
 | 
			
		||||
    if (filedata == NULL) {      // no data to get batch from.
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    p = (struct TexturePack*)filedata;
 | 
			
		||||
    if (i > p -> batchNumber) {  // no such batch in texture pack
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (i == 0) {
 | 
			
		||||
        *batch = (struct TextureBatch*)(filedata + (p -> firstBatchOffset));
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    struct TextureBatchDescriptor *d = NULL;
 | 
			
		||||
    // find how large are previous batches in total
 | 
			
		||||
    // in order to jump to specified texture batch
 | 
			
		||||
    unsigned int pastbatchsize = p -> firstBatchOffset;
 | 
			
		||||
    unsigned int j;
 | 
			
		||||
    for (j = 0; j < i; j++) {
 | 
			
		||||
        gettexdescriptor(&d, j, filedata, filesize);
 | 
			
		||||
        pastbatchsize += (d -> textureSize * d -> texNumber);
 | 
			
		||||
    }
 | 
			
		||||
    *batch = (struct TextureBatch*)(filedata + pastbatchsize);
 | 
			
		||||
    //printf("batch offset: %x i: %d\n" , (p -> firstBatchOffset) + pastbatchsize, i);
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool unpacktexbatch(struct Texture *t,
 | 
			
		||||
                    unsigned int i,
 | 
			
		||||
                    const char *filedata, 
 | 
			
		||||
                    const unsigned int filesize) {
 | 
			
		||||
    if (filedata == NULL) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    struct TextureBatch *b           = NULL;
 | 
			
		||||
    struct TextureBatchDescriptor *d = NULL;
 | 
			
		||||
    gettexbatch(&b, i, filedata, filesize);
 | 
			
		||||
    gettexdescriptor(&d, i, filedata, filesize);
 | 
			
		||||
 | 
			
		||||
    // first texture is at the start of the batch
 | 
			
		||||
    // second texture is right after.
 | 
			
		||||
    unsigned int j;
 | 
			
		||||
    for (j = 0; j < d -> texNumber; j++) {
 | 
			
		||||
        t[j].data = (unsigned char*)b + (d -> textureSize * j);
 | 
			
		||||
        //printf("unpack relative batch offset %x  i= %i t.data= %x\n" , ((d -> textureSize * j)), i, t[j].data);
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user