- restructured the main loop code so that the actual loop is in the common code.

This commit is contained in:
Christoph Oelckers 2020-08-23 17:47:05 +02:00
parent 85875da77a
commit adb98a47ba
12 changed files with 231 additions and 286 deletions

View file

@ -68,8 +68,6 @@ void LocalKeys(void);
void InitCheats(); void InitCheats();
bool bNoDemo = false; bool bNoDemo = false;
bool bQuickStart = true;
char gUserMapFilename[BMAX_PATH]; char gUserMapFilename[BMAX_PATH];
@ -714,13 +712,12 @@ static const char* actions[] = {
"Toggle_Crouch", "Toggle_Crouch",
}; };
static void app_init() void GameInterface::app_init()
{ {
InitCheats(); InitCheats();
buttonMap.SetButtons(actions, NUM_ACTIONS); buttonMap.SetButtons(actions, NUM_ACTIONS);
memcpy(&gGameOptions, &gSingleGameOptions, sizeof(GAMEOPTIONS)); memcpy(&gGameOptions, &gSingleGameOptions, sizeof(GAMEOPTIONS));
gGameOptions.nMonsterSettings = !userConfig.nomonsters; gGameOptions.nMonsterSettings = !userConfig.nomonsters;
bQuickStart = userConfig.nologo;
ReadAllRFS(); ReadAllRFS();
HookReplaceFunctions(); HookReplaceFunctions();
@ -854,7 +851,7 @@ static void drawBackground()
netBroadcastMyLogoff(gQuitRequest == 2); netBroadcastMyLogoff(gQuitRequest == 2);
} }
static void commonTicker(bool &playvideo) static void commonTicker()
{ {
if (TestBitString(gotpic, 2342)) if (TestBitString(gotpic, 2342))
{ {
@ -895,30 +892,16 @@ static void commonTicker(bool &playvideo)
gQuitRequest = 0; gQuitRequest = 0;
gRestartGame = 0; gRestartGame = 0;
if (gGameOptions.nGameType != 0)
{
playvideo = !bQuickStart;
}
else playvideo = false;
// Don't switch to startup if we're already outside the game. // Don't switch to startup if we're already outside the game.
if (gamestate == GS_LEVEL) gamestate = GS_STARTUP; if (gamestate == GS_LEVEL) gamestate = GS_STARTUP;
} }
} }
int GameInterface::app_main() void GameInterface::RunGameFrame()
{
app_init();
gamestate = GS_STARTUP;
bool playvideo = !bQuickStart;
while (true)
{
try
{ {
if (gamestate == GS_STARTUP) gameInit(); if (gamestate == GS_STARTUP) gameInit();
commonTicker(playvideo); commonTicker();
netGetPackets(); netGetPackets();
handleevents(); handleevents();
updatePauseStatus(); updatePauseStatus();
@ -934,7 +917,7 @@ int GameInterface::app_main()
} }
else else
{ {
if (playvideo) playlogos(); if (!userConfig.nologo && gGameOptions.nGameType == 0) playlogos();
else else
{ {
gamestate = GS_MENUSCREEN; gamestate = GS_MENUSCREEN;
@ -964,16 +947,6 @@ int GameInterface::app_main()
gEndGameMgr.Draw(); gEndGameMgr.Draw();
break; break;
} }
videoNextPage();
}
catch (CRecoverableError& err)
{
C_FullConsole();
Printf(TEXTCOLOR_RED "%s\n", err.what());
}
}
return 0;
} }
bool DemoRecordStatus(void) { bool DemoRecordStatus(void) {

View file

@ -107,7 +107,8 @@ void sndPlaySpecialMusicOrNothing(int nMusic);
struct GameInterface : ::GameInterface struct GameInterface : ::GameInterface
{ {
const char* Name() override { return "Blood"; } const char* Name() override { return "Blood"; }
int app_main() override; void app_init() override;
void RunGameFrame() override;
bool GenerateSavePic() override; bool GenerateSavePic() override;
void FreeGameData() override; void FreeGameData() override;
FString statFPS() override; FString statFPS() override;

View file

@ -117,6 +117,7 @@ void I_SetWindowTitle(const char* caption);
void S_ParseSndInfo(); void S_ParseSndInfo();
void I_DetectOS(void); void I_DetectOS(void);
void LoadScripts(); void LoadScripts();
void app_loop();
bool AppActive; bool AppActive;
@ -817,6 +818,7 @@ int RunGame()
{ {
playername = userConfig.CommandName; playername = userConfig.CommandName;
} }
GameTicRate = 30;
CheckUserMap(); CheckUserMap();
GPalette.Init(MAXPALOOKUPS + 2); // one slot for each translation, plus a separate one for the base palettes and the internal one GPalette.Init(MAXPALOOKUPS + 2); // one slot for each translation, plus a separate one for the base palettes and the internal one
TexMan.Init([]() {}, [](BuildInfo &) {}); TexMan.Init([]() {}, [](BuildInfo &) {});
@ -836,16 +838,44 @@ int RunGame()
if (enginePreInit()) if (enginePreInit())
{ {
I_FatalError("app_main: There was a problem initializing the Build engine: %s\n", engineerrstr); I_FatalError("There was a problem initializing the Build engine: %s\n", engineerrstr);
} }
auto exec = C_ParseCmdLineParams(nullptr); auto exec = C_ParseCmdLineParams(nullptr);
if (exec) exec->ExecCommands(); if (exec) exec->ExecCommands();
gamestate = GS_LEVEL; gamestate = GS_LEVEL;
return gi->app_main(); gi->app_init();
app_loop();
} }
//---------------------------------------------------------------------------
//
// The one and only main loop in the entire engine. Yay!
//
//---------------------------------------------------------------------------
void app_loop()
{
gamestate = GS_STARTUP;
while (true)
{
try
{
gi->RunGameFrame();
videoNextPage();
videoSetBrightness(0); // immediately reset this so that the value doesn't stick around in the backend.
}
catch (CRecoverableError& err)
{
C_FullConsole();
Printf(TEXTCOLOR_RED "%s\n", err.what());
}
}
}
//========================================================================== //==========================================================================
// //
// //

View file

@ -58,7 +58,8 @@ struct GameInterface
virtual const char* Name() { return "$"; } virtual const char* Name() { return "$"; }
virtual ~GameInterface() {} virtual ~GameInterface() {}
virtual bool GenerateSavePic() { return false; } virtual bool GenerateSavePic() { return false; }
virtual int app_main() = 0; virtual void app_init() = 0;
virtual void RunGameFrame() = 0;
virtual void clearlocalinputstate() {} virtual void clearlocalinputstate() {}
virtual void UpdateScreenSize() {} virtual void UpdateScreenSize() {}
virtual void FreeGameData() {} virtual void FreeGameData() {}

View file

@ -621,7 +621,7 @@ static const char* actions[] =
void InitGame() void GameInterface::app_init()
{ {
int i; int i;
//int esi = 1; //int esi = 1;

View file

@ -134,7 +134,6 @@ void CheckKeys();
void CheckKeys2(); void CheckKeys2();
void GameTicker(); void GameTicker();
void InitLevel(int); void InitLevel(int);
void InitGame();
void InitNewGame(); void InitNewGame();
void startmainmenu(); void startmainmenu();
@ -294,7 +293,8 @@ struct SavegameHelper
struct GameInterface : ::GameInterface struct GameInterface : ::GameInterface
{ {
const char* Name() override { return "Exhumed"; } const char* Name() override { return "Exhumed"; }
int app_main() override; void app_init() override;
void RunGameFrame() override;
bool GenerateSavePic() override; bool GenerateSavePic() override;
void DrawNativeMenuText(int fontnum, int state, double xpos, double ypos, float fontscale, const char* text, int flags) override; void DrawNativeMenuText(int fontnum, int state, double xpos, double ypos, float fontscale, const char* text, int flags) override;
void MenuOpened() override; void MenuOpened() override;

View file

@ -59,7 +59,6 @@ extern ClockTicks tclocks;
void RunCinemaScene(int num); void RunCinemaScene(int num);
void GameMove(void); void GameMove(void);
void InitGame();
void DrawClock(); void DrawClock();
int32_t calc_smoothratio(ClockTicks totalclk, ClockTicks ototalclk); int32_t calc_smoothratio(ClockTicks totalclk, ClockTicks ototalclk);
void DoTitle(CompletionFunc completion); void DoTitle(CompletionFunc completion);
@ -262,19 +261,10 @@ void GameLoop()
fps++; fps++;
} }
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
int GameInterface::app_main() void GameInterface::RunGameFrame()
{
InitGame();
gamestate = GS_STARTUP;
while (true)
{ {
again:
try try
{ {
HandleAsync(); HandleAsync();
@ -295,7 +285,7 @@ int GameInterface::app_main()
auto map = FindMapByName(userConfig.CommandMap); auto map = FindMapByName(userConfig.CommandMap);
if (map) GameAction = map->levelNumber; if (map) GameAction = map->levelNumber;
userConfig.CommandMap = ""; userConfig.CommandMap = "";
continue; goto again;
} }
else else
{ {
@ -319,21 +309,15 @@ int GameInterface::app_main()
break; break;
} }
videoNextPage();
} }
catch (CRecoverableError& err) catch (CRecoverableError&)
{ {
// Clear all progression sensitive variables here. // Clear all progression sensitive variables here.
GameAction = -1; GameAction = -1;
EndLevel = false; EndLevel = false;
C_FullConsole(); throw;
Printf(TEXTCOLOR_RED "%s\n", err.what());
}
}
} }
}
END_PS_NS END_PS_NS

View file

@ -34,7 +34,8 @@ extern FFont* DigiFont;
struct GameInterface : public ::GameInterface struct GameInterface : public ::GameInterface
{ {
const char* Name() override { return "Duke"; } const char* Name() override { return "Duke"; }
int app_main() override; void app_init() override;
void RunGameFrame() override;
void clearlocalinputstate() override; void clearlocalinputstate() override;
bool GenerateSavePic() override; bool GenerateSavePic() override;
void PlayHudSound() override; void PlayHudSound() override;

View file

@ -423,14 +423,12 @@ static void Startup(void)
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void app_loop(); void GameInterface::app_init()
int GameInterface::app_main()
{ {
Startup(); Startup();
enginePostInit(); enginePostInit();
videoInit(); videoInit();
app_loop(); enginecompatibility_mode = ENGINECOMPATIBILITY_19961112;//bVanilla;
return 0;
} }

View file

@ -399,14 +399,7 @@ void startmainmenu()
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void app_loop() void GameInterface::RunGameFrame()
{
gamestate = GS_STARTUP;
enginecompatibility_mode = ENGINECOMPATIBILITY_19961112;//bVanilla;
while (true)
{
try
{ {
handleevents(); handleevents();
updatePauseStatus(); updatePauseStatus();
@ -459,17 +452,7 @@ void app_loop()
break; break;
} }
videoNextPage();
videoSetBrightness(0); // immediately reset this so that the value doesn't stick around in the backend.
} }
catch (CRecoverableError& err)
{
C_FullConsole();
Printf(TEXTCOLOR_RED "%s\n", err.what());
}
}
}
END_DUKE_NS END_DUKE_NS

View file

@ -213,8 +213,9 @@ static const char* actions[] = {
}; };
bool InitGame() void GameInterface::app_init()
{ {
GameTicRate = 40;
InitCheats(); InitCheats();
buttonMap.SetButtons(actions, NUM_ACTIONS); buttonMap.SetButtons(actions, NUM_ACTIONS);
automapping = 1; automapping = 1;
@ -284,7 +285,6 @@ bool InitGame()
enginePostInit(); enginePostInit();
videoInit(); videoInit();
InitFX(); InitFX();
return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -804,19 +804,8 @@ void GameTicker(void)
} }
} }
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
int32_t GameInterface::app_main() void GameInterface::RunGameFrame()
{
InitGame();
gamestate = GS_STARTUP;
while (true)
{ {
try try
{ {
@ -859,34 +848,18 @@ int32_t GameInterface::app_main()
break; break;
} }
videoNextPage();
} }
catch (CRecoverableError& err) catch (CRecoverableError&)
{ {
// Make sure we do not leave the game in an unstable state
TerminateLevel(); TerminateLevel();
NextLevel = nullptr; NextLevel = nullptr;
SavegameLoaded = false; SavegameLoaded = false;
ExitLevel = false; ExitLevel = false;
FinishAnim = 0; FinishAnim = 0;
C_FullConsole(); throw;
Printf(TEXTCOLOR_RED "%s\n", err.what());
}
} }
#if 0
while (true)
{
handleevents();
C_RunDelayedCommands();
NewLevel();
}
//SybexScreen();
throw CExitEvent(0);
#endif
return 0;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View file

@ -2364,7 +2364,8 @@ extern short Bunny_Count;
struct GameInterface : ::GameInterface struct GameInterface : ::GameInterface
{ {
const char* Name() override { return "ShadowWarrior"; } const char* Name() override { return "ShadowWarrior"; }
int app_main() override; void app_init() override;
void RunGameFrame() override;
void FreeGameData() override; void FreeGameData() override;
bool GenerateSavePic() override; bool GenerateSavePic() override;
void DrawNativeMenuText(int fontnum, int state, double xpos, double ypos, float fontscale, const char* text, int flags) override; void DrawNativeMenuText(int fontnum, int state, double xpos, double ypos, float fontscale, const char* text, int flags) override;