2014-09-21 18:19:07 +02:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2009-09-07 23:51:12 +09:30
|
|
|
*
|
2014-09-21 18:19:07 +02:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2009-09-07 23:51:12 +09:30
|
|
|
* are too numerous to list here. Please refer to the AUTHORS
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2014-09-21 18:19:07 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2009-09-07 23:51:12 +09:30
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-09-21 18:19:07 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-09-07 23:51:12 +09:30
|
|
|
*
|
2014-09-21 18:19:07 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-09-07 23:51:12 +09:30
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "engines/stark/stark.h"
|
2015-01-02 17:32:44 +01:00
|
|
|
|
2014-09-21 17:09:55 +02:00
|
|
|
#include "engines/stark/console.h"
|
2010-01-27 19:14:07 +01:00
|
|
|
#include "engines/stark/debug.h"
|
2014-12-28 11:50:12 +01:00
|
|
|
#include "engines/stark/resources/level.h"
|
|
|
|
#include "engines/stark/resources/location.h"
|
2017-06-14 21:41:53 +02:00
|
|
|
#include "engines/stark/savemetadata.h"
|
2014-09-21 17:26:20 +02:00
|
|
|
#include "engines/stark/scene.h"
|
2015-07-14 13:43:44 +02:00
|
|
|
#include "engines/stark/services/userinterface.h"
|
2015-01-11 10:39:24 +01:00
|
|
|
#include "engines/stark/services/archiveloader.h"
|
2015-01-11 15:25:43 +01:00
|
|
|
#include "engines/stark/services/dialogplayer.h"
|
2017-05-30 14:24:22 +02:00
|
|
|
#include "engines/stark/services/diary.h"
|
2015-11-22 20:50:15 +01:00
|
|
|
#include "engines/stark/services/fontprovider.h"
|
|
|
|
#include "engines/stark/services/gameinterface.h"
|
2015-01-11 10:47:52 +01:00
|
|
|
#include "engines/stark/services/global.h"
|
2015-01-11 10:39:24 +01:00
|
|
|
#include "engines/stark/services/resourceprovider.h"
|
2015-01-11 11:01:01 +01:00
|
|
|
#include "engines/stark/services/services.h"
|
2015-01-11 10:39:24 +01:00
|
|
|
#include "engines/stark/services/stateprovider.h"
|
2015-02-22 11:26:31 +01:00
|
|
|
#include "engines/stark/services/staticprovider.h"
|
2018-05-24 14:50:43 +08:00
|
|
|
#include "engines/stark/services/settings.h"
|
2018-06-02 13:04:56 +08:00
|
|
|
#include "engines/stark/services/gamechapter.h"
|
2018-06-30 13:48:55 +08:00
|
|
|
#include "engines/stark/services/gamemessage.h"
|
2014-09-21 17:26:20 +02:00
|
|
|
#include "engines/stark/gfx/driver.h"
|
2017-05-25 17:56:58 +02:00
|
|
|
#include "engines/stark/gfx/framelimiter.h"
|
2009-09-07 23:51:12 +09:30
|
|
|
|
2019-01-29 08:37:13 +01:00
|
|
|
#include "audio/mixer.h"
|
2010-01-24 23:23:39 +08:00
|
|
|
#include "common/config-manager.h"
|
2015-07-21 08:21:33 +02:00
|
|
|
#include "common/debug-channels.h"
|
2010-01-24 23:23:39 +08:00
|
|
|
#include "common/events.h"
|
2019-01-20 17:50:43 +01:00
|
|
|
#include "common/fs.h"
|
2015-01-02 10:33:42 +01:00
|
|
|
#include "common/random.h"
|
2014-12-29 23:11:22 +01:00
|
|
|
#include "common/savefile.h"
|
2011-09-14 02:09:51 +02:00
|
|
|
#include "common/system.h"
|
2019-01-29 08:37:13 +01:00
|
|
|
#include "common/translation.h"
|
|
|
|
#include "gui/message.h"
|
2009-09-07 23:51:12 +09:30
|
|
|
|
|
|
|
namespace Stark {
|
|
|
|
|
2014-09-21 17:09:55 +02:00
|
|
|
StarkEngine::StarkEngine(OSystem *syst, const ADGameDescription *gameDesc) :
|
2014-12-27 19:48:17 +01:00
|
|
|
Engine(syst),
|
2017-05-25 17:56:58 +02:00
|
|
|
_frameLimiter(nullptr),
|
2014-12-27 19:48:17 +01:00
|
|
|
_console(nullptr),
|
2019-02-07 08:20:41 +01:00
|
|
|
_gameDescription(gameDesc),
|
|
|
|
_lastClickTime(0),
|
|
|
|
_lastAutoSaveTime(0) {
|
2010-01-27 19:14:07 +01:00
|
|
|
// Add the available debug channels
|
2011-09-14 02:09:51 +02:00
|
|
|
DebugMan.addDebugChannel(kDebugArchive, "Archive", "Debug the archive loading");
|
|
|
|
DebugMan.addDebugChannel(kDebugXMG, "XMG", "Debug the loading of XMG images");
|
|
|
|
DebugMan.addDebugChannel(kDebugXRC, "XRC", "Debug the loading of XRC resource trees");
|
2019-01-14 20:29:54 +01:00
|
|
|
DebugMan.addDebugChannel(kDebugModding, "Modding", "Debug the loading of modded assets");
|
2019-01-25 09:48:31 +01:00
|
|
|
DebugMan.addDebugChannel(kDebugAnimation, "Animation", "Debug the animation changes");
|
2011-09-14 02:09:51 +02:00
|
|
|
DebugMan.addDebugChannel(kDebugUnknown, "Unknown", "Debug unknown values on the data");
|
2019-01-20 17:50:43 +01:00
|
|
|
|
|
|
|
addModsToSearchPath();
|
2009-09-07 23:51:12 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
StarkEngine::~StarkEngine() {
|
2018-12-21 16:36:06 +01:00
|
|
|
delete StarkServices::instance().gameInterface;
|
|
|
|
delete StarkServices::instance().diary;
|
|
|
|
delete StarkServices::instance().dialogPlayer;
|
|
|
|
delete StarkServices::instance().randomSource;
|
|
|
|
delete StarkServices::instance().scene;
|
|
|
|
delete StarkServices::instance().gfx;
|
|
|
|
delete StarkServices::instance().staticProvider;
|
|
|
|
delete StarkServices::instance().resourceProvider;
|
|
|
|
delete StarkServices::instance().global;
|
|
|
|
delete StarkServices::instance().stateProvider;
|
|
|
|
delete StarkServices::instance().archiveLoader;
|
|
|
|
delete StarkServices::instance().userInterface;
|
|
|
|
delete StarkServices::instance().fontProvider;
|
|
|
|
delete StarkServices::instance().settings;
|
|
|
|
delete StarkServices::instance().gameChapter;
|
|
|
|
delete StarkServices::instance().gameMessage;
|
2015-01-01 21:13:33 +01:00
|
|
|
|
|
|
|
StarkServices::destroy();
|
2018-12-21 16:36:06 +01:00
|
|
|
|
|
|
|
delete _console;
|
|
|
|
delete _frameLimiter;
|
2009-09-07 23:51:12 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
Common::Error StarkEngine::run() {
|
2015-01-11 11:01:01 +01:00
|
|
|
_console = new Console();
|
2018-12-21 16:36:06 +01:00
|
|
|
_frameLimiter = new Gfx::FrameLimiter(_system, ConfMan.getInt("engine_speed"));
|
2019-02-07 08:20:41 +01:00
|
|
|
_lastAutoSaveTime = _system->getMillis();
|
2010-01-21 20:29:34 +10:30
|
|
|
|
2010-01-21 21:26:06 +10:30
|
|
|
// Get the screen prepared
|
2018-12-21 16:36:06 +01:00
|
|
|
Gfx::Driver *gfx = Gfx::Driver::create();
|
|
|
|
gfx->init();
|
2014-12-27 19:48:17 +01:00
|
|
|
|
2019-01-29 08:37:13 +01:00
|
|
|
checkRecommendedDatafiles();
|
|
|
|
|
2015-01-01 21:13:33 +01:00
|
|
|
// Setup the public services
|
|
|
|
StarkServices &services = StarkServices::instance();
|
2018-12-21 16:36:06 +01:00
|
|
|
services.gfx = gfx;
|
|
|
|
services.archiveLoader = new ArchiveLoader();
|
|
|
|
services.stateProvider = new StateProvider();
|
|
|
|
services.global = new Global();
|
|
|
|
services.resourceProvider = new ResourceProvider(services.archiveLoader, services.stateProvider, services.global);
|
|
|
|
services.staticProvider = new StaticProvider(services.archiveLoader);
|
|
|
|
services.randomSource = new Common::RandomSource("stark");
|
|
|
|
services.fontProvider = new FontProvider();
|
|
|
|
services.scene = new Scene(services.gfx);
|
|
|
|
services.dialogPlayer = new DialogPlayer();
|
|
|
|
services.diary = new Diary();
|
|
|
|
services.gameInterface = new GameInterface();
|
|
|
|
services.userInterface = new UserInterface(services.gfx);
|
|
|
|
services.settings = new Settings(_mixer, _gameDescription);
|
|
|
|
services.gameChapter = new GameChapter();
|
|
|
|
services.gameMessage = new GameMessage();
|
2015-01-01 21:13:33 +01:00
|
|
|
|
2014-12-28 11:50:12 +01:00
|
|
|
// Load global resources
|
2018-12-21 16:36:06 +01:00
|
|
|
services.staticProvider->init();
|
|
|
|
services.fontProvider->initFonts();
|
2018-08-15 20:08:53 +02:00
|
|
|
|
|
|
|
// Apply the sound volume settings
|
|
|
|
syncSoundSettings();
|
|
|
|
|
2015-02-24 21:31:56 +01:00
|
|
|
// Initialize the UI
|
2018-12-21 16:36:06 +01:00
|
|
|
services.userInterface->init();
|
2014-12-27 19:48:17 +01:00
|
|
|
|
2018-05-20 20:35:43 +08:00
|
|
|
// Load through ResidualVM launcher
|
|
|
|
if (ConfMan.hasKey("save_slot")) {
|
|
|
|
loadGameState(ConfMan.getInt("save_slot"));
|
|
|
|
}
|
|
|
|
|
2010-01-21 21:26:06 +10:30
|
|
|
// Start running
|
2010-01-24 23:23:39 +08:00
|
|
|
mainLoop();
|
2009-09-07 23:51:12 +09:30
|
|
|
|
2018-12-21 16:36:06 +01:00
|
|
|
services.staticProvider->shutdown();
|
|
|
|
services.resourceProvider->shutdown();
|
2014-12-27 19:48:17 +01:00
|
|
|
|
2009-09-07 23:51:12 +09:30
|
|
|
return Common::kNoError;
|
|
|
|
}
|
|
|
|
|
2010-01-24 23:23:39 +08:00
|
|
|
void StarkEngine::mainLoop() {
|
2010-01-27 01:28:41 +01:00
|
|
|
while (!shouldQuit()) {
|
2017-05-25 17:56:58 +02:00
|
|
|
_frameLimiter->startFrame();
|
2015-02-24 22:54:03 +01:00
|
|
|
|
2016-04-16 07:39:29 +02:00
|
|
|
processEvents();
|
|
|
|
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkUserInterface->shouldExit()) {
|
2015-02-24 22:54:03 +01:00
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
}
|
2017-05-25 17:56:58 +02:00
|
|
|
|
2019-02-07 08:20:41 +01:00
|
|
|
if (shouldPerformAutoSave(_lastAutoSaveTime)) {
|
|
|
|
tryAutoSaving();
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkResourceProvider->hasLocationChangeRequest()) {
|
|
|
|
StarkGlobal->setNormalSpeed();
|
|
|
|
StarkResourceProvider->performLocationChange();
|
2017-05-25 17:56:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-25 21:02:02 +01:00
|
|
|
StarkUserInterface->doQueuedScreenChange();
|
|
|
|
|
2017-05-25 17:56:58 +02:00
|
|
|
updateDisplayScene();
|
|
|
|
|
|
|
|
// Swap buffers
|
|
|
|
_frameLimiter->delayBeforeSwap();
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkGfx->flipBuffer();
|
2010-01-21 20:29:34 +10:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 07:39:29 +02:00
|
|
|
void StarkEngine::processEvents() {
|
|
|
|
Common::Event e;
|
|
|
|
while (g_system->getEventManager()->pollEvent(e)) {
|
|
|
|
// Handle any buttons, keys and joystick operations
|
2018-07-02 16:32:54 +08:00
|
|
|
|
|
|
|
if (isPaused()) {
|
|
|
|
// Only pressing key P to resume the game is allowed when the game is paused
|
|
|
|
if (e.type == Common::EVENT_KEYDOWN && e.kbd.keycode == Common::KEYCODE_p) {
|
|
|
|
pauseEngine(false);
|
|
|
|
}
|
|
|
|
continue;
|
2018-11-22 12:54:20 +01:00
|
|
|
}
|
2018-07-02 16:32:54 +08:00
|
|
|
|
2016-04-16 07:39:29 +02:00
|
|
|
if (e.type == Common::EVENT_KEYDOWN) {
|
2018-11-22 12:35:05 +01:00
|
|
|
if (e.kbdRepeat) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:36:06 +01:00
|
|
|
if (e.kbd.keycode == Common::KEYCODE_d && (e.kbd.hasFlags(Common::KBD_CTRL))) {
|
|
|
|
_console->attach();
|
|
|
|
_console->onFrame();
|
|
|
|
} else if ((e.kbd.keycode == Common::KEYCODE_RETURN || e.kbd.keycode == Common::KEYCODE_KP_ENTER)
|
|
|
|
&& e.kbd.hasFlags(Common::KBD_ALT)) {
|
|
|
|
StarkGfx->toggleFullscreen();
|
2018-07-02 16:32:54 +08:00
|
|
|
} else if (e.kbd.keycode == Common::KEYCODE_p) {
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkUserInterface->isInGameScreen()) {
|
2018-07-02 16:32:54 +08:00
|
|
|
pauseEngine(true);
|
2018-07-15 23:25:39 +08:00
|
|
|
debug("The game is paused");
|
2018-07-02 16:32:54 +08:00
|
|
|
}
|
2018-12-21 16:36:06 +01:00
|
|
|
} else {
|
|
|
|
StarkUserInterface->handleKeyPress(e.kbd);
|
2016-04-16 07:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (e.type == Common::EVENT_LBUTTONUP) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleMouseUp();
|
2016-04-16 07:39:29 +02:00
|
|
|
} else if (e.type == Common::EVENT_MOUSEMOVE) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleMouseMove(e.mouse);
|
2016-04-16 07:39:29 +02:00
|
|
|
} else if (e.type == Common::EVENT_LBUTTONDOWN) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleClick();
|
2016-04-16 07:39:29 +02:00
|
|
|
if (_system->getMillis() - _lastClickTime < _doubleClickDelay) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleDoubleClick();
|
2016-04-16 07:39:29 +02:00
|
|
|
}
|
|
|
|
_lastClickTime = _system->getMillis();
|
|
|
|
} else if (e.type == Common::EVENT_RBUTTONDOWN) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleRightClick();
|
2017-08-26 22:25:18 +02:00
|
|
|
} else if (e.type == Common::EVENT_SCREEN_CHANGED) {
|
2018-10-13 18:08:34 +02:00
|
|
|
onScreenChanged();
|
2016-04-16 07:39:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-24 23:23:39 +08:00
|
|
|
void StarkEngine::updateDisplayScene() {
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkGlobal->isFastForward()) {
|
2017-05-25 21:20:26 +02:00
|
|
|
// The original engine was frame limited to 30 fps.
|
|
|
|
// Set the frame duration to 1000 / 30 ms so that fast forward
|
|
|
|
// skips the same amount of simulated time as the original.
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkGlobal->setMillisecondsPerGameloop(33);
|
2017-05-25 21:20:26 +02:00
|
|
|
} else {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkGlobal->setMillisecondsPerGameloop(_frameLimiter->getLastFrameDuration());
|
2017-05-25 21:20:26 +02:00
|
|
|
}
|
2010-09-04 22:31:13 +09:30
|
|
|
|
2010-01-27 19:14:07 +01:00
|
|
|
// Clear the screen
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkGfx->clearScreen();
|
2010-01-21 20:29:34 +10:30
|
|
|
|
2015-07-19 17:07:23 +02:00
|
|
|
// Only update the world resources when on the game screen
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkUserInterface->isInGameScreen() && !isPaused()) {
|
2017-03-25 18:31:12 +01:00
|
|
|
int frames = 0;
|
|
|
|
do {
|
|
|
|
// Update the game resources
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkGlobal->getLevel()->onGameLoop();
|
|
|
|
StarkGlobal->getCurrent()->getLevel()->onGameLoop();
|
|
|
|
StarkGlobal->getCurrent()->getLocation()->onGameLoop();
|
2017-03-25 18:31:12 +01:00
|
|
|
frames++;
|
|
|
|
|
|
|
|
// When the game is in fast forward mode, update
|
|
|
|
// the game resources for multiple frames,
|
|
|
|
// but render only once.
|
2018-12-21 16:36:06 +01:00
|
|
|
} while (StarkGlobal->isFastForward() && frames < 100);
|
|
|
|
StarkGlobal->setNormalSpeed();
|
2015-02-26 18:03:12 +01:00
|
|
|
}
|
2015-07-19 17:07:23 +02:00
|
|
|
|
|
|
|
// Render the current scene
|
|
|
|
// Update the UI state before displaying the scene
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->onGameLoop();
|
2015-07-19 17:07:23 +02:00
|
|
|
|
2015-02-23 23:44:32 +01:00
|
|
|
// Tell the UI to render, and update implicitly, if this leads to new mouse-over events.
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->render();
|
2010-01-21 20:29:34 +10:30
|
|
|
}
|
2010-01-24 23:23:39 +08:00
|
|
|
|
2019-01-20 17:50:43 +01:00
|
|
|
static bool modsCompare(const Common::FSNode &a, const Common::FSNode &b) {
|
|
|
|
return a.getName() < b.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StarkEngine::addModsToSearchPath() const {
|
|
|
|
const Common::FSNode gameDataDir(ConfMan.get("path"));
|
|
|
|
const Common::FSNode modsDir = gameDataDir.getChild("mods");
|
|
|
|
if (modsDir.exists()) {
|
|
|
|
Common::FSList list;
|
|
|
|
modsDir.getChildren(list);
|
|
|
|
|
|
|
|
Common::sort(list.begin(), list.end(), modsCompare);
|
|
|
|
|
|
|
|
for (uint i = 0; i < list.size(); i++) {
|
|
|
|
SearchMan.addDirectory("mod_" + list[i].getName(), list[i], 0, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:37:13 +01:00
|
|
|
void StarkEngine::checkRecommendedDatafiles() {
|
|
|
|
ConfMan.registerDefault("warn_about_missing_files", true);
|
|
|
|
|
|
|
|
if (!ConfMan.getBool("warn_about_missing_files")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String message = _("You are missing recommended data files:");
|
|
|
|
|
|
|
|
const Common::FSNode gameDataDir(ConfMan.get("path"));
|
|
|
|
Common::FSNode fontsDir = gameDataDir.getChild("fonts");
|
|
|
|
if (!fontsDir.isDirectory()) {
|
|
|
|
fontsDir = gameDataDir.getChild("Fonts"); // FSNode is case sensitive
|
|
|
|
}
|
|
|
|
if (!fontsDir.isDirectory()) {
|
|
|
|
fontsDir = gameDataDir.getChild("FONTS");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool missingFiles = false;
|
|
|
|
if (!fontsDir.isDirectory()) {
|
|
|
|
message += "\n\n";
|
|
|
|
message += _("The 'fonts' folder is required to experience the text style as it was designed. "
|
|
|
|
"The Steam release is known to be missing it. You can get the fonts from the demo version of the game.");
|
|
|
|
missingFiles = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SearchMan.hasFile("gui.ini")) {
|
|
|
|
message += "\n\n";
|
|
|
|
message += _("'gui.ini' is recommended to get proper font settings for the game localization.");
|
|
|
|
missingFiles = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SearchMan.hasFile("language.ini")) {
|
|
|
|
message += "\n\n";
|
|
|
|
message += _("'language.ini' is recommended to get localized confirmation dialogs.");
|
|
|
|
missingFiles = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SearchMan.hasFile("game.exe") && !SearchMan.hasFile("game.dll")) {
|
|
|
|
message += "\n\n";
|
|
|
|
message += _("'game.exe' is recommended to get styled confirmation dialogs.");
|
|
|
|
missingFiles = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (missingFiles) {
|
|
|
|
warning("%s", message.c_str());
|
|
|
|
|
|
|
|
GUI::MessageDialog dialog(message);
|
|
|
|
dialog.runModal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 23:11:22 +01:00
|
|
|
bool StarkEngine::hasFeature(EngineFeature f) const {
|
|
|
|
return
|
|
|
|
(f == kSupportsLoadingDuringRuntime) ||
|
2015-02-22 09:06:02 +01:00
|
|
|
(f == kSupportsSavingDuringRuntime) ||
|
2015-12-25 09:40:16 +01:00
|
|
|
(f == kSupportsArbitraryResolutions) ||
|
|
|
|
(f == kSupportsRTL);
|
2014-12-29 23:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StarkEngine::canLoadGameStateCurrently() {
|
2018-06-02 14:10:42 +08:00
|
|
|
return !StarkUserInterface->isInSaveLoadMenuScreen();
|
2014-12-29 23:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::Error StarkEngine::loadGameState(int slot) {
|
|
|
|
// Open the save file
|
2016-01-31 08:08:55 +01:00
|
|
|
Common::String filename = formatSaveName(_targetName.c_str(), slot);
|
2014-12-30 21:10:36 +01:00
|
|
|
Common::InSaveFile *save = _saveFileMan->openForLoading(filename);
|
|
|
|
if (!save) {
|
|
|
|
return _saveFileMan->getError();
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:41:53 +02:00
|
|
|
StateReadStream stream(save);
|
2014-12-30 21:10:36 +01:00
|
|
|
|
2016-01-31 09:09:19 +01:00
|
|
|
// Read the header
|
2017-06-14 21:41:53 +02:00
|
|
|
SaveMetadata metadata;
|
|
|
|
Common::ErrorCode metadataErrorCode = metadata.read(&stream, filename);
|
|
|
|
if (metadataErrorCode != Common::kNoError) {
|
|
|
|
return metadataErrorCode;
|
2017-05-30 19:07:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the UI
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->skipFMV();
|
|
|
|
StarkUserInterface->clearLocationDependentState();
|
|
|
|
StarkUserInterface->setInteractive(true);
|
|
|
|
StarkUserInterface->changeScreen(Screen::kScreenGame);
|
|
|
|
StarkUserInterface->inventoryOpen(false);
|
|
|
|
StarkUserInterface->restoreScreenHistory();
|
2017-05-30 19:07:42 +02:00
|
|
|
|
|
|
|
// Clear the previous world resources
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkResourceProvider->shutdown();
|
2014-12-30 21:10:36 +01:00
|
|
|
|
2017-06-14 21:41:53 +02:00
|
|
|
if (metadata.version >= 9) {
|
|
|
|
metadata.skipGameScreenThumbnail(&stream);
|
|
|
|
}
|
|
|
|
|
2016-01-31 09:09:19 +01:00
|
|
|
// Read the resource trees state
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkStateProvider->readStateFromStream(&stream, metadata.version);
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2017-05-30 14:24:22 +02:00
|
|
|
// Read the diary state
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkDiary->readStateFromStream(&stream, metadata.version);
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2018-11-17 10:57:44 +01:00
|
|
|
// Read the location stack
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkResourceProvider->readLocationStack(&stream, metadata.version);
|
2018-11-17 10:57:44 +01:00
|
|
|
|
2017-06-14 21:41:53 +02:00
|
|
|
if (stream.eos()) {
|
|
|
|
warning("Unexpected end of file reached when reading '%s'", filename.c_str());
|
|
|
|
return Common::kReadingFailed;
|
|
|
|
}
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2016-01-31 09:09:19 +01:00
|
|
|
// Initialize the world resources with the loaded state
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkResourceProvider->initGlobal();
|
|
|
|
StarkResourceProvider->setShouldRestoreCurrentState();
|
|
|
|
StarkResourceProvider->requestLocationChange(metadata.levelIndex, metadata.locationIndex);
|
2017-06-14 21:41:53 +02:00
|
|
|
|
|
|
|
if (metadata.version >= 9) {
|
|
|
|
setTotalPlayTime(metadata.totalPlayTime);
|
|
|
|
}
|
2014-12-29 23:11:22 +01:00
|
|
|
|
|
|
|
return Common::kNoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StarkEngine::canSaveGameStateCurrently() {
|
2018-05-21 11:11:37 +02:00
|
|
|
// Disallow saving when there is no level loaded or when a script is running
|
2018-05-31 11:53:51 +08:00
|
|
|
// or when the save & load menu is currently displayed
|
2019-02-07 08:20:41 +01:00
|
|
|
return StarkGlobal->getLevel() && StarkGlobal->getCurrent() && StarkUserInterface->isInteractive() && !StarkUserInterface->isInSaveLoadMenuScreen();
|
2014-12-29 23:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::Error StarkEngine::saveGameState(int slot, const Common::String &desc) {
|
|
|
|
// Ensure the state store is up to date
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkResourceProvider->commitActiveLocationsState();
|
2014-12-29 23:11:22 +01:00
|
|
|
|
|
|
|
// Open the save file
|
2016-01-31 08:08:55 +01:00
|
|
|
Common::String filename = formatSaveName(_targetName.c_str(), slot);
|
2014-12-30 21:10:36 +01:00
|
|
|
Common::OutSaveFile *save = _saveFileMan->openForSaving(filename);
|
|
|
|
if (!save) {
|
|
|
|
return _saveFileMan->getError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Write the header
|
2017-06-14 21:41:53 +02:00
|
|
|
SaveMetadata metadata;
|
|
|
|
metadata.description = desc;
|
|
|
|
metadata.version = StateProvider::kSaveVersion;
|
2018-12-21 16:36:06 +01:00
|
|
|
metadata.levelIndex = StarkGlobal->getCurrent()->getLevel()->getIndex();
|
|
|
|
metadata.locationIndex = StarkGlobal->getCurrent()->getLocation()->getIndex();
|
2017-06-14 21:41:53 +02:00
|
|
|
metadata.totalPlayTime = getTotalPlayTime();
|
2018-12-21 16:36:06 +01:00
|
|
|
metadata.gameWindowThumbnail = StarkUserInterface->getGameWindowThumbnail();
|
2014-12-30 21:10:36 +01:00
|
|
|
|
2017-06-14 21:41:53 +02:00
|
|
|
TimeDate timeDate;
|
|
|
|
_system->getTimeAndDate(timeDate);
|
|
|
|
metadata.setSaveTime(timeDate);
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2017-06-14 21:41:53 +02:00
|
|
|
metadata.write(save);
|
|
|
|
metadata.writeGameScreenThumbnail(save);
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2014-12-30 21:10:36 +01:00
|
|
|
// 2. Write the resource trees state
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkStateProvider->writeStateToStream(save);
|
2014-12-29 23:11:22 +01:00
|
|
|
|
2017-05-30 14:24:22 +02:00
|
|
|
// 3. Write the diary state
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkDiary->writeStateToStream(save);
|
2017-05-30 14:24:22 +02:00
|
|
|
|
2018-11-17 10:57:44 +01:00
|
|
|
// 4. Write the location stack
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkResourceProvider->writeLocationStack(save);
|
2018-11-17 10:57:44 +01:00
|
|
|
|
2014-12-29 23:11:22 +01:00
|
|
|
delete save;
|
|
|
|
|
|
|
|
return Common::kNoError;
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:08:55 +01:00
|
|
|
Common::String StarkEngine::formatSaveName(const char *target, int slot) {
|
|
|
|
return Common::String::format("%s-%03d.tlj", target, slot);
|
|
|
|
}
|
2017-05-25 17:56:58 +02:00
|
|
|
|
2019-06-26 21:07:25 +02:00
|
|
|
Common::StringArray StarkEngine::listSaveNames(const char *target) {
|
|
|
|
Common::String pattern = Common::String::format("%s-###.tlj", target);
|
|
|
|
return g_system->getSavefileManager()->listSavefiles(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
int StarkEngine::getSaveNameSlot(const char *target, const Common::String &saveName) {
|
|
|
|
int targetLen = strlen(target);
|
|
|
|
|
|
|
|
char slot[4];
|
|
|
|
slot[0] = saveName[targetLen + 1];
|
|
|
|
slot[1] = saveName[targetLen + 2];
|
|
|
|
slot[2] = saveName[targetLen + 3];
|
|
|
|
slot[3] = '\0';
|
|
|
|
|
|
|
|
return atoi(slot);
|
|
|
|
}
|
|
|
|
|
2019-02-07 08:20:41 +01:00
|
|
|
void StarkEngine::tryAutoSaving() {
|
|
|
|
if (!canSaveGameStateCurrently()) {
|
|
|
|
return; // Can't save right now, try again on the next frame
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastAutoSaveTime = _system->getMillis();
|
|
|
|
|
|
|
|
// Get a thumbnail of the game screen if we don't have one already
|
|
|
|
bool reuseThumbnail = StarkUserInterface->getGameWindowThumbnail() != nullptr;
|
|
|
|
if (!reuseThumbnail) {
|
|
|
|
StarkUserInterface->saveGameScreenThumbnail();
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Error result = saveGameState(0, "Autosave");
|
|
|
|
if (result.getCode() != Common::kNoError) {
|
|
|
|
warning("Unable to autosave: %s.", result.getDesc().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reuseThumbnail) {
|
|
|
|
StarkUserInterface->freeGameScreenThumbnail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:56:58 +02:00
|
|
|
void StarkEngine::pauseEngineIntern(bool pause) {
|
|
|
|
Engine::pauseEngineIntern(pause);
|
|
|
|
|
2018-10-13 18:08:34 +02:00
|
|
|
// This function may be called when an error occurs before the engine is fully initialized
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkGlobal && StarkGlobal->getLevel() && StarkGlobal->getCurrent()) {
|
|
|
|
StarkGlobal->getLevel()->onEnginePause(pause);
|
|
|
|
StarkGlobal->getCurrent()->getLevel()->onEnginePause(pause);
|
|
|
|
StarkGlobal->getCurrent()->getLocation()->onEnginePause(pause);
|
2017-05-25 17:56:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 18:08:34 +02:00
|
|
|
if (_frameLimiter) {
|
|
|
|
_frameLimiter->pause(pause);
|
|
|
|
}
|
2017-06-14 21:41:53 +02:00
|
|
|
|
|
|
|
// Grab a game screen thumbnail in case we need one when writing a save file
|
2018-12-21 16:36:06 +01:00
|
|
|
if (StarkUserInterface && StarkUserInterface->isInGameScreen()) {
|
2017-11-05 15:30:37 +01:00
|
|
|
if (pause) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->saveGameScreenThumbnail();
|
2017-11-05 15:30:37 +01:00
|
|
|
} else {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->freeGameScreenThumbnail();
|
2017-11-05 15:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 18:08:34 +02:00
|
|
|
// The user may have moved the mouse or resized the window while the engine was paused
|
2018-12-21 16:36:06 +01:00
|
|
|
if (!pause && StarkUserInterface) {
|
2018-10-13 18:08:34 +02:00
|
|
|
onScreenChanged();
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkUserInterface->handleMouseMove(_eventMan->getMousePos());
|
2018-10-13 18:08:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StarkEngine::onScreenChanged() const {
|
2018-12-21 16:36:06 +01:00
|
|
|
bool changed = StarkGfx->computeScreenViewport();
|
2018-10-13 18:08:34 +02:00
|
|
|
if (changed) {
|
2018-12-21 16:36:06 +01:00
|
|
|
StarkFontProvider->initFonts();
|
|
|
|
StarkUserInterface->onScreenChanged();
|
2017-06-14 21:41:53 +02:00
|
|
|
}
|
2017-05-25 17:56:58 +02:00
|
|
|
}
|
2018-10-13 18:08:34 +02:00
|
|
|
|
2010-01-24 23:23:39 +08:00
|
|
|
} // End of namespace Stark
|