From 9e8e441d0214740f3fba35f21277ad2e1cf54124 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Fri, 27 Mar 2020 22:17:01 +1100 Subject: [PATCH] Further improve Duke3D and RR main game loop. - Replace if statement containing nested do/while loop with a while loop. --- source/duke3d/src/game.cpp | 74 ++++++++++++++++++------------------- source/rr/src/game.cpp | 75 ++++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 78 deletions(-) diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index cd21b4cc1..cad63aede 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -5759,52 +5759,48 @@ MAIN_LOOP_RESTART: bool gameUpdate = false; double gameUpdateStartTime = timerGetHiTicks(); - if (((g_netClient || g_netServer) || (myplayer.gm & (MODE_MENU|MODE_DEMO)) == 0) && totalclock >= ototalclock+TICSPERFRAME) + while (((g_netClient || g_netServer) || (myplayer.gm & (MODE_MENU | MODE_DEMO)) == 0) && (int)(totalclock - ototalclock) >= TICSPERFRAME) { - do + ototalclock += TICSPERFRAME; + + P_GetInput(myconnectindex); + + // this is where we fill the input_t struct that is actually processed by P_ProcessInput() + auto const pPlayer = g_player[myconnectindex].ps; + auto const q16ang = fix16_to_int(pPlayer->q16ang); + auto & input = inputfifo[0][myconnectindex]; + + input = localInput; + input.fvel = mulscale9(localInput.fvel, sintable[(q16ang + 2560) & 2047]) + + mulscale9(localInput.svel, sintable[(q16ang + 2048) & 2047]); + input.svel = mulscale9(localInput.fvel, sintable[(q16ang + 2048) & 2047]) + + mulscale9(localInput.svel, sintable[(q16ang + 1536) & 2047]); + + if (!FURY) { - ototalclock += TICSPERFRAME; - - P_GetInput(myconnectindex); - - // this is where we fill the input_t struct that is actually processed by P_ProcessInput() - auto const pPlayer = g_player[myconnectindex].ps; - auto const q16ang = fix16_to_int(pPlayer->q16ang); - auto & input = inputfifo[0][myconnectindex]; - - input = localInput; - input.fvel = mulscale9(localInput.fvel, sintable[(q16ang + 2560) & 2047]) + - mulscale9(localInput.svel, sintable[(q16ang + 2048) & 2047]); - input.svel = mulscale9(localInput.fvel, sintable[(q16ang + 2048) & 2047]) + - mulscale9(localInput.svel, sintable[(q16ang + 1536) & 2047]); - - if (!FURY) - { - input.fvel += pPlayer->fric.x; - input.svel += pPlayer->fric.y; - } - - localInput = {}; - - if (((!GUICapture && (myplayer.gm & MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (g_netServer || ud.multimode > 1)) - && (myplayer.gm & MODE_GAME)) - { - Net_GetPackets(); - G_DoMoveThings(); - } + input.fvel += pPlayer->fric.x; + input.svel += pPlayer->fric.y; } - while (((g_netClient || g_netServer) || (myplayer.gm & (MODE_MENU | MODE_DEMO)) == 0) && (int)(totalclock - ototalclock) >= TICSPERFRAME); - gameUpdate = true; - g_gameUpdateTime = timerGetHiTicks() - gameUpdateStartTime; + localInput = {}; - if (g_gameUpdateAvgTime <= 0.0) - g_gameUpdateAvgTime = g_gameUpdateTime; - - g_gameUpdateAvgTime - = ((GAMEUPDATEAVGTIMENUMSAMPLES - 1.f) * g_gameUpdateAvgTime + g_gameUpdateTime) / ((float)GAMEUPDATEAVGTIMENUMSAMPLES); + if (((!GUICapture && (myplayer.gm & MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (g_netServer || ud.multimode > 1)) + && (myplayer.gm & MODE_GAME)) + { + Net_GetPackets(); + G_DoMoveThings(); + } } + gameUpdate = true; + g_gameUpdateTime = timerGetHiTicks() - gameUpdateStartTime; + + if (g_gameUpdateAvgTime <= 0.0) + g_gameUpdateAvgTime = g_gameUpdateTime; + + g_gameUpdateAvgTime + = ((GAMEUPDATEAVGTIMENUMSAMPLES - 1.f) * g_gameUpdateAvgTime + g_gameUpdateTime) / ((float)GAMEUPDATEAVGTIMENUMSAMPLES); + G_DoCheats(); if (myplayer.gm & (MODE_EOL|MODE_RESTART)) diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index f30763f62..ee26f932f 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -7228,50 +7228,47 @@ MAIN_LOOP_RESTART: char gameUpdate = false; double const gameUpdateStartTime = timerGetHiTicks(); - if (((g_netClient || g_netServer) || !(g_player[myconnectindex].ps->gm & (MODE_MENU|MODE_DEMO))) && totalclock >= ototalclock+TICSPERFRAME) + + while (((g_netClient || g_netServer) || !(g_player[myconnectindex].ps->gm & (MODE_MENU|MODE_DEMO))) && (int)(totalclock - ototalclock) >= TICSPERFRAME) { - do + ototalclock += TICSPERFRAME; + + if (RRRA && g_player[myconnectindex].ps->on_motorcycle) + P_GetInputMotorcycle(myconnectindex); + else if (RRRA && g_player[myconnectindex].ps->on_boat) + P_GetInputBoat(myconnectindex); + else + P_GetInput(myconnectindex); + + // this is where we fill the input_t struct that is actually processed by P_ProcessInput() + auto const pPlayer = g_player[myconnectindex].ps; + auto const q16ang = fix16_to_int(pPlayer->q16ang); + auto & input = inputfifo[g_player[myconnectindex].movefifoend&(MOVEFIFOSIZ-1)][myconnectindex]; + + input = localInput; + input.fvel = mulscale9(localInput.fvel, sintable[(q16ang + 2560) & 2047]) + + mulscale9(localInput.svel, sintable[(q16ang + 2048) & 2047]) + + pPlayer->fric.x; + input.svel = mulscale9(localInput.fvel, sintable[(q16ang + 2048) & 2047]) + + mulscale9(localInput.svel, sintable[(q16ang + 1536) & 2047]) + + pPlayer->fric.y; + localInput = {}; + + g_player[myconnectindex].movefifoend++; + + if (((!GUICapture && (g_player[myconnectindex].ps->gm&MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (g_netServer || ud.multimode > 1)) && + (g_player[myconnectindex].ps->gm&MODE_GAME)) { - ototalclock += TICSPERFRAME; - - if (RRRA && g_player[myconnectindex].ps->on_motorcycle) - P_GetInputMotorcycle(myconnectindex); - else if (RRRA && g_player[myconnectindex].ps->on_boat) - P_GetInputBoat(myconnectindex); - else - P_GetInput(myconnectindex); - - // this is where we fill the input_t struct that is actually processed by P_ProcessInput() - auto const pPlayer = g_player[myconnectindex].ps; - auto const q16ang = fix16_to_int(pPlayer->q16ang); - auto & input = inputfifo[g_player[myconnectindex].movefifoend&(MOVEFIFOSIZ-1)][myconnectindex]; - - input = localInput; - input.fvel = mulscale9(localInput.fvel, sintable[(q16ang + 2560) & 2047]) + - mulscale9(localInput.svel, sintable[(q16ang + 2048) & 2047]) + - pPlayer->fric.x; - input.svel = mulscale9(localInput.fvel, sintable[(q16ang + 2048) & 2047]) + - mulscale9(localInput.svel, sintable[(q16ang + 1536) & 2047]) + - pPlayer->fric.y; - localInput = {}; - - g_player[myconnectindex].movefifoend++; - - if (((!GUICapture && (g_player[myconnectindex].ps->gm&MODE_MENU) != MODE_MENU) || ud.recstat == 2 || (g_netServer || ud.multimode > 1)) && - (g_player[myconnectindex].ps->gm&MODE_GAME)) - { - G_MoveLoop(); - } + G_MoveLoop(); } - while (((g_netClient || g_netServer) || !(g_player[myconnectindex].ps->gm & (MODE_MENU|MODE_DEMO))) && (int)(totalclock - ototalclock) >= TICSPERFRAME); - - gameUpdate = true; - g_gameUpdateTime = timerGetHiTicks()-gameUpdateStartTime; - if (g_gameUpdateAvgTime < 0.f) - g_gameUpdateAvgTime = g_gameUpdateTime; - g_gameUpdateAvgTime = ((GAMEUPDATEAVGTIMENUMSAMPLES-1.f)*g_gameUpdateAvgTime+g_gameUpdateTime)/((float) GAMEUPDATEAVGTIMENUMSAMPLES); } + gameUpdate = true; + g_gameUpdateTime = timerGetHiTicks()-gameUpdateStartTime; + if (g_gameUpdateAvgTime < 0.f) + g_gameUpdateAvgTime = g_gameUpdateTime; + g_gameUpdateAvgTime = ((GAMEUPDATEAVGTIMENUMSAMPLES-1.f)*g_gameUpdateAvgTime+g_gameUpdateTime)/((float) GAMEUPDATEAVGTIMENUMSAMPLES); + G_DoCheats(); if (g_player[myconnectindex].ps->gm & (MODE_EOL|MODE_RESTART))