Skip to content

Commit

Permalink
Fixes to Windows MSVC++
Browse files Browse the repository at this point in the history
MSVC in C++ mode requires a cast from `void *` to a specific type, e.g. with `malloc()` calls.
  • Loading branch information
greg-kennedy committed Dec 2, 2022
1 parent a34920d commit 620a5b9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ Module.symvers
Mkfile.old
dkms.conf

# Visual Studio
*.suo
*.ncb
*.user
Debug/
Release/

# test data
*.bsp
*.wad
16 changes: 8 additions & 8 deletions bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -60,15 +60,15 @@ 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 );
}

// ///////////////////////
// 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 );

// ///////////////////////
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
// 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 <stddef.h>

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
unsigned int texture_count;
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
Expand Down
18 changes: 9 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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 ++) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ++;
}
Expand All @@ -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 ++;
}
Expand Down Expand Up @@ -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 ++;
}
Expand All @@ -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 ++;
Expand All @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
10 changes: 5 additions & 5 deletions wad.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ 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);
do {
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;

// read directory entries
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 ++)
{
Expand Down Expand Up @@ -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 ++;
Expand Down

0 comments on commit 620a5b9

Please sign in to comment.