1
0
Fork 0

Codechange: switch all video drivers to std::chrono for keeping time

On all OSes we tested the std::chrono::steady_clock is of a high
enough resolution to do millisecond measurements, which is all we
need.

By accident, this fixes a Win32 driver bug, where we would never
hit our targets, as the resolution of the clock was too low to
do accurate millisecond measurements with (it was ~16ms resolution
instead).
pull/8694/head
Patric Stout 2021-02-17 14:46:19 +01:00 committed by Patric Stout
parent d437445c67
commit 25f6851ca1
7 changed files with 44 additions and 101 deletions

View File

@ -445,34 +445,15 @@ void VideoDriver_Allegro::Stop()
if (--_allegro_instance_count == 0) allegro_exit(); if (--_allegro_instance_count == 0) allegro_exit();
} }
#if defined(UNIX) || defined(__OS2__)
# include <sys/time.h> /* gettimeofday */
static uint32 GetTime()
{
struct timeval tim;
gettimeofday(&tim, nullptr);
return tim.tv_usec / 1000 + tim.tv_sec * 1000;
}
#else
static uint32 GetTime()
{
return GetTickCount();
}
#endif
void VideoDriver_Allegro::MainLoop() void VideoDriver_Allegro::MainLoop()
{ {
uint32 cur_ticks = GetTime(); auto cur_ticks = std::chrono::steady_clock::now();
uint32 last_cur_ticks = cur_ticks; auto last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; auto next_tick = cur_ticks;
CheckPaletteAnim(); CheckPaletteAnim();
for (;;) { for (;;) {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
PollEvent(); PollEvent();
@ -491,11 +472,11 @@ void VideoDriver_Allegro::MainLoop()
_fast_forward = 0; _fast_forward = 0;
} }
cur_ticks = GetTime(); cur_ticks = std::chrono::steady_clock::now();
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
_realtime_tick += cur_ticks - last_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed; bool old_ctrl_pressed = _ctrl_pressed;

View File

@ -634,19 +634,13 @@ bool VideoDriver_Cocoa::PollEvent()
/** Main game loop. */ /** Main game loop. */
void VideoDriver_Cocoa::GameLoop() void VideoDriver_Cocoa::GameLoop()
{ {
uint32 cur_ticks = GetTick(); auto cur_ticks = std::chrono::steady_clock::now();
uint32 last_cur_ticks = cur_ticks; auto last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; auto next_tick = cur_ticks;
#ifdef _DEBUG
uint32 et0 = GetTick();
uint32 st = 0;
#endif
for (;;) { for (;;) {
@autoreleasepool { @autoreleasepool {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
while (this->PollEvent()) {} while (this->PollEvent()) {}
@ -669,11 +663,11 @@ void VideoDriver_Cocoa::GameLoop()
_fast_forward = 0; _fast_forward = 0;
} }
cur_ticks = GetTick(); cur_ticks = std::chrono::steady_clock::now();
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
_realtime_tick += cur_ticks - last_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed; bool old_ctrl_pressed = _ctrl_pressed;
@ -688,28 +682,13 @@ void VideoDriver_Cocoa::GameLoop()
this->CheckPaletteAnim(); this->CheckPaletteAnim();
this->Draw(); this->Draw();
} else { } else {
#ifdef _DEBUG
uint32 st0 = GetTick();
#endif
CSleep(1); CSleep(1);
#ifdef _DEBUG
st += GetTick() - st0;
#endif
NetworkDrawChatMessage(); NetworkDrawChatMessage();
DrawMouseCursor(); DrawMouseCursor();
this->Draw(); this->Draw();
} }
} }
} }
#ifdef _DEBUG
uint32 et = GetTick();
DEBUG(driver, 1, "cocoa_v: nextEventMatchingMask took %i ms total", _tEvent);
DEBUG(driver, 1, "cocoa_v: game loop took %i ms total (%i ms without sleep)", et - et0, et - et0 - st);
DEBUG(driver, 1, "cocoa_v: (nextEventMatchingMask total)/(game loop total) is %f%%", (double)_tEvent / (double)(et - et0) * 100);
DEBUG(driver, 1, "cocoa_v: (nextEventMatchingMask total)/(game loop without sleep total) is %f%%", (double)_tEvent / (double)(et - et0 - st) * 100);
#endif
} }

View File

@ -195,14 +195,6 @@ static bool InputWaiting()
return select(STDIN + 1, &readfds, nullptr, nullptr, &tv) > 0; return select(STDIN + 1, &readfds, nullptr, nullptr, &tv) > 0;
} }
static uint32 GetTime()
{
struct timeval tim;
gettimeofday(&tim, nullptr);
return tim.tv_usec / 1000 + tim.tv_sec * 1000;
}
#else #else
static bool InputWaiting() static bool InputWaiting()
@ -210,11 +202,6 @@ static bool InputWaiting()
return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0; return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
} }
static uint32 GetTime()
{
return GetTickCount();
}
#endif #endif
static void DedicatedHandleKeyInput() static void DedicatedHandleKeyInput()
@ -248,8 +235,9 @@ static void DedicatedHandleKeyInput()
void VideoDriver_Dedicated::MainLoop() void VideoDriver_Dedicated::MainLoop()
{ {
uint32 cur_ticks = GetTime(); auto cur_ticks = std::chrono::steady_clock::now();
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; auto last_cur_ticks = cur_ticks;
auto next_tick = cur_ticks;
/* Signal handlers */ /* Signal handlers */
#if defined(UNIX) #if defined(UNIX)
@ -290,15 +278,14 @@ void VideoDriver_Dedicated::MainLoop()
} }
while (!_exit_game) { while (!_exit_game) {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
if (!_dedicated_forks) DedicatedHandleKeyInput(); if (!_dedicated_forks) DedicatedHandleKeyInput();
cur_ticks = GetTime(); cur_ticks = std::chrono::steady_clock::now();
_realtime_tick += cur_ticks - prev_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) { if (cur_ticks >= next_tick || _ddc_fastforward) {
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
GameLoop(); GameLoop();
UpdateWindows(); UpdateWindows();

View File

@ -734,7 +734,6 @@ void VideoDriver_SDL::LoopOnce()
uint32 mod; uint32 mod;
int numkeys; int numkeys;
const Uint8 *keys; const Uint8 *keys;
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
while (PollEvent() == -1) {} while (PollEvent() == -1) {}
@ -768,11 +767,11 @@ void VideoDriver_SDL::LoopOnce()
_fast_forward = 0; _fast_forward = 0;
} }
cur_ticks = SDL_GetTicks(); cur_ticks = std::chrono::steady_clock::now();
if (SDL_TICKS_PASSED(cur_ticks, next_tick) || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
_realtime_tick += cur_ticks - last_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed; bool old_ctrl_pressed = _ctrl_pressed;
@ -826,9 +825,9 @@ void VideoDriver_SDL::LoopOnce()
void VideoDriver_SDL::MainLoop() void VideoDriver_SDL::MainLoop()
{ {
cur_ticks = SDL_GetTicks(); cur_ticks = std::chrono::steady_clock::now();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks;
this->CheckPaletteAnim(); this->CheckPaletteAnim();

View File

@ -62,9 +62,9 @@ private:
*/ */
bool edit_box_focused; bool edit_box_focused;
uint32 cur_ticks; std::chrono::steady_clock::time_point cur_ticks;
uint32 last_cur_ticks; std::chrono::steady_clock::time_point last_cur_ticks;
uint32 next_tick; std::chrono::steady_clock::time_point next_tick;
int startup_display; int startup_display;
std::thread draw_thread; std::thread draw_thread;

View File

@ -648,9 +648,9 @@ void VideoDriver_SDL::Stop()
void VideoDriver_SDL::MainLoop() void VideoDriver_SDL::MainLoop()
{ {
uint32 cur_ticks = SDL_GetTicks(); auto cur_ticks = std::chrono::steady_clock::now();
uint32 last_cur_ticks = cur_ticks; auto last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; auto next_tick = cur_ticks;
uint32 mod; uint32 mod;
int numkeys; int numkeys;
Uint8 *keys; Uint8 *keys;
@ -690,7 +690,6 @@ void VideoDriver_SDL::MainLoop()
DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no "); DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
for (;;) { for (;;) {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
while (PollEvent() == -1) {} while (PollEvent() == -1) {}
@ -719,11 +718,11 @@ void VideoDriver_SDL::MainLoop()
_fast_forward = 0; _fast_forward = 0;
} }
cur_ticks = SDL_GetTicks(); cur_ticks = std::chrono::steady_clock::now();
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
_realtime_tick += cur_ticks - last_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed; bool old_ctrl_pressed = _ctrl_pressed;

View File

@ -1133,9 +1133,9 @@ static void CheckPaletteAnim()
void VideoDriver_Win32::MainLoop() void VideoDriver_Win32::MainLoop()
{ {
MSG mesg; MSG mesg;
uint32 cur_ticks = GetTickCount(); auto cur_ticks = std::chrono::steady_clock::now();
uint32 last_cur_ticks = cur_ticks; auto last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; auto next_tick = cur_ticks;
std::thread draw_thread; std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock; std::unique_lock<std::recursive_mutex> draw_lock;
@ -1176,8 +1176,6 @@ void VideoDriver_Win32::MainLoop()
CheckPaletteAnim(); CheckPaletteAnim();
for (;;) { for (;;) {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
while (PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) { while (PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) {
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
/* Convert key messages to char messages if we want text input. */ /* Convert key messages to char messages if we want text input. */
@ -1198,11 +1196,11 @@ void VideoDriver_Win32::MainLoop()
_fast_forward = 0; _fast_forward = 0;
} }
cur_ticks = GetTickCount(); cur_ticks = std::chrono::steady_clock::now();
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) { if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
_realtime_tick += cur_ticks - last_cur_ticks; _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks; last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK; next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed; bool old_ctrl_pressed = _ctrl_pressed;