Skip to content

Commit

Permalink
WAD API that doesn't suck
Browse files Browse the repository at this point in the history
  • Loading branch information
viciious committed Oct 19, 2023
1 parent fd5263b commit 02165d2
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 166 deletions.
16 changes: 5 additions & 11 deletions d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,13 @@ void START_Title(void)
l = gameinfo.titlePage;

if (*l) {
W_Push();
W_LoadPWAD();

int lump = W_CheckNumForName(l);
if (lump >= 0)
titlepic = W_CacheLumpNum(lump, PU_STATIC);

W_Pop();
W_UnloadPWAD();
}

#ifdef MARS
Expand Down Expand Up @@ -613,17 +612,15 @@ static void START_Credits (void)
{
#ifdef MARS
int i;
int lumps[2];
VINT lumps[2];
lumpinfo_t li[2];
wadinfo_t pwad;

credits[0] = credits[1] = NULL;
titlepic = NULL;
creditspage = 1;
if (!*gameinfo.creditsPage)
return;

W_Push();
W_LoadPWAD();

/* build a temp in-memory PWAD */
Expand All @@ -637,13 +634,12 @@ static void START_Credits (void)
lumps[i] = W_CheckNumForName(name);
}

pwad.numlumps = W_GetLumpInfoSubset(li, W_GetLumpInfo(), i, lumps);
W_SetPWAD(&pwad, li);
W_CacheWADLumps(li, i, lumps, true);

for (i = 0; i < 2; i++)
credits[i] = W_CacheLumpName(li[i].name, PU_STATIC);

W_Pop();
W_UnloadPWAD();

if (!credits[0])
return;
Expand Down Expand Up @@ -866,8 +862,6 @@ static void RunAttractDemos (void)
// Z_Malloc failure
Z_FreeTags(mainzone);

W_Push();

W_LoadPWAD();

demo = NULL;
Expand All @@ -877,7 +871,7 @@ static void RunAttractDemos (void)
if (l >= 0)
demo = W_CacheLumpNum(l, PU_STATIC);

W_Pop();
W_UnloadPWAD();

if (!first)
I_RestoreScreenCopy();
Expand Down
4 changes: 2 additions & 2 deletions d_mapinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ int G_BuiltinMapNumForMapName(const char* map)
return (map[3] - '0') * 10 + (map[4] - '0');
return 0;
}
extern void I_Debug(void);

static char* G_LoadMapinfoLump(void)
{
int len;
int lump;
char *buf;

buf = (char*)I_WorkBuffer();
I_Debug();

lump = W_CheckNumForName("DMAPINFO");
if (lump < 0) {
return NULL;
Expand Down
9 changes: 3 additions & 6 deletions doomdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,16 +684,13 @@ typedef struct

void W_Init (void);

int W_Push (void);
int W_Pop (void);
void W_LoadPWAD(void);
void W_SetPWAD (wadinfo_t *wad, void *lumpinfo);
lumpinfo_t *W_GetLumpInfo (void);
int W_GetLumpInfoSubset(lumpinfo_t *out, const lumpinfo_t *in, int numlumps, int *lumps);
void W_UnloadPWAD (void);
int W_CacheWADLumps (lumpinfo_t *li, int numlumps, VINT *lumps, boolean setpwad);

int W_CheckNumForName (const char *name);
int W_GetNumForName (const char *name);
int W_CheckNumForNameExt (const char *name, int start, int end);
int W_CheckRangeForName (const char *name, int start, int end);

int W_LumpLength (int lump);
int W_ReadLump (int lump, void *dest);
Expand Down
5 changes: 2 additions & 3 deletions g_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ void G_Init(void)

// copy mapnumbers to a temp buffer, then free, then allocate again
// to avoid zone memory fragmentation
W_Push();
W_LoadPWAD();

maplist = G_LoadMaplist(&mapcount, &gameinfo);
Expand Down Expand Up @@ -451,9 +450,9 @@ void G_Init(void)
gamemapcount = mapcount;
}

G_InitPlayerResp();
W_UnloadPWAD();

W_Pop();
G_InitPlayerResp();

if (!*gameinfo.borderFlat)
gameinfo.borderFlat = "ROCKS";
Expand Down
9 changes: 3 additions & 6 deletions in_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,8 @@ void IN_SingleDrawer(void)
void IN_Start (void)
{
int i,l;
int lumps[7];
VINT lumps[7];
lumpinfo_t li[7];
wadinfo_t pwad;

interm = Z_Malloc(sizeof(*interm), PU_STATIC);
D_memset(interm, 0, sizeof(*interm));
Expand Down Expand Up @@ -367,7 +366,6 @@ void IN_Start (void)
#ifdef MARS
Z_FreeTags (mainzone);

W_Push();
W_LoadPWAD();

/* build a temp in-memory PWAD */
Expand All @@ -378,9 +376,8 @@ void IN_Start (void)
lumps[4] = W_CheckNumForName("I_KILLS");
lumps[5] = W_CheckNumForName("I_ITEMS");
lumps[6] = W_CheckNumForName("I_FINISH");
pwad.numlumps = W_GetLumpInfoSubset(li, W_GetLumpInfo(), 7, lumps);

W_SetPWAD(&pwad, li);
W_CacheWADLumps(li, 7, lumps, true);

l = W_CheckNumForName("INTERPIC");
if (l != -1)
Expand All @@ -404,7 +401,7 @@ void IN_Start (void)
#endif

#ifdef MARS
W_Pop();
W_UnloadPWAD();
#endif

snums = W_CheckNumForName("NUM_0");
Expand Down
3 changes: 1 addition & 2 deletions m_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ void M_Start2 (boolean startup_)
startup = startup_;
if (startup)
{
W_Push();
W_LoadPWAD();

i = W_CheckNumForName("M_DOOM");
m_doom = i != -1 ? W_CacheLumpNum(i, PU_STATIC) : NULL;

W_Pop();
W_UnloadPWAD();
}
else
{
Expand Down
43 changes: 21 additions & 22 deletions marssound.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ void S_Init(void)
int i;
int initmusictype;
int start, end;
int lumps[NUMSFX > 99 ? NUMSFX : 99];
int sfxol[NUMSFX*2];
VINT lumps[NUMSFX > 99 ? NUMSFX : 99];

for (i = 0; i < SFXCHANNELS; i++)
sfxchannels[i].data = NULL;
Expand All @@ -149,7 +148,6 @@ void S_Init(void)
muslooping = 0;
S_StopSong();

W_Push();
W_LoadPWAD();

/* build an in-memory PWAD with all music */
Expand All @@ -163,7 +161,7 @@ void S_Init(void)
for (i = 0; i < num_music; i++)
lumps[i] = start + i + 1;
vgm_tracks = Z_Malloc(sizeof(*vgm_tracks) * num_music, PU_STATIC);
W_GetLumpInfoSubset(vgm_tracks, W_GetLumpInfo(), num_music, lumps);
W_CacheWADLumps(vgm_tracks, num_music, lumps, false);
}

/* build an in-memory PWAD with all SFX */
Expand All @@ -177,25 +175,35 @@ void S_Init(void)

for (i=1 ; i < NUMSFX ; i++)
{
S_sfx[i].lump = W_CheckNumForNameExt(S_sfxnames[i], start, end) - start;
S_sfx[i].lump = W_CheckRangeForName(S_sfxnames[i], start, end);
}

if (mcd_avail)
{
lumpinfo_t *li = W_GetLumpInfo();
lumpinfo_t li[NUMSFX];
int sfxol[NUMSFX*2];

for (i = 0; i < numsfx; i++)
{
if (S_sfx[i].lump >= 0)
S_sfx[i].lump -= start;
lumps[i] = start + 1 + i;
}

W_CacheWADLumps(li, numsfx, lumps, false);

/* load all SFX in a single batch */
for (i = 0; i < numsfx; i++)
{
sfxol[i*2] = li[start + 1 + i].filepos;
sfxol[i*2+1] = li[start + 1 + i].size;
sfxol[i*2] = li[i].filepos;
sfxol[i*2+1] = li[i].size;
}

Mars_MCDLoadSfxFileOfs(1, numsfx, PWAD_NAME, sfxol);
}
}

W_Pop();
W_UnloadPWAD();

Mars_SetPriCmdCallback(&S_Pri_CmdHandler);

Expand Down Expand Up @@ -619,20 +627,11 @@ int S_CDAvailable(void)

int S_SongForMapnum(int mapnum)
{
int i;
VINT songs[100];
VINT numsongs;

numsongs = 0;
for (i = 0; i < num_music + cdtrack_lastmap; i++) {
songs[numsongs++] = i + 1;
if (numsongs == sizeof(songs) / sizeof(songs[0]))
break;
}

if (numsongs == 0)
int numsongs;
numsongs = num_music + cdtrack_lastmap;
if (numsongs <= 0)
return mus_none;
return songs[(mapnum - 1) % numsongs];
return (mapnum - 1) % numsongs + 1;
}

int S_SongForName(const char *str)
Expand Down
43 changes: 18 additions & 25 deletions p_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ void P_LoadSectors (int lump)
D_memset (sectors, 0, numsectors*sizeof(sector_t));
data = W_GetLumpData(lump);

// pop the WAD stack to point to the IWAD,
// otherwise R_FlatNumForName is going to break
W_Pop();

ms = (mapsector_t *)data;
ss = sectors;
for (i=0 ; i<numsectors ; i++, ss++, ms++)
Expand Down Expand Up @@ -202,8 +198,6 @@ void P_LoadSectors (int lump)

ss->tag = LITTLESHORT(ms->tag);
}

W_Push();
}


Expand Down Expand Up @@ -642,8 +636,7 @@ void P_SetupLevel (const char *lumpname, skill_t skill)
mobj_t *mobj;
#endif
extern int cy;
int lumpnum, lumps[ML_BLOCKMAP+1];
wadinfo_t pwad;
VINT lumpnum, lumps[ML_BLOCKMAP+1];
lumpinfo_t li[ML_BLOCKMAP+1];

M_ClearRandom ();
Expand All @@ -654,32 +647,32 @@ D_printf ("P_SetupLevel(%s,%i)\n",lumpname,skill);

P_InitThinkers ();

W_Push();
W_LoadPWAD();

lumpnum = W_GetNumForName(lumpname);
lumpnum = W_CheckNumForName(lumpname);
if (lumpnum < 0)
I_Error("Map %s not found!", lumpname);

/* build a temp in-memory PWAD */
for (i = 0; i < ML_BLOCKMAP+1; i++)
lumps[i] = lumpnum+i;
pwad.numlumps = W_GetLumpInfoSubset(li, W_GetLumpInfo(), ML_BLOCKMAP+1, lumps);

W_SetPWAD(&pwad, li);
W_CacheWADLumps(li, ML_BLOCKMAP+1, lumps, true);

lumpnum = W_GetNumForName(lumpname);

/* note: most of this ordering is important */
P_LoadBlockMap (ML_BLOCKMAP);
P_LoadVertexes (ML_VERTEXES);
P_LoadSectors (ML_SECTORS);
P_LoadSideDefs (ML_SIDEDEFS);
P_LoadLineDefs (ML_LINEDEFS);
P_LoadSubsectors (ML_SSECTORS);
P_LoadNodes (ML_NODES);
P_LoadSegs (ML_SEGS);

rejectmatrix = Z_Malloc (W_LumpLength (ML_REJECT),PU_LEVEL);
W_ReadLump (ML_REJECT,rejectmatrix);
P_LoadBlockMap (lumpnum+ML_BLOCKMAP);
P_LoadVertexes (lumpnum+ML_VERTEXES);
P_LoadSectors (lumpnum+ML_SECTORS);
P_LoadSideDefs (lumpnum+ML_SIDEDEFS);
P_LoadLineDefs (lumpnum+ML_LINEDEFS);
P_LoadSubsectors (lumpnum+ML_SSECTORS);
P_LoadNodes (lumpnum+ML_NODES);
P_LoadSegs (lumpnum+ML_SEGS);

rejectmatrix = Z_Malloc (W_LumpLength (lumpnum+ML_REJECT),PU_LEVEL);
W_ReadLump (lumpnum+ML_REJECT,rejectmatrix);

validcount = Z_Malloc((numlines + 1) * sizeof(*validcount) * 2, PU_LEVEL);
D_memset(validcount, 0, (numlines + 1) * sizeof(*validcount) * 2);
Expand All @@ -704,9 +697,9 @@ D_printf ("P_SetupLevel(%s,%i)\n",lumpname,skill);

bodyqueslot = 0;
deathmatch_p = deathmatchstarts;
P_LoadThings (ML_THINGS);
P_LoadThings (lumpnum+ML_THINGS);

W_Pop();
W_UnloadPWAD();

/* */
/* if deathmatch, randomly spawn the active players */
Expand Down
4 changes: 2 additions & 2 deletions r_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void R_InitTextures (void)
for (j = 0; j < MIPLEVELS; j++)
texture->data[j] = NULL; /* not cached yet */
if (start >= 0 && end > 0)
texture->lumpnum = W_CheckNumForNameExt(texture->name, start, end);
texture->lumpnum = W_CheckRangeForName(texture->name, start, end);
else
texture->lumpnum = W_CheckNumForName(texture->name);
if (texture->lumpnum == -1)
Expand Down Expand Up @@ -253,7 +253,7 @@ void R_InitData (void)

int R_FlatNumForName (const char *name)
{
int f = W_CheckNumForNameExt (name, firstflat, lastflat+1);
int f = W_CheckRangeForName (name, firstflat, lastflat+1);
if (f < 0)
return f;
return f - firstflat;
Expand Down
Loading

0 comments on commit 02165d2

Please sign in to comment.