From 620a5b98d0ab2128933e7f0f6dc278c0bfbb244f Mon Sep 17 00:00:00 2001 From: Greg Kennedy Date: Thu, 1 Dec 2022 02:34:46 -0600 Subject: [PATCH] Fixes to Windows MSVC++ MSVC in C++ mode requires a cast from `void *` to a specific type, e.g. with `malloc()` calls. --- .gitignore | 7 +++++++ bsp.c | 16 ++++++++-------- bsp.h | 7 +++++-- main.c | 18 +++++++++--------- texture.c | 8 ++++---- utils.h | 4 ++-- wad.c | 10 +++++----- 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 6102a58..0cad126 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,13 @@ Module.symvers Mkfile.old dkms.conf +# Visual Studio +*.suo +*.ncb +*.user +Debug/ +Release/ + # test data *.bsp *.wad diff --git a/bsp.c b/bsp.c index 175b189..6419bd3 100644 --- a/bsp.c +++ b/bsp.c @@ -29,7 +29,7 @@ struct s_bsp * read_bsp(const char * path) } // create a struct to hold our info - struct s_bsp *ret = u_malloc(sizeof(struct s_bsp)); + struct s_bsp *ret = (struct s_bsp *)u_malloc(sizeof(struct s_bsp)); // get all lump info unsigned int lumpOffset[15]; @@ -60,7 +60,7 @@ struct s_bsp * read_bsp(const char * path) if (i == LUMP_ENTITIES || i == LUMP_TEXTURES) continue; u_fseek( fp, lumpOffset[i] ); - ret->raw_lump[i] = u_malloc( ret->raw_lump_size[i] ); + ret->raw_lump[i] = (unsigned char*)u_malloc( ret->raw_lump_size[i] ); u_fread(ret->raw_lump[i], ret->raw_lump_size[i], fp ); } @@ -68,7 +68,7 @@ struct s_bsp * read_bsp(const char * path) // entity lump u_fseek( fp, lumpOffset[LUMP_ENTITIES] ); ret->entity_lump_size = ret->raw_lump_size[LUMP_ENTITIES]; - ret->entity_lump = u_malloc( ret->entity_lump_size ); + ret->entity_lump = (char *)u_malloc( ret->entity_lump_size ); u_fread(ret->entity_lump, ret->entity_lump_size, fp ); // /////////////////////// @@ -77,10 +77,10 @@ struct s_bsp * read_bsp(const char * path) u_fread(buf, 4, fp); ret->texture_count = parseU32(buf); - ret->textures = u_malloc(ret->texture_count * sizeof(struct texture*)); + ret->textures = (struct s_texture **)u_malloc(ret->texture_count * sizeof(struct texture*)); // get the offsets to each texture - unsigned int *mipTexOffsets = u_malloc(ret->texture_count * sizeof(unsigned int)); + unsigned int *mipTexOffsets = (unsigned int*)u_malloc(ret->texture_count * sizeof(unsigned int)); for (unsigned int i = 0; i < ret->texture_count; i ++) { u_fread(buf, 4, fp); @@ -123,8 +123,8 @@ void write_bsp(const struct s_bsp *const bsp, const char* path) // compute lump offsets using BSP lump order // begin with uint32 'version', plus 8 bytes for each of 15 lumps - unsigned int offset = 4 + (8 * 15); - unsigned int lumpOffset[15]; + size_t offset = 4 + (8 * 15); + size_t lumpOffset[15]; for (int a = 0; a < 15; a ++) { // whose turn is it? int i = bsp->lump_order[a]; @@ -152,7 +152,7 @@ void write_bsp(const struct s_bsp *const bsp, const char* path) u_fwrite(buf, 4, fp); // size - unsigned int lumpSize; + size_t lumpSize; if (i == LUMP_ENTITIES) { lumpSize = bsp->entity_lump_size; } else if (i == LUMP_TEXTURES) { diff --git a/bsp.h b/bsp.h index f380b65..f7b5fdf 100644 --- a/bsp.h +++ b/bsp.h @@ -4,11 +4,14 @@ // This is only a partial decoding of a BSP file. // Since we only care about the ENTITY and TEXTURE lumps, // all others are just stored as blocks of data. + +#include + struct s_bsp { // entity lump is a large multi-line ASCII string, // zero-terminated - unsigned int entity_lump_size; + size_t entity_lump_size; char *entity_lump; // texture lump is a series of texture structs @@ -16,7 +19,7 @@ struct s_bsp struct s_texture **textures; // other lumps are copied wholesale - unsigned int raw_lump_size[15]; + size_t raw_lump_size[15]; unsigned char * raw_lump[15]; // order of lumps - helps minimize change when writing diff --git a/main.c b/main.c index 1332524..74a4a68 100644 --- a/main.c +++ b/main.c @@ -28,7 +28,7 @@ int main (int argc, char *argv[]) // read every WAD file and get the Directory out int wad_count = argc - 3; - struct s_wad** wad = u_calloc(wad_count * sizeof(struct wad*)); + struct s_wad** wad = (struct s_wad **)u_calloc(wad_count * sizeof(struct wad*)); for (int i = 3; i < argc; i ++) wad[i-3] = read_wad(argv[i]); @@ -47,7 +47,7 @@ int main (int argc, char *argv[]) // some flags before we begin unsigned int unreferenced_textures = 0; unsigned int embedded_textures = 0; - unsigned int *wad_used = u_calloc(wad_count * sizeof(unsigned int)); + unsigned int *wad_used = (unsigned int *)u_calloc(wad_count * sizeof(unsigned int)); // identify duplicates for (unsigned int i = 0; i < bsp->texture_count; i ++) { @@ -138,10 +138,10 @@ int main (int argc, char *argv[]) if (*end == '"' || *end == ';') { if (end > start) { // Quotes or semicolons terminate a WAD name - char *temp_wad_name = u_calloc(end - start); + char *temp_wad_name = (char *)u_calloc(end - start); for (int i=0; i < end - start; i ++) { - temp_wad_name[i] = tolower(start[i]); + temp_wad_name[i] = (char)tolower(start[i]); } // see if the WAD is in the wadlist already @@ -171,7 +171,7 @@ int main (int argc, char *argv[]) } else { // must be kept because the unreferenced texture might // be in the Mystery WAD - wadlist = u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); + wadlist = (char **)u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); wadlist[wadlist_count] = temp_wad_name; wadlist_count ++; } @@ -182,7 +182,7 @@ int main (int argc, char *argv[]) free(temp_wad_name); } else { // must be kept - wadlist = u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); + wadlist = (char **)u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); wadlist[wadlist_count] = temp_wad_name; wadlist_count ++; } @@ -214,7 +214,7 @@ int main (int argc, char *argv[]) // used WAD from CLI not in list - append it if (! is_matched) { // must be kept - wadlist = u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); + wadlist = (char **)u_realloc(wadlist, sizeof(char *) * (wadlist_count + 1)); wadlist[wadlist_count] = strdup(wad[i]->name); wadlist_count ++; } @@ -229,7 +229,7 @@ int main (int argc, char *argv[]) // smash all these together into a new Entity List // before and up to " // " until end (incl. trailing null) - unsigned int new_entity_lump_size = (p - bsp->entity_lump) + (bsp->entity_lump_size - (end - bsp->entity_lump)); + size_t new_entity_lump_size = (p - bsp->entity_lump) + (bsp->entity_lump_size - (end - bsp->entity_lump)); for (int j = 0; j < wadlist_count; j ++) { // semicolon separator for each WAD name beyond the first if (j > 0) new_entity_lump_size ++; @@ -238,7 +238,7 @@ int main (int argc, char *argv[]) } // we have calculated the size of the output string, ready to allocate it - char *new_entity_lump = u_calloc( new_entity_lump_size ); + char *new_entity_lump = (char *)u_calloc( new_entity_lump_size ); // copy first N bytes - everything up to, and including, "wad" " strncpy(new_entity_lump, bsp->entity_lump, p - bsp->entity_lump); diff --git a/texture.c b/texture.c index f435de4..b119c5a 100644 --- a/texture.c +++ b/texture.c @@ -23,13 +23,13 @@ struct s_texture* read_texture(FILE *const fp, const unsigned int offset) levelOffs[i] = parseU32(buf+24+4*i); // create struct to hold the copied texture data - struct s_texture *ret = u_calloc(sizeof(struct s_texture)); + struct s_texture *ret = (struct s_texture *)u_calloc(sizeof(struct s_texture)); // copy name - uppercased for (int c = 0; c < 15; c ++) { if (buf[c] == '\0') break; - ret->name[c] = toupper(buf[c]); + ret->name[c] = (char)toupper(buf[c]); } // copy other values @@ -45,7 +45,7 @@ struct s_texture* read_texture(FILE *const fp, const unsigned int offset) u_fseek(fp, offset + levelOffs[i]); - ret->level[i] = u_malloc(width * height); + ret->level[i] = (unsigned char *)u_malloc(width * height); u_fread(ret->level[i], width * height, fp); width >>= 1; height >>= 1; @@ -54,7 +54,7 @@ struct s_texture* read_texture(FILE *const fp, const unsigned int offset) u_fread(buf, 2, fp); ret->palette_count = parseU16(buf); - ret->palette = u_malloc(ret->palette_count * 3); + ret->palette = (unsigned char *)u_malloc(ret->palette_count * 3); u_fread(ret->palette, ret->palette_count * 3, fp); } diff --git a/utils.h b/utils.h index 5eeccf6..ae01678 100644 --- a/utils.h +++ b/utils.h @@ -9,8 +9,8 @@ /* endianness */ #define parseU32(x) (*(x) | (*(x+1) << 8) | (*(x+2) << 16) | (*(x+3) << 24)) #define parseU16(x) (*(x) | (*(x+1) << 8)) -#define packU32(x,y) { *(x) = (y & 0xFF); *(x+1) = ((y >> 8) & 0xFF); *(x+2) = ((y >> 16) & 0xFF); *(x+3) = ((y >> 24) & 0xFF); } -#define packU16(x,y) { *(x) = (y & 0xFF); *(x+1) = ((y >> 8) & 0xFF); } +#define packU32(x,y) { *(x) = (unsigned char)(y & 0xFF); *(x+1) = (unsigned char)((y >> 8) & 0xFF); *(x+2) = (unsigned char)((y >> 16) & 0xFF); *(x+3) = (unsigned char)((y >> 24) & 0xFF); } +#define packU16(x,y) { *(x) = (unsigned char)(y & 0xFF); *(x+1) = (unsigned char)((y >> 8) & 0xFF); } /* file handling */ #define u_fopen(fname,mode) u_fopen_int(fname, mode, __FILE__, __LINE__) diff --git a/wad.c b/wad.c index fb46705..80a205e 100644 --- a/wad.c +++ b/wad.c @@ -38,7 +38,7 @@ struct s_wad * read_wad(const char* path) unsigned int dirOffset = parseU32(buf+8); // initialize a structure to hold our data - struct s_wad* ret = u_calloc(sizeof(struct s_wad)); + struct s_wad* ret = (struct s_wad *)u_calloc(sizeof(struct s_wad)); // copy base path into name // HL tends to use lowercased WAD names and uppercased texture names const char *base = path + strlen(path); @@ -46,9 +46,9 @@ struct s_wad * read_wad(const char* path) base--; } while (base > path && *base != '/' && *base != '\\'); - ret->name = u_calloc(strlen(base) + 1); + ret->name = (char *)u_calloc(strlen(base) + 1); for (unsigned int i = 0; i < strlen(base); i ++) - ret->name[i] = tolower(base[i]); + ret->name[i] = (char)tolower(base[i]); unsigned int texture_limit = 0; @@ -56,7 +56,7 @@ struct s_wad * read_wad(const char* path) printf("WAD3 '%s': %u entries beginning at offset %u\n", ret->name, numEntries, dirOffset); u_fseek( fp, dirOffset ); - struct dir_entry* directory = u_calloc(numEntries * sizeof(struct dir_entry)); + struct dir_entry* directory = (struct dir_entry *)u_calloc(numEntries * sizeof(struct dir_entry)); for (unsigned int i = 0; i < numEntries; i ++) { @@ -93,7 +93,7 @@ struct s_wad * read_wad(const char* path) // add it to the list if (ret->texture_count == texture_limit) { texture_limit = (texture_limit << 1) + 1; - ret->textures = u_realloc(ret->textures, texture_limit * sizeof(struct s_texture*)); + ret->textures = (struct s_texture **)u_realloc(ret->textures, texture_limit * sizeof(struct s_texture*)); } ret->textures[ret->texture_count] = tex; ret->texture_count ++;