From 28965eefd5ee8e03d7a0d1d4689c944295507a62 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 18 Jul 2020 11:56:49 +0200 Subject: [PATCH] - moved the main loop to gameloop.cpp. --- source/games/duke/src/funct.h | 1 + source/games/duke/src/game_misc.cpp | 6 ++ source/games/duke/src/gameloop.cpp | 148 +++++++++++++++++++++++++++- source/games/duke/src/zz_game.cpp | 137 ------------------------- 4 files changed, 151 insertions(+), 141 deletions(-) diff --git a/source/games/duke/src/funct.h b/source/games/duke/src/funct.h index b819d71b7..5802b1979 100644 --- a/source/games/duke/src/funct.h +++ b/source/games/duke/src/funct.h @@ -234,5 +234,6 @@ void apply_seasick(player_struct* p, double scalefactor); void calcviewpitch(player_struct* p, double factor); void sethorizon(int snum, int sb_snum, double factor, bool frominput = false); bool movementBlocked(int snum); +void GetInput(); END_DUKE_NS diff --git a/source/games/duke/src/game_misc.cpp b/source/games/duke/src/game_misc.cpp index 33590becb..b5c5d2ad4 100644 --- a/source/games/duke/src/game_misc.cpp +++ b/source/games/duke/src/game_misc.cpp @@ -741,6 +741,12 @@ bool GameInterface::automapActive() return ud.overhead_on != 0; } +::GameInterface* CreateInterface() +{ + return new GameInterface; +} + + END_DUKE_NS diff --git a/source/games/duke/src/gameloop.cpp b/source/games/duke/src/gameloop.cpp index 8c6f62b4d..82338d37a 100644 --- a/source/games/duke/src/gameloop.cpp +++ b/source/games/duke/src/gameloop.cpp @@ -67,12 +67,12 @@ static inline void GetNextInput() movefifoplc++; } -void advancequeue(int myconnectindex) +static void advancequeue(int myconnectindex) { movefifoend[myconnectindex]++; } -input_t& nextinput(int myconnectindex) +static input_t& nextinput(int myconnectindex) { return inputfifo[movefifoend[myconnectindex] & (MOVEFIFOSIZ - 1)][myconnectindex]; } @@ -115,7 +115,7 @@ void prediction() // //--------------------------------------------------------------------------- -int menuloop(void) +static int menuloop(void) { FX_StopAllSounds(); while (menuactive != MENU_Off) @@ -297,7 +297,6 @@ int domovethings() // //--------------------------------------------------------------------------- - int moveloop() { prediction(); @@ -310,6 +309,147 @@ int moveloop() return 0; } +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +bool GameTicker() +{ + handleevents(); + if (ps[myconnectindex].gm == MODE_DEMO) + { + M_ClearMenus(); + return true; + } + + //Net_GetPackets(); + + nonsharedkeys(); + + C_RunDelayedCommands(); + + char gameUpdate = false; + gameupdatetime.Reset(); + gameupdatetime.Clock(); + + while ((!(ps[myconnectindex].gm & (MODE_MENU | MODE_DEMO))) && (int)(totalclock - ototalclock) >= TICSPERFRAME) + { + ototalclock += TICSPERFRAME; + + GetInput(); + // this is where we fill the input_t struct that is actually processed by P_ProcessInput() + auto const pPlayer = &ps[myconnectindex]; + auto const q16ang = fix16_to_int(pPlayer->q16ang); + auto& input = nextinput(myconnectindex); + + input = loc; + input.fvel = mulscale9(loc.fvel, sintable[(q16ang + 2560) & 2047]) + + mulscale9(loc.svel, sintable[(q16ang + 2048) & 2047]) + + pPlayer->fric.x; + input.svel = mulscale9(loc.fvel, sintable[(q16ang + 2048) & 2047]) + + mulscale9(loc.svel, sintable[(q16ang + 1536) & 2047]) + + pPlayer->fric.y; + loc = {}; + + advancequeue(myconnectindex); + + if (((!System_WantGuiCapture() && (ps[myconnectindex].gm & MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (ud.multimode > 1)) && + (ps[myconnectindex].gm & MODE_GAME)) + { + moveloop(); + } + } + + gameUpdate = true; + gameupdatetime.Unclock(); + + if (ps[myconnectindex].gm & (MODE_EOL | MODE_RESTART)) + { + switch (exitlevel()) + { + case 1: return false; + case 2: return true; + } + } + + + GetInput(); + + int const smoothRatio = calc_smoothratio(totalclock, ototalclock); + + drawtime.Reset(); + drawtime.Clock(); + displayrooms(screenpeek, smoothRatio); + displayrest(smoothRatio); + drawtime.Unclock(); + + return (ps[myconnectindex].gm & MODE_DEMO); +} + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +void app_loop() +{ + + while (true) + { + totalclock = 0; + ototalclock = 0; + lockclock = 0; + + ps[myconnectindex].ftq = 0; + + //if (ud.warp_on == 0) + { +#if 0 // fixme once the game loop has been done. + if ((ud.multimode > 1) && startupMap.IsNotEmpty()) + { + auto maprecord = FindMap(startupMap); + ud.m_respawn_monsters = ud.m_player_skill == 4; + + for (int i = 0; i != -1; i = connectpoint2[i]) + { + resetweapons(i); + resetinventory(i); + } + + StartGame(maprecord); + } + else +#endif + { + fi.ShowLogo([](bool) {}); + } + + M_StartControlPanel(false); + M_SetMenu(NAME_Mainmenu); + if (menuloop()) + { + FX_StopAllSounds(); + continue; + } + } + + ud.showweapons = cl_showweapon; + setlocalplayerinput(&ps[myconnectindex]); + PlayerColorChanged(); + inputState.ClearAllInput(); + + bool res; + do + { + res = GameTicker(); + videoNextPage(); + } while (!res); + } +} + END_DUKE_NS diff --git a/source/games/duke/src/zz_game.cpp b/source/games/duke/src/zz_game.cpp index a04a37eed..f5f3a4cb1 100644 --- a/source/games/duke/src/zz_game.cpp +++ b/source/games/duke/src/zz_game.cpp @@ -46,12 +46,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. BEGIN_DUKE_NS -int32_t moveloop(void); -int menuloop(void); -void advancequeue(int myconnectindex); -input_t& nextinput(int myconnectindex); -void GetInput(); - int16_t max_ammo_amount[MAX_WEAPONS]; int32_t spriteqamount = 64; @@ -72,136 +66,5 @@ int32_t PHEIGHT = PHEIGHT_DUKE; int32_t lastvisinc; -void app_loop() -{ - -MAIN_LOOP_RESTART: - totalclock = 0; - ototalclock = 0; - lockclock = 0; - - ps[myconnectindex].ftq = 0; - - //if (ud.warp_on == 0) - { -#if 0 // fixme once the game loop has been done. - if ((ud.multimode > 1) && startupMap.IsNotEmpty()) - { - auto maprecord = FindMap(startupMap); - ud.m_respawn_monsters = ud.m_player_skill == 4; - - for (int i = 0; i != -1; i = connectpoint2[i]) - { - resetweapons(i); - resetinventory(i); - } - - StartGame(maprecord); - } - else -#endif - { - fi.ShowLogo([](bool) {}); - } - - M_StartControlPanel(false); - M_SetMenu(NAME_Mainmenu); - if (menuloop()) - { - FX_StopAllSounds(); - goto MAIN_LOOP_RESTART; - } - } - - ud.showweapons = cl_showweapon; - setlocalplayerinput(&ps[myconnectindex]); - PlayerColorChanged(); - inputState.ClearAllInput(); - - do //main loop - { - handleevents(); - if (ps[myconnectindex].gm == MODE_DEMO) - { - M_ClearMenus(); - goto MAIN_LOOP_RESTART; - } - - //Net_GetPackets(); - - nonsharedkeys(); - - C_RunDelayedCommands(); - - char gameUpdate = false; - gameupdatetime.Reset(); - gameupdatetime.Clock(); - - while ((!(ps[myconnectindex].gm & (MODE_MENU|MODE_DEMO))) && (int)(totalclock - ototalclock) >= TICSPERFRAME) - { - ototalclock += TICSPERFRAME; - - GetInput(); - // this is where we fill the input_t struct that is actually processed by P_ProcessInput() - auto const pPlayer = &ps[myconnectindex]; - auto const q16ang = fix16_to_int(pPlayer->q16ang); - auto& input = nextinput(myconnectindex); - - input = loc; - input.fvel = mulscale9(loc.fvel, sintable[(q16ang + 2560) & 2047]) + - mulscale9(loc.svel, sintable[(q16ang + 2048) & 2047]) + - pPlayer->fric.x; - input.svel = mulscale9(loc.fvel, sintable[(q16ang + 2048) & 2047]) + - mulscale9(loc.svel, sintable[(q16ang + 1536) & 2047]) + - pPlayer->fric.y; - loc = {}; - - advancequeue(myconnectindex); - - if (((!System_WantGuiCapture() && (ps[myconnectindex].gm&MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (ud.multimode > 1)) && - (ps[myconnectindex].gm&MODE_GAME)) - { - moveloop(); - } - } - - gameUpdate = true; - gameupdatetime.Unclock(); - - if (ps[myconnectindex].gm & (MODE_EOL|MODE_RESTART)) - { - switch (exitlevel()) - { - case 1: continue; - case 2: goto MAIN_LOOP_RESTART; - } - } - - - if (G_FPSLimit()) - { - GetInput(); - - int const smoothRatio = calc_smoothratio(totalclock, ototalclock); - - drawtime.Reset(); - drawtime.Clock(); - displayrooms(screenpeek, smoothRatio); - displayrest(smoothRatio); - drawtime.Unclock(); - videoNextPage(); - } - - if (ps[myconnectindex].gm&MODE_DEMO) - goto MAIN_LOOP_RESTART; - } - while (1); -} - -::GameInterface* CreateInterface() -{ - return new GameInterface; -} - END_DUKE_NS