From c1a6b61cdc7518cb4787cf5053429b5be5d91ff6 Mon Sep 17 00:00:00 2001 From: morfidon <57798071+morfidon@users.noreply.github.com> Date: Wed, 11 Mar 2026 23:16:40 +0100 Subject: [PATCH] Fix autosave SDL3 tick comparisons Autosave used SDL_TICKS_PASSED in diablo.cpp, but that macro is not available in SDL3, which broke the Linux SDL3 CI build. Switch the autosave timer state to the SDL_GetTicks return type and compare deadlines through a small local helper. The helper keeps SDL_TICKS_PASSED semantics on SDL1/SDL2 while using a direct comparison on SDL3, so this fixes the SDL3 compile error without broad timer refactoring in other code paths. --- Source/diablo.cpp | 48 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index ebe28e9c9..eaa19b552 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -176,19 +176,35 @@ bool was_archives_init = false; /** To know if surfaces have been initialized or not */ bool was_window_init = false; bool was_ui_init = false; -uint32_t autoSaveNextTimerDueAt = 0; +using TickCount = decltype(SDL_GetTicks()); + +TickCount autoSaveNextTimerDueAt = 0; AutoSaveReason pendingAutoSaveReason = AutoSaveReason::None; /** Prevent autosave from running immediately after session start before player interaction. */ bool hasEnteredActiveGameplay = false; -uint32_t autoSaveCooldownUntil = 0; -uint32_t autoSaveCombatCooldownUntil = 0; +TickCount autoSaveCooldownUntil = 0; +TickCount autoSaveCombatCooldownUntil = 0; bool wasAutoSaveEnabled = false; -constexpr uint32_t AutoSaveCooldownMilliseconds = 5000; -constexpr uint32_t AutoSaveCombatCooldownMilliseconds = 4000; +constexpr TickCount AutoSaveCooldownMilliseconds = 5000; +constexpr TickCount AutoSaveCombatCooldownMilliseconds = 4000; + +bool HaveTicksPassed(TickCount now, TickCount due) +{ +#ifdef USE_SDL3 + return now >= due; +#else + return SDL_TICKS_PASSED(now, due); +#endif +} + +bool HaveTicksPassed(TickCount due) +{ + return HaveTicksPassed(SDL_GetTicks(), due); +} -uint32_t GetAutoSaveIntervalMilliseconds() +TickCount GetAutoSaveIntervalMilliseconds() { - return static_cast(std::max(1, *GetOptions().Gameplay.autoSaveIntervalSeconds)) * 1000; + return static_cast(std::max(1, *GetOptions().Gameplay.autoSaveIntervalSeconds)) * 1000; } int GetAutoSavePriority(AutoSaveReason reason) @@ -1623,8 +1639,8 @@ void GameLogic() } if (autoSaveEnabled) { - const uint32_t now = SDL_GetTicks(); - if (SDL_TICKS_PASSED(now, autoSaveNextTimerDueAt)) { + const TickCount now = SDL_GetTicks(); + if (HaveTicksPassed(now, autoSaveNextTimerDueAt)) { QueueAutoSave(AutoSaveReason::Timer); } } @@ -1895,10 +1911,10 @@ bool IsAutoSaveSafe() if (!hasEnteredActiveGameplay) return false; - if (!SDL_TICKS_PASSED(SDL_GetTicks(), autoSaveCooldownUntil)) + if (!HaveTicksPassed(autoSaveCooldownUntil)) return false; - if (!SDL_TICKS_PASSED(SDL_GetTicks(), autoSaveCombatCooldownUntil)) + if (!HaveTicksPassed(autoSaveCombatCooldownUntil)) return false; if (movie_playing || PauseMode != 0 || gmenu_is_active() || IsPlayerInStore()) @@ -1944,11 +1960,11 @@ int GetSecondsUntilNextAutoSave() if (HasPendingAutoSave()) return 0; - const uint32_t now = SDL_GetTicks(); - if (SDL_TICKS_PASSED(now, autoSaveNextTimerDueAt)) + const TickCount now = SDL_GetTicks(); + if (HaveTicksPassed(now, autoSaveNextTimerDueAt)) return 0; - const uint32_t remainingMilliseconds = autoSaveNextTimerDueAt - now; + const TickCount remainingMilliseconds = autoSaveNextTimerDueAt - now; return static_cast((remainingMilliseconds + 999) / 1000); } @@ -1988,9 +2004,9 @@ bool AttemptAutoSave(AutoSaveReason reason) return false; const EventHandler saveProc = SetEventHandler(DisableInputEventHandler); - const uint32_t currentTime = SDL_GetTicks(); + const TickCount currentTime = SDL_GetTicks(); const SaveResult saveResult = SaveGame(SaveKind::Auto); - const uint32_t afterSaveTime = SDL_GetTicks(); + const TickCount afterSaveTime = SDL_GetTicks(); const bool saveSucceeded = saveResult == SaveResult::Success; autoSaveCooldownUntil = afterSaveTime + AutoSaveCooldownMilliseconds;