Browse Source

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.
pull/8497/head
morfidon 5 days ago
parent
commit
c1a6b61cdc
  1. 48
      Source/diablo.cpp

48
Source/diablo.cpp

@ -176,19 +176,35 @@ bool was_archives_init = false;
/** To know if surfaces have been initialized or not */ /** To know if surfaces have been initialized or not */
bool was_window_init = false; bool was_window_init = false;
bool was_ui_init = false; bool was_ui_init = false;
uint32_t autoSaveNextTimerDueAt = 0; using TickCount = decltype(SDL_GetTicks());
TickCount autoSaveNextTimerDueAt = 0;
AutoSaveReason pendingAutoSaveReason = AutoSaveReason::None; AutoSaveReason pendingAutoSaveReason = AutoSaveReason::None;
/** Prevent autosave from running immediately after session start before player interaction. */ /** Prevent autosave from running immediately after session start before player interaction. */
bool hasEnteredActiveGameplay = false; bool hasEnteredActiveGameplay = false;
uint32_t autoSaveCooldownUntil = 0; TickCount autoSaveCooldownUntil = 0;
uint32_t autoSaveCombatCooldownUntil = 0; TickCount autoSaveCombatCooldownUntil = 0;
bool wasAutoSaveEnabled = false; bool wasAutoSaveEnabled = false;
constexpr uint32_t AutoSaveCooldownMilliseconds = 5000; constexpr TickCount AutoSaveCooldownMilliseconds = 5000;
constexpr uint32_t AutoSaveCombatCooldownMilliseconds = 4000; 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<uint32_t>(std::max(1, *GetOptions().Gameplay.autoSaveIntervalSeconds)) * 1000; return static_cast<TickCount>(std::max(1, *GetOptions().Gameplay.autoSaveIntervalSeconds)) * 1000;
} }
int GetAutoSavePriority(AutoSaveReason reason) int GetAutoSavePriority(AutoSaveReason reason)
@ -1623,8 +1639,8 @@ void GameLogic()
} }
if (autoSaveEnabled) { if (autoSaveEnabled) {
const uint32_t now = SDL_GetTicks(); const TickCount now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, autoSaveNextTimerDueAt)) { if (HaveTicksPassed(now, autoSaveNextTimerDueAt)) {
QueueAutoSave(AutoSaveReason::Timer); QueueAutoSave(AutoSaveReason::Timer);
} }
} }
@ -1895,10 +1911,10 @@ bool IsAutoSaveSafe()
if (!hasEnteredActiveGameplay) if (!hasEnteredActiveGameplay)
return false; return false;
if (!SDL_TICKS_PASSED(SDL_GetTicks(), autoSaveCooldownUntil)) if (!HaveTicksPassed(autoSaveCooldownUntil))
return false; return false;
if (!SDL_TICKS_PASSED(SDL_GetTicks(), autoSaveCombatCooldownUntil)) if (!HaveTicksPassed(autoSaveCombatCooldownUntil))
return false; return false;
if (movie_playing || PauseMode != 0 || gmenu_is_active() || IsPlayerInStore()) if (movie_playing || PauseMode != 0 || gmenu_is_active() || IsPlayerInStore())
@ -1944,11 +1960,11 @@ int GetSecondsUntilNextAutoSave()
if (HasPendingAutoSave()) if (HasPendingAutoSave())
return 0; return 0;
const uint32_t now = SDL_GetTicks(); const TickCount now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, autoSaveNextTimerDueAt)) if (HaveTicksPassed(now, autoSaveNextTimerDueAt))
return 0; return 0;
const uint32_t remainingMilliseconds = autoSaveNextTimerDueAt - now; const TickCount remainingMilliseconds = autoSaveNextTimerDueAt - now;
return static_cast<int>((remainingMilliseconds + 999) / 1000); return static_cast<int>((remainingMilliseconds + 999) / 1000);
} }
@ -1988,9 +2004,9 @@ bool AttemptAutoSave(AutoSaveReason reason)
return false; return false;
const EventHandler saveProc = SetEventHandler(DisableInputEventHandler); const EventHandler saveProc = SetEventHandler(DisableInputEventHandler);
const uint32_t currentTime = SDL_GetTicks(); const TickCount currentTime = SDL_GetTicks();
const SaveResult saveResult = SaveGame(SaveKind::Auto); const SaveResult saveResult = SaveGame(SaveKind::Auto);
const uint32_t afterSaveTime = SDL_GetTicks(); const TickCount afterSaveTime = SDL_GetTicks();
const bool saveSucceeded = saveResult == SaveResult::Success; const bool saveSucceeded = saveResult == SaveResult::Success;
autoSaveCooldownUntil = afterSaveTime + AutoSaveCooldownMilliseconds; autoSaveCooldownUntil = afterSaveTime + AutoSaveCooldownMilliseconds;

Loading…
Cancel
Save