From f725e598935860d15099a0310a3aef7197a58e9c Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 7 Mar 2022 23:56:54 +0100 Subject: Replaced old integrated clock with new one The interface remains unchanged for now, but changing to use GetClock or similar method to get clock owned by the Game instance is preferred. --- Stars45/Clock.cpp | 30 ++++++++++-------- Stars45/Clock.h | 3 +- Stars45/Game.cpp | 84 ++++++++++++++----------------------------------- Stars45/Game.h | 9 ++---- Stars45/StarServer.cpp | 19 +---------- Stars45/Starshatter.cpp | 25 ++------------- 6 files changed, 49 insertions(+), 121 deletions(-) diff --git a/Stars45/Clock.cpp b/Stars45/Clock.cpp index 0bd3447..4973a5c 100644 --- a/Stars45/Clock.cpp +++ b/Stars45/Clock.cpp @@ -20,7 +20,6 @@ Clock::Clock() : m_delta {STARTING_DELTA}, m_gui_delta {STARTING_DELTA} { - Set(); } @@ -28,27 +27,17 @@ void Clock::Set() { m_point = inner_clock::now(); - m_compression = NO_COMPRESSION; m_delta = STARTING_DELTA; m_gui_delta = STARTING_DELTA; } -void -Clock::Reset() -{ - m_game_elapsed = elapsed_duration::zero(); - m_real_elapsed = elapsed_duration::zero(); - Set(); -} - - double Clock::Step() { const auto next = inner_clock::now(); - const auto delta = std::chrono::duration_cast(next - m_point); - m_game_elapsed += std::chrono::duration_cast(delta * m_compression); + const auto delta = std::chrono::duration_cast(next - m_point); + m_game_elapsed += std::chrono::duration_cast(delta * m_compression); m_real_elapsed += delta; const std::chrono::duration seconds = delta; m_delta = seconds.count() * m_compression; @@ -58,6 +47,21 @@ Clock::Step() } +void +Clock::ResetGameTime() +{ + m_game_elapsed = elapsed_duration::zero(); +} + + +void +Clock::SkipGameTime(double seconds) +{ + const std::chrono::duration skip {seconds}; + m_game_elapsed += std::chrono::duration_cast(skip); +} + + double Clock::Delta() const { diff --git a/Stars45/Clock.h b/Stars45/Clock.h index e2ddcb0..d7b41a7 100644 --- a/Stars45/Clock.h +++ b/Stars45/Clock.h @@ -20,8 +20,9 @@ public: Clock(); void Set(); - void Reset(); double Step(); + void ResetGameTime(); + void SkipGameTime(double seconds); double Delta() const; double GuiDelta() const; diff --git a/Stars45/Game.cpp b/Stars45/Game.cpp index 6d91489..92937e6 100644 --- a/Stars45/Game.cpp +++ b/Stars45/Game.cpp @@ -25,26 +25,21 @@ #include "VideoFactory.h" #include "VideoSettings.h" #include "ContentBundle.h" +#include "Clock.h" // +--------------------------------------------------------------------+ Game* game = 0; -const int VIDEO_FPS = 30; -const double MAX_FRAME_TIME_VIDEO = 1.0 / (double) VIDEO_FPS; const double MAX_FRAME_TIME_NORMAL = 1.0 / 5.0; -static LARGE_INTEGER perf_freq; -static LARGE_INTEGER perf_cnt1; -static LARGE_INTEGER perf_cnt2; - // +--------------------------------------------------------------------+ Game::Game() : world(0), video_factory(0), video(0), video_settings(0), soundcard(0), gamma(128), max_tex_size(2048), screen(0), totaltime(0), hInst(0), hwnd(0), frame_rate(0), frame_count(0), frame_count0(0), - frame_time(0), frame_time0(0), gui_seconds(0), + frame_time(0), frame_time0(0), status(Game::OK), exit_code(0), window_style(0) { if (!game) { @@ -54,9 +49,6 @@ Game::Game() paused = false; server = false; show_mouse = false; - real_time = 0; - game_time = 0; - time_comp = 1; frame_number = 0; max_frame_length = MAX_FRAME_TIME_NORMAL; @@ -194,7 +186,7 @@ double Game::FrameTime() { if (game) - return game->seconds; + return game->GetClock()->Delta(); return 0; } @@ -203,7 +195,7 @@ double Game::GUITime() { if (game) - return game->gui_seconds; + return game->GetClock()->GuiDelta(); return 0; } @@ -317,10 +309,6 @@ Game::InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; - // initialize the game timer: - QueryPerformanceFrequency(&perf_freq); - QueryPerformanceCounter(&perf_cnt1); - // center window on display: int screenx = GetSystemMetrics(SM_CXSCREEN); int screeny = GetSystemMetrics(SM_CYSCREEN); @@ -765,6 +753,7 @@ Game::Run() Print("+====================================================================+\n"); // Polling messages from event queue until quit + clock.Set(); while (status < EXIT || Panic::Panicked()) { if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) @@ -849,24 +838,9 @@ Game::GameLoop() Pause(false); } - QueryPerformanceCounter(&perf_cnt2); - - double freq = (double) (perf_freq.QuadPart); - double msec = (double) (perf_cnt2.QuadPart - perf_cnt1.QuadPart); - - msec /= freq; - msec *= 1000.0; - - if (msec < 1) - msec = 1; - - real_time += (DWORD) msec; - + clock.Step(); frame_number++; Mouse::w = 0; - - perf_cnt1 = perf_cnt2; - return wait_for_windows_events; } @@ -875,26 +849,8 @@ Game::GameLoop() void Game::UpdateWorld() { - long new_time = real_time; - double delta = new_time - frame_time; - gui_seconds = delta * 0.001; - seconds = max_frame_length; - - if (time_comp == 1) - { - if (delta < max_frame_length * 1000) - seconds = delta * 0.001; - } - else - { - seconds = time_comp * delta * 0.001; - } - - frame_time = new_time; - game_time += (DWORD) (seconds * 1000); - if (world) - world->ExecFrame(seconds); + world->ExecFrame(clock.Delta()); } // +--------------------------------------------------------------------+ @@ -972,7 +928,7 @@ Game::CollectStats() { frame_count++; - if (!totaltime) totaltime = real_time; + if (!totaltime) totaltime = clock.RealTime(); if (frame_time - frame_time0 > 200) { frame_rate = (frame_count - frame_count0) * 1000.0 / (frame_time - frame_time0); @@ -1000,7 +956,7 @@ Game::ShowStats() { if (server) return; - totaltime = real_time - totaltime; + totaltime = clock.RealTime() - totaltime; Print("\n"); Print("Performance Data:\n"); @@ -1282,34 +1238,40 @@ GetKeyPlus(int& key, int& shift) // +====================================================================+ +Clock* +Game::GetClock() +{ + return &clock; +} + + DWORD GetRealTime() { Game* game = Game::GetInstance(); if (game) return game->RealTime(); - - return timeGetTime(); + return 0; } DWORD Game::RealTime() { - return real_time; + return clock.RealTime(); } DWORD Game::GameTime() { - return game_time; + return clock.GameTime(); } DWORD Game::TimeCompression() { - return time_comp; + return clock.TimeCompression(); } void Game::SetTimeCompression(DWORD comp) { if (comp > 0 && comp <= 100) - time_comp = comp; + clock.SetTimeCompression(static_cast(comp)); } DWORD Game::Frame() @@ -1319,11 +1281,11 @@ DWORD Game::Frame() void Game::ResetGameTime() { - game_time = 0; + clock.ResetGameTime(); } void Game::SkipGameTime(double seconds) { if (seconds > 0) - game_time += (DWORD) (seconds * 1000); + clock.SkipGameTime(seconds); } diff --git a/Stars45/Game.h b/Stars45/Game.h index 5ce1756..d9054e9 100644 --- a/Stars45/Game.h +++ b/Stars45/Game.h @@ -11,6 +11,7 @@ #define Game_h #include "ApplicationDX9.h" +#include "Clock.h" #include "Types.h" #include "Screen.h" #include "Video.h" @@ -75,6 +76,7 @@ public: void SetGammaLevel(int g); void SetMaxTexSize(int n); + Clock* GetClock(); DWORD RealTime(); DWORD GameTime(); DWORD TimeCompression(); @@ -163,9 +165,7 @@ protected: RECT bounds_rect; // Saved window bounds for mode switches RECT client_rect; // Saved client area size for mode switches - - double gui_seconds; - double seconds; + Clock clock; double frame_rate; int frame_count; int frame_count0; @@ -180,9 +180,6 @@ protected: bool paused; bool server; bool show_mouse; - DWORD real_time; - DWORD game_time; - DWORD time_comp; DWORD frame_number; double max_frame_length; diff --git a/Stars45/StarServer.cpp b/Stars45/StarServer.cpp index c972a41..34eb77f 100644 --- a/Stars45/StarServer.cpp +++ b/Stars45/StarServer.cpp @@ -320,21 +320,6 @@ StarServer::GameLoop() void StarServer::UpdateWorld() { - long new_time = real_time; - double delta = new_time - frame_time; - seconds = max_frame_length; - gui_seconds = delta * 0.001; - - if (frame_time == 0) - gui_seconds = 0; - - time_comp = 1; - - if (delta < max_frame_length * 1000) - seconds = delta * 0.001; - - frame_time = new_time; - Galaxy* galaxy = Galaxy::GetInstance(); if (galaxy) galaxy->ExecFrame(); @@ -347,12 +332,10 @@ StarServer::UpdateWorld() } else { - game_time += (DWORD) (seconds * 1000); - Drive::StartFrame(); if (world) - world->ExecFrame(seconds); + world->ExecFrame(clock.Delta()); } static DWORD refresh_time = 0; diff --git a/Stars45/Starshatter.cpp b/Stars45/Starshatter.cpp index 7326128..eb971e8 100644 --- a/Stars45/Starshatter.cpp +++ b/Stars45/Starshatter.cpp @@ -855,23 +855,6 @@ Starshatter::GameLoop() void Starshatter::UpdateWorld() { - long new_time = real_time; - double delta = new_time - frame_time; // in milliseconds - seconds = max_frame_length; // in seconds - gui_seconds = delta * 0.001; - - if (frame_time == 0) - gui_seconds = 0; - - if (delta < time_comp * max_frame_length * 1000) { - seconds = time_comp * delta * 0.001; - } - else { - seconds = time_comp * max_frame_length; - } - - frame_time = new_time; - Galaxy* galaxy = Galaxy::GetInstance(); if (galaxy) galaxy->ExecFrame(); @@ -890,12 +873,10 @@ Starshatter::UpdateWorld() } else { - game_time += (DWORD) (seconds * 1000); - Drive::StartFrame(); if (world) - world->ExecFrame(seconds); + world->ExecFrame(clock.Delta()); } if (game_mode == PLAY_MODE || InCutscene()) { @@ -906,7 +887,7 @@ Starshatter::UpdateWorld() cam_dir->VirtualHeadOffset(head_tracker->GetX(), head_tracker->GetY(), head_tracker->GetZ()); } - cam_dir->ExecFrame(gui_seconds); + cam_dir->ExecFrame(clock.GuiDelta()); } Sim* sim = Sim::GetSim(); @@ -916,7 +897,7 @@ Starshatter::UpdateWorld() ListIter iter = rgn->Ships(); while (++iter) { Ship* s = iter.value(); - s->SelectDetail(seconds); + s->SelectDetail(clock.Delta()); } } } -- cgit v1.1