Skip to content

Commit

Permalink
cleaner demoloop internals
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiangreffrath authored and elf-alchemist committed Jan 14, 2025
1 parent da68481 commit ae0063e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
37 changes: 18 additions & 19 deletions src/d_demoloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ static demoloop_entry_t demoloop_commercial[] = {
{ "DEMO4", "", 0, TYPE_DEMO, WIPE_MELT },
};

// For local parsing purposes only.
static demoloop_entry_t current_entry;

demoloop_t demoloop;
demoloop_t demoloop = NULL;
int demoloop_count = 0;

void D_ParseDemoLoopEntry(json_t *entry)
Expand All @@ -90,16 +87,26 @@ void D_ParseDemoLoopEntry(json_t *entry)
outro_wipe = WIPE_MELT;
}

demoloop_entry_t current_entry = {0};

// Remove pointer reference to in-memory JSON data.
M_CopyLumpName(current_entry.primary_lump, primary_buffer);
M_CopyLumpName(current_entry.secondary_lump, secondary_buffer);
// Providing the time in seconds is much more intuitive for the end users.
current_entry.duration = seconds * TICRATE;
current_entry.type = type;
current_entry.outro_wipe = outro_wipe;

// Should there be a malformed entry, discard it.
if (current_entry.type == TYPE_NONE)
{
return;
}

array_push(demoloop, current_entry);
}

void D_ParseDemoLoop(void)
static void D_ParseDemoLoop(void)
{
// Does the JSON lump even exist?
json_t *json = JS_Open("DEMOLOOP", "demoloop", (version_t){1, 0, 0});
Expand Down Expand Up @@ -128,27 +135,17 @@ void D_ParseDemoLoop(void)

// If so, now parse them.
json_t *entry;

JS_ArrayForEach(entry, entry_list)
{
D_ParseDemoLoopEntry(entry);

// Should there be a malformed entry, discard it.
if (current_entry.type == TYPE_NONE)
{
continue;
}

array_push(demoloop, current_entry);
demoloop_count++;
}
demoloop_count = array_size(demoloop);

// No need to keep in memory
JS_Close("DEMOLOOP");
return;
}

void D_GetDefaultDemoLoop(GameMission_t mission, GameMode_t mode)
static void D_GetDefaultDemoLoop(GameMode_t mode)
{
switch(mode)
{
Expand All @@ -172,17 +169,19 @@ void D_GetDefaultDemoLoop(GameMission_t mission, GameMode_t mode)
default:
// How did we get here?
demoloop = NULL;
demoloop_count = 0;
break;
}

return;
}

void D_SetupDemoLoop(void) {
void D_SetupDemoLoop(void)
{
D_ParseDemoLoop();

if (demoloop == NULL)
{
D_GetDefaultDemoLoop(gamemission, gamemode);
D_GetDefaultDemoLoop(gamemode);
}
}
6 changes: 0 additions & 6 deletions src/d_demoloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// graphic, music or DEMO lump.
//

#include "doomdef.h"

#ifndef _D_DEMOLOOP_
#define _D_DEMOLOOP_

Expand Down Expand Up @@ -56,10 +54,6 @@ extern demoloop_t demoloop;
// Formerly 7 for Ultimate Doom & Final Doom, 6 otherwise.
extern int demoloop_count;

// Parse the DEMOLOOP, returning NULL to "demoloop" should any errors occur.
void D_ParseDemoLoop(void);
// If "demoloop" is NULL, check for defaults using mission and mode.
void D_GetDefaultDemoLoop(GameMission_t mission, GameMode_t mode);
// Perform both "D_ParseDemoLoop" and "D_GetDefaultDemoLoop".
void D_SetupDemoLoop(void);

Expand Down
26 changes: 13 additions & 13 deletions src/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ void D_Display (void)
static int demosequence; // killough 5/2/98: made static
static int pagetic;
static const char *pagename;
static demoloop_entry_t demoloop_point;
static demoloop_t demoloop_point;

//
// D_PageTicker
Expand Down Expand Up @@ -477,7 +477,7 @@ void D_AdvanceDemo(void)
void D_AdvanceDemoLoop(void)
{
demosequence = (demosequence + 1) % demoloop_count;
demoloop_point = demoloop[demosequence];
demoloop_point = &demoloop[demosequence];
}

void D_DoAdvanceDemo(void)
Expand All @@ -489,23 +489,23 @@ void D_DoAdvanceDemo(void)
gameaction = ga_nothing;

D_AdvanceDemoLoop();
switch (demoloop_point.type)
switch (demoloop_point->type)
{
case TYPE_ART:
gamestate = GS_DEMOSCREEN;

// Needed to support the Doom 3: BFG Edition variant
if (W_CheckNumForName(demoloop_point.primary_lump) < 0
&& !strcasecmp(demoloop_point.primary_lump, "TITLEPIC"))
if (W_CheckNumForName(demoloop_point->primary_lump) < 0
&& !strcasecmp(demoloop_point->primary_lump, "TITLEPIC"))
{
M_CopyLumpName(demoloop_point.primary_lump, "DMENUPIC");
M_CopyLumpName(demoloop_point->primary_lump, "DMENUPIC");
}

if (W_CheckNumForName(demoloop_point.primary_lump) >= 0)
if (W_CheckNumForName(demoloop_point->primary_lump) >= 0)
{
pagename = demoloop_point.primary_lump;
pagetic = demoloop_point.duration;
int music = W_CheckNumForName(demoloop_point.secondary_lump);
pagename = demoloop_point->primary_lump;
pagetic = demoloop_point->duration;
int music = W_CheckNumForName(demoloop_point->secondary_lump);
if (music >= 0)
{
S_ChangeMusInfoMusic(music, false);
Expand All @@ -517,10 +517,10 @@ void D_DoAdvanceDemo(void)
case TYPE_DEMO:
gamestate = GS_DEMOSCREEN;

if (W_CheckNumForName(demoloop_point.primary_lump) >= 0)
if (W_CheckNumForName(demoloop_point->primary_lump) >= 0)
{
G_DeferedPlayDemo(demoloop_point.primary_lump);
break;
G_DeferedPlayDemo(demoloop_point->primary_lump);
break;
}
// fallthrough

Expand Down

0 comments on commit ae0063e

Please sign in to comment.