From c92e10914a61e7b2453b099e7a98105c2f8a30fe Mon Sep 17 00:00:00 2001 From: Dennis Felsing Date: Fri, 28 Oct 2016 09:31:22 +0200 Subject: [PATCH] Refactor demo speed setting, add 1.5x speed (fixes #568) --- src/engine/demo.h | 3 ++ src/engine/shared/demo.cpp | 8 +++++ src/engine/shared/demo.h | 2 ++ src/game/client/components/menus_demo.cpp | 36 +++-------------------- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/engine/demo.h b/src/engine/demo.h index 8c45815c7..1e7cc6aca 100644 --- a/src/engine/demo.h +++ b/src/engine/demo.h @@ -10,6 +10,8 @@ enum MAX_TIMELINE_MARKERS=64 }; +const double g_aSpeeds[] = {0.1f, 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 4.0f, 8.0f}; + struct CDemoHeader { unsigned char m_aMarker[7]; @@ -56,6 +58,7 @@ public: ~IDemoPlayer() {} virtual void SetSpeed(float Speed) = 0; + virtual void SetSpeedIndex(int Offset) = 0; virtual int SetPos(float Percent) = 0; virtual void Pause() = 0; virtual void Unpause() = 0; diff --git a/src/engine/shared/demo.cpp b/src/engine/shared/demo.cpp index d716dffb5..e947259fa 100644 --- a/src/engine/shared/demo.cpp +++ b/src/engine/shared/demo.cpp @@ -369,6 +369,7 @@ CDemoPlayer::CDemoPlayer(class CSnapshotDelta *pSnapshotDelta) { m_File = 0; m_pKeyFrames = 0; + m_SpeedIndex = 4; m_pSnapshotDelta = pSnapshotDelta; m_LastSnapshotDataSize = -1; @@ -657,6 +658,7 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const m_Info.m_Info.m_CurrentTick = -1; m_Info.m_PreviousTick = -1; m_Info.m_Info.m_Speed = 1; + m_SpeedIndex = 4; m_LastSnapshotDataSize = -1; @@ -818,6 +820,12 @@ void CDemoPlayer::SetSpeed(float Speed) m_Info.m_Info.m_Speed = Speed; } +void CDemoPlayer::SetSpeedIndex(int Offset) +{ + m_SpeedIndex = clamp(m_SpeedIndex + Offset, 0, (int)(sizeof(g_aSpeeds)/sizeof(g_aSpeeds[0])-1)); + SetSpeed(g_aSpeeds[m_SpeedIndex]); +} + int CDemoPlayer::Update(bool RealTime) { int64 Now = time_get(); diff --git a/src/engine/shared/demo.h b/src/engine/shared/demo.h index d592e2266..927e068eb 100644 --- a/src/engine/shared/demo.h +++ b/src/engine/shared/demo.h @@ -101,6 +101,7 @@ private: char m_aFilename[256]; CKeyFrame *m_pKeyFrames; CMapInfo m_MapInfo; + int m_SpeedIndex; CPlaybackInfo m_Info; int m_DemoType; @@ -125,6 +126,7 @@ public: void Unpause(); int Stop(); void SetSpeed(float Speed); + void SetSpeedIndex(int Offset); int SetPos(float Percent); const CInfo *BaseInfo() const { return &m_Info.m_Info; } void GetDemoName(char *pBuffer, int BufferSize) const; diff --git a/src/game/client/components/menus_demo.cpp b/src/game/client/components/menus_demo.cpp index f59aa8b90..789835b07 100644 --- a/src/game/client/components/menus_demo.cpp +++ b/src/game/client/components/menus_demo.cpp @@ -148,26 +148,12 @@ void CMenus::RenderDemoPlayer(CUIRect MainView) // handle mousewheel independent of active menu if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP)) { - if(pInfo->m_Speed < 0.1f) DemoPlayer()->SetSpeed(0.1f); - else if(pInfo->m_Speed < 0.25f) DemoPlayer()->SetSpeed(0.25f); - else if(pInfo->m_Speed < 0.5f) DemoPlayer()->SetSpeed(0.5f); - else if(pInfo->m_Speed < 0.75f) DemoPlayer()->SetSpeed(0.75f); - else if(pInfo->m_Speed < 1.0f) DemoPlayer()->SetSpeed(1.0f); - else if(pInfo->m_Speed < 2.0f) DemoPlayer()->SetSpeed(2.0f); - else if(pInfo->m_Speed < 4.0f) DemoPlayer()->SetSpeed(4.0f); - else DemoPlayer()->SetSpeed(8.0f); + DemoPlayer()->SetSpeedIndex(+1); LastSpeedChange = time_get(); } else if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN)) { - if(pInfo->m_Speed > 4.0f) DemoPlayer()->SetSpeed(4.0f); - else if(pInfo->m_Speed > 2.0f) DemoPlayer()->SetSpeed(2.0f); - else if(pInfo->m_Speed > 1.0f) DemoPlayer()->SetSpeed(1.0f); - else if(pInfo->m_Speed > 0.75f) DemoPlayer()->SetSpeed(0.75f); - else if(pInfo->m_Speed > 0.5f) DemoPlayer()->SetSpeed(0.5f); - else if(pInfo->m_Speed > 0.25f) DemoPlayer()->SetSpeed(0.25f); - else if(pInfo->m_Speed > 0.1f) DemoPlayer()->SetSpeed(0.1f); - else DemoPlayer()->SetSpeed(0.05f); + DemoPlayer()->SetSpeedIndex(-1); LastSpeedChange = time_get(); } @@ -422,26 +408,12 @@ void CMenus::RenderDemoPlayer(CUIRect MainView) if(IncreaseDemoSpeed) { - if(pInfo->m_Speed < 0.1f) DemoPlayer()->SetSpeed(0.1f); - else if(pInfo->m_Speed < 0.25f) DemoPlayer()->SetSpeed(0.25f); - else if(pInfo->m_Speed < 0.5f) DemoPlayer()->SetSpeed(0.5f); - else if(pInfo->m_Speed < 0.75f) DemoPlayer()->SetSpeed(0.75f); - else if(pInfo->m_Speed < 1.0f) DemoPlayer()->SetSpeed(1.0f); - else if(pInfo->m_Speed < 2.0f) DemoPlayer()->SetSpeed(2.0f); - else if(pInfo->m_Speed < 4.0f) DemoPlayer()->SetSpeed(4.0f); - else DemoPlayer()->SetSpeed(8.0f); + DemoPlayer()->SetSpeedIndex(+1); LastSpeedChange = time_get(); } else if(DecreaseDemoSpeed) { - if(pInfo->m_Speed > 4.0f) DemoPlayer()->SetSpeed(4.0f); - else if(pInfo->m_Speed > 2.0f) DemoPlayer()->SetSpeed(2.0f); - else if(pInfo->m_Speed > 1.0f) DemoPlayer()->SetSpeed(1.0f); - else if(pInfo->m_Speed > 0.75f) DemoPlayer()->SetSpeed(0.75f); - else if(pInfo->m_Speed > 0.5f) DemoPlayer()->SetSpeed(0.5f); - else if(pInfo->m_Speed > 0.25f) DemoPlayer()->SetSpeed(0.25f); - else if(pInfo->m_Speed > 0.1f) DemoPlayer()->SetSpeed(0.1f); - else DemoPlayer()->SetSpeed(0.05f); + DemoPlayer()->SetSpeedIndex(-1); LastSpeedChange = time_get(); } }