summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Game.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-03-07 23:56:54 +0100
committerAki <please@ignore.pl>2022-03-07 23:57:49 +0100
commitf725e598935860d15099a0310a3aef7197a58e9c (patch)
treeab227ca1318c4a79abdc5ba749724e158869aa46 /Stars45/Game.cpp
parenta80a4f25fe4f7b235443c87ff840e5e06a41290e (diff)
downloadstarshatter-f725e598935860d15099a0310a3aef7197a58e9c.zip
starshatter-f725e598935860d15099a0310a3aef7197a58e9c.tar.gz
starshatter-f725e598935860d15099a0310a3aef7197a58e9c.tar.bz2
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.
Diffstat (limited to 'Stars45/Game.cpp')
-rw-r--r--Stars45/Game.cpp84
1 files changed, 23 insertions, 61 deletions
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<double>(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);
}