diff --git a/backends/events/default/default-events.cpp b/backends/events/default/default-events.cpp index 655466356f7..9565d110b54 100644 --- a/backends/events/default/default-events.cpp +++ b/backends/events/default/default-events.cpp @@ -89,6 +89,10 @@ bool DefaultEventManager::pollEvent(Common::Event &event) { handleKeyRepeat(); } + if (g_engine) + // Handle autosaves if enabled + g_engine->handleAutoSave(); + if (_eventQueue.empty()) { return false; } diff --git a/engines/engine.cpp b/engines/engine.cpp index 1a66089a563..c9582a533bc 100644 --- a/engines/engine.cpp +++ b/engines/engine.cpp @@ -148,7 +148,9 @@ Engine::Engine(OSystem *syst) _saveSlotToLoad(-1), _engineStartTime(_system->getMillis()), _mainMenuDialog(NULL), - _debugger(NULL) { + _debugger(NULL), + _autosaveInterval(ConfMan.getInt("autosave_period")), + _lastAutosaveTime(_system->getMillis()) { g_engine = this; Common::setErrorOutputFormatter(defaultOutputFormatter); @@ -482,10 +484,16 @@ void Engine::checkCD() { #endif } -bool Engine::shouldPerformAutoSave(int lastSaveTime) { - const int diff = _system->getMillis() - lastSaveTime; - const int autosavePeriod = ConfMan.getInt("autosave_period"); - return autosavePeriod != 0 && diff > autosavePeriod * 1000; +void Engine::handleAutoSave() { + const int diff = _system->getMillis() - _lastAutosaveTime; + + if (_autosaveInterval != 0 && diff > (_autosaveInterval * 1000)) { + // Save the autosave + saveGameState(getAutosaveSlot(), _("Autosave"), true); + + // Reset the last autosave time + _lastAutosaveTime = _system->getMillis(); + } } void Engine::errorString(const char *buf1, char *buf2, int size) { diff --git a/engines/engine.h b/engines/engine.h index 38ed8294674..c60dffaabb1 100644 --- a/engines/engine.h +++ b/engines/engine.h @@ -91,6 +91,16 @@ private: */ int32 _engineStartTime; + /** + * Autosave interval + */ + const int _autosaveInterval; + + /** + * The last time an autosave was done + */ + int _lastAutosaveTime; + /** * Save slot selected via global main menu. * This slot will be loaded after main menu execution (not from inside @@ -290,7 +300,6 @@ public: bool loadGameDialog(); protected: - /** * Actual implementation of pauseEngine by subclasses. See there * for details. @@ -367,17 +376,27 @@ public: inline Common::SaveFileManager *getSaveFileManager() { return _saveFileMan; } public: - /** On some systems, check if the game appears to be run from CD. */ void checkCD(); -protected: /** - * Indicate whether an autosave should be performed. + * Checks for whether it's time to do an autosave, and if so, does it. */ - bool shouldPerformAutoSave(int lastSaveTime); + void handleAutoSave(); + /** + * Returns the slot that should be used for autosaves + */ + virtual int getAutosaveSlot() const { + return 0; + } + + bool shouldPerformAutoSave(int lastSaveTime) { + // TODO: Remove deprecated method once all engines are refactored + // to no longer do autosaves directly themselves + return false; + } }; // Chained games