Skip to content

Commit

Permalink
Refactor DSDHacked initialization
Browse files Browse the repository at this point in the history
See issue #846.
  • Loading branch information
bradharding committed Nov 2, 2024
1 parent 0869ba1 commit b6987c2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 127 deletions.
61 changes: 30 additions & 31 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "i_system.h"
#include "info.h"
#include "m_array.h"
#include "p_local.h"
#include "r_defs.h"
#include "sounds.h"
Expand Down Expand Up @@ -7557,38 +7558,36 @@ void InitMobjInfo(void)

void dsdh_EnsureMobjInfoCapacity(const int limit)
{
const int old_nummobjtypes = nummobjtypes;
static bool first_allocation = true;

while (limit >= nummobjtypes)
{
const int old_nummobjtypes = nummobjtypes;

nummobjtypes *= 2;

if (first_allocation)
{
first_allocation = false;
mobjinfo = I_Malloc(nummobjtypes * sizeof(*mobjinfo));
memcpy(mobjinfo, original_mobjinfo, old_nummobjtypes * sizeof(*mobjinfo));
}
else
mobjinfo = I_Realloc(mobjinfo, nummobjtypes * sizeof(*mobjinfo));

memset(mobjinfo + old_nummobjtypes, 0,
((size_t)nummobjtypes - old_nummobjtypes) * sizeof(*mobjinfo));

for (int i = old_nummobjtypes; i < nummobjtypes; i++)
{
mobjinfo[i].bloodcolor = REDBLOOD;
mobjinfo[i].droppeditem = MT_NULL;
mobjinfo[i].infightinggroup = IG_DEFAULT;
mobjinfo[i].projectilegroup = PG_DEFAULT;
mobjinfo[i].splashgroup = SG_DEFAULT;
mobjinfo[i].altspeed = NO_ALTSPEED;
mobjinfo[i].meleerange = MELEERANGE;
M_snprintf(mobjinfo[i].name1, sizeof(mobjinfo[0].name1), "Deh_Actor_%i", i);
M_snprintf(mobjinfo[i].name2, sizeof(mobjinfo[0].name2), "Deh_Actor_%i", i);
M_snprintf(mobjinfo[i].name3, sizeof(mobjinfo[0].name3), "Deh_Actor_%i", i);
}
if (limit < nummobjtypes)
return;

if (first_allocation)
{
mobjinfo = NULL;
array_grow(mobjinfo, old_nummobjtypes + limit);
memcpy(mobjinfo, original_mobjinfo, old_nummobjtypes * sizeof(*mobjinfo));
first_allocation = false;
}
else
array_grow(mobjinfo, limit);

nummobjtypes = array_capacity(mobjinfo);
memset(mobjinfo + old_nummobjtypes, 0, (nummobjtypes - old_nummobjtypes) * sizeof(*mobjinfo));

for (int i = old_nummobjtypes; i < nummobjtypes; i++)
{
mobjinfo[i].bloodcolor = REDBLOOD;
mobjinfo[i].droppeditem = MT_NULL;
mobjinfo[i].infightinggroup = IG_DEFAULT;
mobjinfo[i].projectilegroup = PG_DEFAULT;
mobjinfo[i].splashgroup = SG_DEFAULT;
mobjinfo[i].altspeed = NO_ALTSPEED;
mobjinfo[i].meleerange = MELEERANGE;
M_snprintf(mobjinfo[i].name1, sizeof(mobjinfo[0].name1), "Deh_Actor_%i", i);
M_snprintf(mobjinfo[i].name2, sizeof(mobjinfo[0].name2), "Deh_Actor_%i", i);
M_snprintf(mobjinfo[i].name3, sizeof(mobjinfo[0].name3), "Deh_Actor_%i", i);
}
}
8 changes: 8 additions & 0 deletions src/m_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,20 @@ inline static int array_size(const void *v)
return (v ? array_ptr(v)->size : 0);
}

inline static int array_capacity(const void *v)
{
return (v ? array_ptr(v)->capacity : 0);
}

inline static void array_clear(const void *v)
{
if (v)
array_ptr(v)->size = 0;
}

#define array_grow(v, n) \
((v) = M_ArrayGrow((v), sizeof(*(v)), n))

#define array_push(v, e) \
{ \
if (!(v)) \
Expand Down
69 changes: 34 additions & 35 deletions src/sounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "doomdef.h"
#include "i_system.h"
#include "m_array.h"
#include "m_misc.h"
#include "sounds.h"

Expand Down Expand Up @@ -484,71 +485,69 @@ sfxinfo_t original_s_sfx[NUMSFX] =
sfxinfo_t *s_sfx;
int numsfx;
static char **deh_soundnames;
static int deh_soundnames_size;
static byte *sfx_state;

void InitSFX(void)
{
s_sfx = original_s_sfx;
numsfx = NUMSFX;
deh_soundnames_size = numsfx + 1;
deh_soundnames = I_Malloc(deh_soundnames_size * sizeof(*deh_soundnames));

array_grow(deh_soundnames, numsfx);

for (int i = 1; i < numsfx; i++)
deh_soundnames[i] = (*s_sfx[i].name1 ? M_StringDuplicate(s_sfx[i].name1) : NULL);

deh_soundnames[0] = NULL;
deh_soundnames[numsfx] = NULL;
sfx_state = calloc(numsfx, sizeof(*sfx_state));
array_grow(sfx_state, numsfx);
memset(sfx_state, 0, numsfx * sizeof(*sfx_state));
}

void FreeSFX(void)
{
for (int i = 1; i < deh_soundnames_size; i++)
for (int i = 1; i < array_capacity(deh_soundnames); i++)
if (deh_soundnames[i])
free(deh_soundnames[i]);

free(deh_soundnames);
free(sfx_state);
array_free(deh_soundnames);
array_free(sfx_state);
}

void dsdh_EnsureSFXCapacity(const int limit)
{
static int first_allocation = true;

while (limit >= numsfx)
{
const int old_numsfx = numsfx;
const int old_numsfx = numsfx;
static bool first_allocation = true;
int size_delta;

numsfx *= 2;
if (limit < numsfx)
return;

if (first_allocation)
{
first_allocation = false;
s_sfx = I_Malloc(numsfx * sizeof(*s_sfx));
memcpy(s_sfx, original_s_sfx, old_numsfx * sizeof(*s_sfx));
}
else
s_sfx = I_Realloc(s_sfx, numsfx * sizeof(*s_sfx));
if (first_allocation)
{
s_sfx = NULL;
array_grow(s_sfx, old_numsfx + limit);
memcpy(s_sfx, original_s_sfx, old_numsfx * sizeof(*s_sfx));
first_allocation = false;
}
else
array_grow(s_sfx, limit);

memset(s_sfx + old_numsfx, 0, (numsfx - old_numsfx) * sizeof(*s_sfx));
numsfx = array_capacity(s_sfx);
size_delta = numsfx - old_numsfx;
memset(s_sfx + old_numsfx, 0, size_delta * sizeof(*s_sfx));

sfx_state = I_Realloc(sfx_state, numsfx * sizeof(*sfx_state));
memset(sfx_state + old_numsfx, 0,
(numsfx - old_numsfx) * sizeof(*sfx_state));
array_grow(sfx_state, size_delta);
memset(sfx_state + old_numsfx, 0, size_delta * sizeof(*sfx_state));

for (int i = old_numsfx; i < numsfx; i++)
{
s_sfx[i].priority = 127;
s_sfx[i].lumpnum = -1;
}
for (int i = old_numsfx; i < numsfx; i++)
{
s_sfx[i].priority = 127;
s_sfx[i].lumpnum = -1;
}
}

int dsdh_GetDehSFXIndex(const char *key, size_t length)
{
for (int i = 1; i < numsfx; i++)
if (*s_sfx[i].name1
if (s_sfx[i].name1
&& strlen(s_sfx[i].name1) == length
&& !strncasecmp(s_sfx[i].name1, key, length)
&& !sfx_state[i])
Expand All @@ -564,8 +563,8 @@ int dsdh_GetOriginalSFXIndex(const char *key)
{
int limit;

for (int i = 1; deh_soundnames[i]; i++)
if (!strncasecmp(deh_soundnames[i], key, 6))
for (int i = 1; i < array_capacity(deh_soundnames); i++)
if (deh_soundnames[i] && !strncasecmp(deh_soundnames[i], key, 6))
return i;

// is it a number?
Expand Down
56 changes: 28 additions & 28 deletions src/sprites.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include "doomdef.h"
#include "i_system.h"
#include "m_array.h"
#include "m_misc.h"
#include "sprites.h"

Expand Down Expand Up @@ -945,58 +946,57 @@ const sproffset_t sproffsets[] =
char **sprnames;
int numsprites;
static char **deh_spritenames;
static int deh_spritenames_size;
static byte *sprnames_state;

void InitSprites(void)
{
sprnames = original_sprnames;
numsprites = NUMSPRITES;
deh_spritenames_size = numsprites + 1;
deh_spritenames = I_Malloc(deh_spritenames_size * sizeof(*deh_spritenames));

array_grow(deh_spritenames, numsprites);

for (int i = 0; i < numsprites; i++)
deh_spritenames[i] = M_StringDuplicate(sprnames[i]);

deh_spritenames[numsprites] = NULL;
sprnames_state = calloc(numsprites, sizeof(*sprnames_state));
array_grow(sprnames_state, numsprites);
memset(sprnames_state, 0, numsprites * sizeof(*sprnames_state));
}

void FreeSprites(void)
{
for (int i = 0; i < deh_spritenames_size; i++)
for (int i = 0; i < array_capacity(deh_spritenames); i++)
if (deh_spritenames[i])
free(deh_spritenames[i]);

free(deh_spritenames);
free(sprnames_state);
array_free(deh_spritenames);
array_free(sprnames_state);
}

static void EnsureSpritesCapacity(const int limit)
{
const int old_numsprites = numsprites;
static bool first_allocation = true;
int size_delta;

while (limit >= numsprites)
{
const int old_numsprites = numsprites;

numsprites *= 2;
if (limit < numsprites)
return;

if (first_allocation)
{
first_allocation = false;
sprnames = I_Malloc(numsprites * sizeof(*sprnames));
memcpy(sprnames, original_sprnames, old_numsprites * sizeof(*sprnames));
}
else
sprnames = I_Realloc(sprnames, numsprites * sizeof(*sprnames));
if (first_allocation)
{
sprnames = NULL;
array_grow(sprnames, old_numsprites + limit);
memcpy(sprnames, original_sprnames, old_numsprites * sizeof(*sprnames));
first_allocation = false;
}
else
array_grow(sprnames, limit);

memset(sprnames + old_numsprites, 0, (numsprites - old_numsprites) * sizeof(*sprnames));
numsprites = array_capacity(sprnames);
size_delta = numsprites - old_numsprites;
memset(sprnames + old_numsprites, 0, size_delta * sizeof(*sprnames));

sprnames_state = I_Realloc(sprnames_state, numsprites * sizeof(*sprnames_state));
memset(sprnames_state + old_numsprites, 0,
(numsprites - old_numsprites) * sizeof(*sprnames_state));
}
array_grow(sprnames_state, size_delta);
memset(sprnames_state + old_numsprites, 0, size_delta * sizeof(*sprnames_state));
}

int dsdh_GetDehSpriteIndex(const char *key)
Expand All @@ -1015,8 +1015,8 @@ int dsdh_GetOriginalSpriteIndex(const char *key)
{
int limit;

for (int i = 0; deh_spritenames[i]; i++)
if (!strncasecmp(deh_spritenames[i], key, 4))
for (int i = 0; i < array_capacity(deh_spritenames); i++)
if (deh_spritenames[i] && !strncasecmp(deh_spritenames[i], key, 4))
return i;

// is it a number?
Expand Down
Loading

0 comments on commit b6987c2

Please sign in to comment.