From 4bc69a011160919b9e4984e5b06fbdf437b9008d Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 1 Dec 2018 16:36:00 +0100 Subject: [PATCH 1/2] Introduce gCurrentRealTimeTicks and refactor some variable names. --- src/openrct2-ui/input/MouseInput.cpp | 6 +++--- src/openrct2/Context.cpp | 11 ++++++----- src/openrct2/Game.cpp | 3 ++- src/openrct2/Game.h | 3 ++- src/openrct2/GameState.cpp | 9 +++++++-- src/openrct2/interface/Window.cpp | 2 +- src/openrct2/localisation/Localisation.Date.cpp | 1 + src/openrct2/rct2/S6Importer.cpp | 1 + 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/openrct2-ui/input/MouseInput.cpp b/src/openrct2-ui/input/MouseInput.cpp index ffdb67a28e..059676227c 100644 --- a/src/openrct2-ui/input/MouseInput.cpp +++ b/src/openrct2-ui/input/MouseInput.cpp @@ -259,7 +259,7 @@ static void input_scroll_right(int32_t x, int32_t y, int32_t state) switch (state) { case MOUSE_STATE_RELEASED: - _ticksSinceDragStart += gTicksSinceLastUpdate; + _ticksSinceDragStart += gCurrentDeltaTime; if (x != 0 || y != 0) { _ticksSinceDragStart = 1000; @@ -552,7 +552,7 @@ static void input_viewport_drag_continue() } viewport = w->viewport; - _ticksSinceDragStart += gTicksSinceLastUpdate; + _ticksSinceDragStart += gCurrentDeltaTime; if (viewport == nullptr) { context_show_cursor(); @@ -1479,7 +1479,7 @@ static void input_update_tooltip(rct_window* w, rct_widgetindex widgetIndex, int window_tooltip_close(); } - gTooltipTimeout += gTicksSinceLastUpdate; + gTooltipTimeout += gCurrentDeltaTime; if (gTooltipTimeout >= 8000) { window_close_by_class(WC_TOOLTIP); diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 7f4078ca06..d866972646 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -110,7 +110,7 @@ namespace OpenRCT2 bool _isWindowMinimised = false; uint32_t _lastTick = 0; uint32_t _accumulator = 0; - uint32_t _lastUpdateTick = 0; + uint32_t _lastUpdateTime = 0; bool _variableFrame = false; // If set, will end the OpenRCT2 game loop. Intentially private to this module so that the flag can not be set back to @@ -914,13 +914,14 @@ namespace OpenRCT2 void Update() { - uint32_t currentUpdateTick = platform_get_ticks(); - gTicksSinceLastUpdate = std::min(currentUpdateTick - _lastUpdateTick, 500); - _lastUpdateTick = currentUpdateTick; + uint32_t currentUpdateTime = platform_get_ticks(); + + gCurrentDeltaTime = std::min(currentUpdateTime - _lastUpdateTime, 500); + _lastUpdateTime = currentUpdateTime; if (game_is_not_paused()) { - gPaletteEffectFrame += gTicksSinceLastUpdate; + gPaletteEffectFrame += gCurrentDeltaTime; } date_update_real_time_of_day(); diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 3b1a082237..507088ce6d 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -65,7 +65,7 @@ #define NUMBER_OF_AUTOSAVES_TO_KEEP 9 -uint16_t gTicksSinceLastUpdate; +uint16_t gCurrentDeltaTime; uint8_t gGamePaused = 0; int32_t gGameSpeed = 1; bool gDoSingleUpdate = false; @@ -82,6 +82,7 @@ uint8_t gUnk13CA740; uint8_t gUnk141F568; uint32_t gCurrentTicks; +uint32_t gCurrentRealTimeTicks; // clang-format off GAME_COMMAND_CALLBACK_POINTER * game_command_callback = nullptr; diff --git a/src/openrct2/Game.h b/src/openrct2/Game.h index c26b00aefe..c0dea522e4 100644 --- a/src/openrct2/Game.h +++ b/src/openrct2/Game.h @@ -137,8 +137,9 @@ extern rct_string_id gErrorStringId; extern GAME_COMMAND_POINTER* new_game_command_table[GAME_COMMAND_COUNT]; extern uint32_t gCurrentTicks; +extern uint32_t gCurrentRealTimeTicks; -extern uint16_t gTicksSinceLastUpdate; +extern uint16_t gCurrentDeltaTime; extern uint8_t gGamePaused; extern int32_t gGameSpeed; extern bool gDoSingleUpdate; diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index b48026b6b0..f88f790794 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -91,6 +91,12 @@ void GameState::Update() } } + uint32_t realtimeTicksElapsed = gCurrentDeltaTime / GAME_UPDATE_TIME_MS; + realtimeTicksElapsed = std::clamp(realtimeTicksElapsed, 1, GAME_MAX_UPDATES); + + // We use this variable to always advance ticks in normal speed. + gCurrentRealTimeTicks += realtimeTicksElapsed; + // Determine how many times we need to update the game if (gGameSpeed > 1) { @@ -98,8 +104,7 @@ void GameState::Update() } else { - numUpdates = gTicksSinceLastUpdate / GAME_UPDATE_TIME_MS; - numUpdates = std::clamp(numUpdates, 1, GAME_MAX_UPDATES); + numUpdates = realtimeTicksElapsed; } if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED diff --git a/src/openrct2/interface/Window.cpp b/src/openrct2/interface/Window.cpp index 9a93a7577c..659418aecb 100644 --- a/src/openrct2/interface/Window.cpp +++ b/src/openrct2/interface/Window.cpp @@ -133,7 +133,7 @@ void window_update_all() // gfx_draw_all_dirty_blocks(); // 1000 tick update - gWindowUpdateTicks += gTicksSinceLastUpdate; + gWindowUpdateTicks += gCurrentDeltaTime; if (gWindowUpdateTicks >= 1000) { gWindowUpdateTicks = 0; diff --git a/src/openrct2/localisation/Localisation.Date.cpp b/src/openrct2/localisation/Localisation.Date.cpp index 8f7e5320a6..8d4b8fbec5 100644 --- a/src/openrct2/localisation/Localisation.Date.cpp +++ b/src/openrct2/localisation/Localisation.Date.cpp @@ -62,6 +62,7 @@ void date_reset() gDateMonthsElapsed = 0; gDateMonthTicks = 0; gCurrentTicks = 0; + gCurrentRealTimeTicks = 0; } void date_set(int32_t year, int32_t month, int32_t day) diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index acfda40e1c..a407f1c81d 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -381,6 +381,7 @@ public: user_string_clear_all(); memcpy(gUserStrings, _s6.custom_strings, sizeof(_s6.custom_strings)); gCurrentTicks = _s6.game_ticks_1; + gCurrentRealTimeTicks = 0; ImportRides(); From 862ef3018cc7c3e27d8b0f34cc1d42e31ffc7095 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 1 Dec 2018 17:00:11 +0100 Subject: [PATCH 2/2] Use gCurrentRealTimeTicks whenever appropriate. --- src/openrct2-ui/windows/GameBottomToolbar.cpp | 2 +- src/openrct2-ui/windows/Guest.cpp | 2 +- src/openrct2-ui/windows/Ride.cpp | 2 +- src/openrct2-ui/windows/RideList.cpp | 2 +- src/openrct2/GameState.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/openrct2-ui/windows/GameBottomToolbar.cpp b/src/openrct2-ui/windows/GameBottomToolbar.cpp index 1418f4663a..a049b530ad 100644 --- a/src/openrct2-ui/windows/GameBottomToolbar.cpp +++ b/src/openrct2-ui/windows/GameBottomToolbar.cpp @@ -486,7 +486,7 @@ static void window_game_bottom_toolbar_draw_park_rating( bar_width = (factor * 114) / 255; gfx_fill_rect_inset(dpi, x + 1, y + 1, x + 114, y + 9, w->colours[1], INSET_RECT_F_30); - if (!(colour & IMAGE_TYPE_REMAP_2_PLUS) || game_is_paused() || (gCurrentTicks & 8)) + if (!(colour & IMAGE_TYPE_REMAP_2_PLUS) || game_is_paused() || (gCurrentRealTimeTicks & 8)) { if (bar_width > 2) gfx_fill_rect_inset(dpi, x + 2, y + 2, x + bar_width - 1, y + 8, colour & 0x7FFFFFFF, 0); diff --git a/src/openrct2-ui/windows/Guest.cpp b/src/openrct2-ui/windows/Guest.cpp index 66c0aac2b5..ec842a3670 100644 --- a/src/openrct2-ui/windows/Guest.cpp +++ b/src/openrct2-ui/windows/Guest.cpp @@ -1378,7 +1378,7 @@ static void window_guest_stats_bars_paint( int32_t blink_flag = colour & BAR_BLINK; colour &= ~BAR_BLINK; - if (!blink_flag || game_is_paused() || (gCurrentTicks & 8) == 0) + if (!blink_flag || game_is_paused() || (gCurrentRealTimeTicks & 8) == 0) { value *= 118; value >>= 8; diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index abf11263c6..e1a93cf2b7 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -3785,7 +3785,7 @@ static void window_ride_maintenance_draw_bar( if (colour & BAR_BLINK) { colour &= ~BAR_BLINK; - if (game_is_not_paused() && (gCurrentTicks & 8)) + if (game_is_not_paused() && (gCurrentRealTimeTicks & 8)) return; } diff --git a/src/openrct2-ui/windows/RideList.cpp b/src/openrct2-ui/windows/RideList.cpp index 22966b91dc..cd1ac2b525 100644 --- a/src/openrct2-ui/windows/RideList.cpp +++ b/src/openrct2-ui/windows/RideList.cpp @@ -302,7 +302,7 @@ static void window_ride_list_resize(rct_window* w) // Refreshing the list can be a very intensive operation // owing to its use of ride_has_any_track_elements(). // This makes sure it's only refreshed every 64 ticks. - if (!(gCurrentTicks & 0x3f)) + if (!(gCurrentRealTimeTicks & 0x3f)) { window_ride_list_refresh_list(w); } diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index f88f790794..a461069af5 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -168,7 +168,7 @@ void GameState::Update() // the flickering frequency is reduced by 4, compared to the original // it was done due to inability to reproduce original frequency // and decision that the original one looks too fast - if (gCurrentTicks % 4 == 0) + if (gCurrentRealTimeTicks % 4 == 0) gWindowMapFlashingFlags ^= (1 << 15); // Handle guest map flashing