From fab561d757afaafac150018f442fc2f47bcf4c0e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 29 Aug 2020 21:20:10 +0200 Subject: [PATCH] - cleaned up Duke's main GameTicker function. * moved the part that alters the input before queuing it to GetInput * moved moveloop into the main function * reshuffled a few things for better grouping by task. --- source/core/gamecontrol.cpp | 2 +- source/core/gamecvars.cpp | 2 - source/core/gamecvars.h | 1 - source/core/gamestruct.h | 2 + source/games/duke/src/duke3d.h | 3 +- source/games/duke/src/funct.h | 3 +- source/games/duke/src/game_misc.cpp | 2 +- source/games/duke/src/gameloop.cpp | 106 ++++++++++------------------ source/games/duke/src/global.cpp | 1 - source/games/duke/src/global.h | 1 - source/games/duke/src/input.cpp | 38 +++++++++- 11 files changed, 82 insertions(+), 79 deletions(-) diff --git a/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index d617aef53..084802a17 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -118,7 +118,7 @@ CCMD(togglefollow) gi->ResetFollowPos(true); } -glcycle_t thinktime, actortime, gameupdatetime, drawtime; +cycle_t thinktime, actortime, gameupdatetime, drawtime; gamestate_t gamestate = GS_STARTUP; diff --git a/source/core/gamecvars.cpp b/source/core/gamecvars.cpp index 9975ebb75..a30852efe 100644 --- a/source/core/gamecvars.cpp +++ b/source/core/gamecvars.cpp @@ -229,8 +229,6 @@ CUSTOM_CVARD(Int, in_mousebias, 0, CVAR_GLOBALCONFIG|CVAR_ARCHIVE, "emulates the else if (self > 32) self = 32; } -CVARD(Bool, in_mousesmoothing, false, CVAR_GLOBALCONFIG|CVAR_ARCHIVE, "enable/disable mouse input smoothing") - CUSTOM_CVARD(Float, in_mousesensitivity, 1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "changes the mouse sensitivity") { if (self < 0) self = 0; diff --git a/source/core/gamecvars.h b/source/core/gamecvars.h index aaecae826..e412cd54f 100644 --- a/source/core/gamecvars.h +++ b/source/core/gamecvars.h @@ -88,7 +88,6 @@ EXTERN_CVAR(Int, gl_ssao) EXTERN_CVAR(Bool, use_joystick) EXTERN_CVAR(Int, in_mousebias) EXTERN_CVAR(Bool, in_mouseflip) -EXTERN_CVAR(Bool, in_mousesmoothing) EXTERN_CVAR(Float, in_mousesensitivity) EXTERN_CVAR(Float, in_mousescalex) EXTERN_CVAR(Float, in_mousescaley) diff --git a/source/core/gamestruct.h b/source/core/gamestruct.h index 7ae7977b7..adbf617d1 100644 --- a/source/core/gamestruct.h +++ b/source/core/gamestruct.h @@ -5,6 +5,7 @@ bool System_WantGuiCapture(); // During playing this tells us whether the game m #include #include "vectors.h" #include "engineerrors.h" +#include "packet.h" struct GameStats { @@ -91,6 +92,7 @@ struct GameInterface virtual void ExitFromMenu() { throw CExitEvent(0); } virtual ReservedSpace GetReservedScreenSpace(int viewsize) { return { 0, 0 }; } virtual void ResetFollowPos(bool) {} + virtual void GetInput(InputPacket* packet) {} }; diff --git a/source/games/duke/src/duke3d.h b/source/games/duke/src/duke3d.h index 4ebde4370..0d24c1c62 100644 --- a/source/games/duke/src/duke3d.h +++ b/source/games/duke/src/duke3d.h @@ -22,7 +22,7 @@ #include "stats.h" #include "binaryangle.h" -extern glcycle_t drawtime, actortime, thinktime, gameupdatetime; +extern cycle_t drawtime, actortime, thinktime, gameupdatetime; BEGIN_DUKE_NS @@ -57,6 +57,7 @@ struct GameInterface : public ::GameInterface ReservedSpace GetReservedScreenSpace(int viewsize) override; void DrawPlayerSprite(const DVector2& origin, bool onteam) override; void ResetFollowPos(bool message) override; + void GetInput(InputPacket* packet) override; }; diff --git a/source/games/duke/src/funct.h b/source/games/duke/src/funct.h index 5cb8e16bf..3027c5bf4 100644 --- a/source/games/duke/src/funct.h +++ b/source/games/duke/src/funct.h @@ -212,7 +212,7 @@ void dobonus(int bonusonly, const CompletionFunc& completion); void dobonus_d(bool bonusonly, const CompletionFunc& completion); void dobonus_r(bool bonusonly, const CompletionFunc& completion); -void displayrest(double smoothratio); +void drawoverlays(double smoothratio); void drawbackground(void); void displayrooms(int32_t playerNum, double smoothratio); void setgamepalette(int palid); @@ -234,7 +234,6 @@ void apply_seasick(player_struct* p, double scalefactor); void calcviewpitch(player_struct* p, double factor); void sethorizon(int snum, ESyncBits actions, double factor, fixed_t adjustment); bool movementBlocked(int snum); -void GetInput(); void startmainmenu(); void loadcons(); diff --git a/source/games/duke/src/game_misc.cpp b/source/games/duke/src/game_misc.cpp index 8c5b57b4d..db93759a2 100644 --- a/source/games/duke/src/game_misc.cpp +++ b/source/games/duke/src/game_misc.cpp @@ -235,7 +235,7 @@ void V_AddBlend (float r, float g, float b, float a, float v_blend[4]) // //--------------------------------------------------------------------------- -void displayrest(double smoothratio) +void drawoverlays(double smoothratio) { int i, j; unsigned char fader = 0, fadeg = 0, fadeb = 0, fadef = 0, tintr = 0, tintg = 0, tintb = 0, tintf = 0, dotint = 0; diff --git a/source/games/duke/src/gameloop.cpp b/source/games/duke/src/gameloop.cpp index a6893fa5c..5099e7d19 100644 --- a/source/games/duke/src/gameloop.cpp +++ b/source/games/duke/src/gameloop.cpp @@ -51,7 +51,6 @@ static int bufferjitter; void clearfifo(void) { - loc = {}; memset(&inputfifo, 0, sizeof(inputfifo)); memset(sync, 0, sizeof(sync)); } @@ -280,48 +279,17 @@ int domovethings() // //--------------------------------------------------------------------------- -int moveloop() -{ - prediction(); - - if (numplayers < 2) bufferjitter = 0; - while (shouldprocessinput(myconnectindex)) - { - if( domovethings() ) return 1; - } - return 0; -} - -//--------------------------------------------------------------------------- -// -// -// -//--------------------------------------------------------------------------- - -static void checkTimerActive() -{ - FStat *stat = FStat::FindStat("fps"); - glcycle_t::active = (stat != NULL && stat->isActive()); -} - -//--------------------------------------------------------------------------- -// -// -// -//--------------------------------------------------------------------------- - -bool GameTicker() +void GameTicker() { if (ps[myconnectindex].gm == MODE_DEMO) { M_ClearMenus(); - return true; + gamestate = GS_STARTUP; + return; } //Net_GetPackets(); - nonsharedkeys(); - checkTimerActive(); gameupdatetime.Reset(); gameupdatetime.Clock(); @@ -332,31 +300,24 @@ bool GameTicker() while (playrunning() && currentTic - lastTic >= 1) { lastTic = currentTic; - - GetInput(); - 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 = {}; + gi->GetInput(&input); advancequeue(myconnectindex); if (playrunning()) { - moveloop(); + prediction(); + + if (numplayers < 2) bufferjitter = 0; + while (shouldprocessinput(myconnectindex)) + { + if (domovethings()) break; + } } } - double const smoothRatio = playrunning() ? I_GetTimeFrac() * MaxSmoothRatio : MaxSmoothRatio; - gameupdatetime.Unclock(); if (ps[myconnectindex].gm & (MODE_EOL | MODE_RESTART)) @@ -366,17 +327,23 @@ bool GameTicker() if (!cl_syncinput) { - GetInput(); + gi->GetInput(nullptr); } + nonsharedkeys(); + S_Update(); drawtime.Reset(); drawtime.Clock(); - S_Update(); + videoSetBrightness(thunder_brightness); + double const smoothRatio = playrunning() ? I_GetTimeFrac() * MaxSmoothRatio : MaxSmoothRatio; displayrooms(screenpeek, smoothRatio); - displayrest(smoothRatio); + drawoverlays(smoothRatio); drawtime.Unclock(); - return (ps[myconnectindex].gm & MODE_DEMO); + if (ps[myconnectindex].gm == MODE_DEMO) + { + gamestate = GS_STARTUP; + } } //--------------------------------------------------------------------------- @@ -393,18 +360,9 @@ void startmainmenu() FX_StopAllSounds(); } -//--------------------------------------------------------------------------- -// -// -// -//--------------------------------------------------------------------------- -void GameInterface::RunGameFrame() +static void Startup() { - switch (gamestate) - { - default: - case GS_STARTUP: I_ResetTime(); lastTic = -1; gameclock = 0; @@ -432,6 +390,21 @@ void GameInterface::RunGameFrame() { fi.ShowLogo([](bool) { startmainmenu(); }); } +} + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +void GameInterface::RunGameFrame() +{ + switch (gamestate) + { + default: + case GS_STARTUP: + Startup(); break; case GS_MENUSCREEN: @@ -440,13 +413,12 @@ void GameInterface::RunGameFrame() break; case GS_LEVEL: - if (GameTicker()) gamestate = GS_STARTUP; - else videoSetBrightness(thunder_brightness); + GameTicker(); break; case GS_INTERMISSION: case GS_INTRO: - RunScreenJobFrame(); // This handles continuation through its completion callback. + RunScreenJobFrame(); break; } diff --git a/source/games/duke/src/global.cpp b/source/games/duke/src/global.cpp index 831fcfb9d..ed76dc31f 100644 --- a/source/games/duke/src/global.cpp +++ b/source/games/duke/src/global.cpp @@ -63,7 +63,6 @@ int16_t max_ammo_amount[MAX_WEAPONS]; int16_t weaponsandammosprites[15]; int PHEIGHT = PHEIGHT_DUKE; int duke3d_globalflags; -InputPacket loc; uint8_t ready2send; int playerswhenstarted; int show_shareware; diff --git a/source/games/duke/src/global.h b/source/games/duke/src/global.h index 19493020b..9e13c619c 100644 --- a/source/games/duke/src/global.h +++ b/source/games/duke/src/global.h @@ -51,7 +51,6 @@ extern int16_t weaponsandammosprites[15]; extern int32_t PHEIGHT; extern int duke3d_globalflags; extern uint8_t ready2send; -extern InputPacket loc; extern int playerswhenstarted; extern int show_shareware; extern int screenpeek; diff --git a/source/games/duke/src/input.cpp b/source/games/duke/src/input.cpp index 3fed36a2d..5f6e94d81 100644 --- a/source/games/duke/src/input.cpp +++ b/source/games/duke/src/input.cpp @@ -44,6 +44,7 @@ static int nonsharedtimer; static int turnheldtime; static int lastcontroltime; static double lastCheck; +static InputPacket loc; // input accumulation buffer. void GameInterface::ResetFollowPos(bool message) { @@ -1033,7 +1034,7 @@ static void FinalizeInput(int playerNum, InputPacket& input, bool vehicle) // //--------------------------------------------------------------------------- -void GetInput() +static void GetInputInternal(InputPacket &locInput) { double elapsedInputTicks; auto const p = &ps[myconnectindex]; @@ -1090,14 +1091,47 @@ void GetInput() } } +//--------------------------------------------------------------------------- +// +// External entry point +// +//--------------------------------------------------------------------------- + +void GameInterface::GetInput(InputPacket* packet) +{ + GetInputInternal(loc); + if (packet) + { + auto const pPlayer = &ps[myconnectindex]; + auto const q16ang = fix16_to_int(pPlayer->q16ang); + + *packet = loc; + auto fvel = loc.fvel; + auto svel = loc.svel; + packet->fvel = mulscale9(fvel, sintable[(q16ang + 2560) & 2047]) + + mulscale9(svel, sintable[(q16ang + 2048) & 2047]) + + pPlayer->fric.x; + packet->svel = mulscale9(fvel, sintable[(q16ang + 2048) & 2047]) + + mulscale9(svel, sintable[(q16ang + 1536) & 2047]) + + pPlayer->fric.y; + loc = {}; + } +} + +//--------------------------------------------------------------------------- +// // This is called from ImputState::ClearAllInput and resets all static state being used here. +// +//--------------------------------------------------------------------------- + void GameInterface::clearlocalinputstate() { + loc = {}; nonsharedtimer = 0; turnheldtime = 0; lastcontroltime = 0; lastCheck = 0; - } + END_DUKE_NS