1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/stark/stark.h"
#include "engines/stark/console.h"
#include "engines/stark/debug.h"
#include "engines/stark/resources/level.h"
#include "engines/stark/resources/location.h"
#include "engines/stark/savemetadata.h"
#include "engines/stark/scene.h"
#include "engines/stark/services/userinterface.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/dialogplayer.h"
#include "engines/stark/services/diary.h"
#include "engines/stark/services/fontprovider.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/resourceprovider.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/services/staticprovider.h"
#include "engines/stark/services/settings.h"
#include "engines/stark/services/gamechapter.h"
#include "engines/stark/services/gamemessage.h"
#include "engines/stark/gfx/driver.h"
#include "audio/mixer.h"
#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/events.h"
#include "common/fs.h"
#include "common/random.h"
#include "common/savefile.h"
#include "common/system.h"
#include "common/translation.h"
#include "engines/advancedDetector.h"
#include "graphics/renderer.h"
#include "graphics/framelimiter.h"
#include "gui/error.h"
#include "gui/message.h"
namespace Stark {
StarkEngine::StarkEngine(OSystem *syst, const ADGameDescription *gameDesc) :
Engine(syst),
_frameLimiter(nullptr),
_gameDescription(gameDesc),
_lastClickTime(0) {
addModsToSearchPath();
}
StarkEngine::~StarkEngine() {
delete StarkServices::instance().gameInterface;
delete StarkServices::instance().diary;
delete StarkServices::instance().dialogPlayer;
delete StarkServices::instance().randomSource;
delete StarkServices::instance().scene;
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;
delete StarkServices::instance().gfx;
StarkServices::destroy();
delete _frameLimiter;
}
Common::Error StarkEngine::run() {
setDebugger(new Console());
_frameLimiter = new Graphics::FrameLimiter(_system, ConfMan.getInt("engine_speed"));
// Get the screen prepared
Gfx::Driver *gfx = Gfx::Driver::create();
if (gfx == nullptr)
return Common::kNoError;
gfx->init();
if (StarkSettings->isAssetsModEnabled() && !gfx->supportsModdedAssets()) {
GUI::displayErrorDialog(_("Software renderer does not support modded assets"));
}
checkRecommendedDatafiles();
// Setup the public services
StarkServices &services = StarkServices::instance();
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(this, services.gfx);
services.settings = new Settings(_mixer, _gameDescription);
services.gameChapter = new GameChapter();
services.gameMessage = new GameMessage();
// Load global resources
services.staticProvider->init();
services.fontProvider->initFonts();
// Apply the sound volume settings
syncSoundSettings();
// Initialize the UI
services.userInterface->init();
// Load through ScummVM launcher
if (ConfMan.hasKey("save_slot")) {
Common::Error loadError = loadGameState(ConfMan.getInt("save_slot"));
if (loadError.getCode() != Common::kNoError) {
return loadError;
}
}
// Start running
mainLoop();
services.staticProvider->shutdown();
services.resourceProvider->shutdown();
return Common::kNoError;
}
void StarkEngine::mainLoop() {
while (!shouldQuit()) {
_frameLimiter->startFrame();
processEvents();
if (StarkUserInterface->shouldExit())
break;
if (StarkResourceProvider->hasLocationChangeRequest()) {
StarkGlobal->setNormalSpeed();
StarkResourceProvider->performLocationChange();
}
StarkUserInterface->doQueuedScreenChange();
updateDisplayScene();
// Swap buffers
_frameLimiter->delayBeforeSwap();
StarkGfx->flipBuffer();
}
}
void StarkEngine::processEvents() {
Common::Event e;
while (g_system->getEventManager()->pollEvent(e)) {
// Handle any buttons, keys and joystick operations
if (isPaused()) {
// Only pressing key P to resume the game is allowed when the game is paused
if (e.type == Common::EVENT_CUSTOM_ENGINE_ACTION_START && e.customType == kActionPause) {
_gamePauseToken.clear();
}
continue;
}
if (e.type == Common::EVENT_KEYDOWN || e.type == Common::EVENT_CUSTOM_ENGINE_ACTION_START) {
if (e.kbdRepeat) {
continue;
}
if (e.customType == kActionPause) {
if (StarkUserInterface->isInGameScreen()) {
_gamePauseToken = pauseEngine();
debug("The game is paused");
}
} else {
StarkUserInterface->handleKeyPress(e.kbd);
StarkUserInterface->handleActions(e.customType);
}
} else if (e.type == Common::EVENT_LBUTTONUP) {
StarkUserInterface->handleMouseUp();
} else if (e.type == Common::EVENT_MOUSEMOVE) {
StarkUserInterface->handleMouseMove(e.mouse);
} else if (e.type == Common::EVENT_LBUTTONDOWN) {
StarkUserInterface->handleClick();
if (_system->getMillis() - _lastClickTime < _doubleClickDelay) {
StarkUserInterface->handleDoubleClick();
}
_lastClickTime = _system->getMillis();
} else if (e.type == Common::EVENT_RBUTTONDOWN) {
StarkUserInterface->handleRightClick();
} else if (e.type == Common::EVENT_SCREEN_CHANGED) {
onScreenChanged();
}
}
}
void StarkEngine::updateDisplayScene() {
if (StarkGlobal->isFastForward()) {
// 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.
StarkGlobal->setMillisecondsPerGameloop(33);
} else {
StarkGlobal->setMillisecondsPerGameloop(_frameLimiter->getLastFrameDuration());
}
// Clear the screen
StarkGfx->clearScreen();
// Only update the world resources when on the game screen
if (StarkUserInterface->isInGameScreen() && !isPaused()) {
int frames = 0;
do {
// Update the game resources
StarkGlobal->getLevel()->onGameLoop();
StarkGlobal->getCurrent()->getLevel()->onGameLoop();
StarkGlobal->getCurrent()->getLocation()->onGameLoop();
frames++;
// When the game is in fast forward mode, update
// the game resources for multiple frames,
// but render only once.
} while (StarkGlobal->isFastForward() && frames < 100);
StarkGlobal->setNormalSpeed();
}
// Render the current scene
// Update the UI state before displaying the scene
StarkUserInterface->onGameLoop();
// Tell the UI to render, and update implicitly, if this leads to new mouse-over events.
StarkUserInterface->render();
}
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.getPath("path"));
const Common::FSNode modsDir = gameDataDir.getChild("mods");
if (modsDir.exists()) {
Common::FSList list;
if (!modsDir.getChildren(list))
return;
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);
}
}
}
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.getPath("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();
}
}
bool StarkEngine::hasFeature(EngineFeature f) const {
// The TinyGL renderer does not support arbitrary resolutions for now
Common::String rendererConfig = ConfMan.get("renderer");
Graphics::RendererType desiredRendererType = Graphics::Renderer::parseTypeCode(rendererConfig);
Graphics::RendererType matchingRendererType = Graphics::Renderer::getBestMatchingAvailableType(desiredRendererType,
#if defined(USE_OPENGL_GAME)
Graphics::kRendererTypeOpenGL |
#endif
#if defined(USE_OPENGL_SHADERS)
Graphics::kRendererTypeOpenGLShaders |
#endif
#if defined(USE_TINYGL)
Graphics::kRendererTypeTinyGL |
#endif
0);
bool softRenderer = matchingRendererType == Graphics::kRendererTypeTinyGL;
return
(f == kSupportsLoadingDuringRuntime) ||
(f == kSupportsSavingDuringRuntime) ||
(f == kSupportsArbitraryResolutions && !softRenderer) ||
(f == kSupportsReturnToLauncher);
}
bool StarkEngine::canLoadGameStateCurrently(Common::U32String *msg) {
return !StarkUserInterface->isInSaveLoadMenuScreen();
}
Common::Error StarkEngine::loadGameState(int slot) {
// Open the save file
Common::String filename = formatSaveName(_targetName.c_str(), slot);
Common::InSaveFile *save = _saveFileMan->openForLoading(filename);
if (!save) {
return Common::kReadingFailed;
}
StateReadStream stream(save);
// Read the header
SaveMetadata metadata;
Common::ErrorCode metadataErrorCode = metadata.read(&stream, filename);
if (metadataErrorCode != Common::kNoError) {
return metadataErrorCode;
}
// Reset the UI
StarkUserInterface->skipFMV();
StarkUserInterface->clearLocationDependentState();
StarkUserInterface->setInteractive(true);
StarkUserInterface->changeScreen(Screen::kScreenGame);
StarkUserInterface->inventoryOpen(false);
StarkUserInterface->restoreScreenHistory();
// Clear the previous world resources
StarkResourceProvider->shutdown();
if (metadata.version >= 9) {
metadata.skipGameScreenThumbnail(&stream);
}
// Read the resource trees state
StarkStateProvider->readStateFromStream(&stream, metadata.version);
// Read the diary state
StarkDiary->readStateFromStream(&stream, metadata.version);
// Read the location stack
StarkResourceProvider->readLocationStack(&stream, metadata.version);
if (stream.eos()) {
warning("Unexpected end of file reached when reading '%s'", filename.c_str());
return Common::kReadingFailed;
}
if (stream.err()) {
warning("An error occurred when reading '%s'", filename.c_str());
return Common::kReadingFailed;
}
// Initialize the world resources with the loaded state
StarkResourceProvider->initGlobal();
StarkResourceProvider->setShouldRestoreCurrentState();
StarkResourceProvider->requestLocationChange(metadata.levelIndex, metadata.locationIndex);
if (metadata.version >= 9) {
setTotalPlayTime(metadata.totalPlayTime);
}
return Common::kNoError;
}
bool StarkEngine::canSaveGameStateCurrently(Common::U32String *msg) {
// Disallow saving when there is no level loaded or when a script is running
// or when the save & load menu is currently displayed
return StarkGlobal->getLevel() && StarkGlobal->getCurrent() && StarkUserInterface->isInteractive() && !StarkUserInterface->isInSaveLoadMenuScreen();
}
Common::Error StarkEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
// Ensure the state store is up to date
StarkResourceProvider->commitActiveLocationsState();
// Open the save file
Common::String filename = formatSaveName(_targetName.c_str(), slot);
Common::OutSaveFile *save = _saveFileMan->openForSaving(filename);
if (!save) {
return Common::kCreatingFileFailed;
}
bool reuseThumbnail = StarkUserInterface->getGameWindowThumbnail() != nullptr;
if (!reuseThumbnail) {
StarkUserInterface->saveGameScreenThumbnail();
}
// 1. Write the header
SaveMetadata metadata;
metadata.description = desc;
metadata.version = StateProvider::kSaveVersion;
metadata.levelIndex = StarkGlobal->getCurrent()->getLevel()->getIndex();
metadata.locationIndex = StarkGlobal->getCurrent()->getLocation()->getIndex();
metadata.totalPlayTime = getTotalPlayTime();
metadata.gameWindowThumbnail = StarkUserInterface->getGameWindowThumbnail();
metadata.isAutoSave = isAutosave;
TimeDate timeDate;
_system->getTimeAndDate(timeDate);
metadata.setSaveTime(timeDate);
metadata.write(save);
metadata.writeGameScreenThumbnail(save);
// 2. Write the resource trees state
StarkStateProvider->writeStateToStream(save);
// 3. Write the diary state
StarkDiary->writeStateToStream(save);
// 4. Write the location stack
StarkResourceProvider->writeLocationStack(save);
if (!reuseThumbnail) {
StarkUserInterface->freeGameScreenThumbnail();
}
if (save->err()) {
warning("An error occurred when writing '%s'", filename.c_str());
delete save;
return Common::kWritingFailed;
}
delete save;
return Common::kNoError;
}
Common::String StarkEngine::formatSaveName(const char *target, int slot) {
return Common::String::format("%s-%03d.tlj", target, slot);
}
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);
}
void StarkEngine::pauseEngineIntern(bool pause) {
Engine::pauseEngineIntern(pause);
// This function may be called when an error occurs before the engine is fully initialized
if (StarkGlobal && StarkGlobal->getLevel() && StarkGlobal->getCurrent()) {
StarkGlobal->getLevel()->onEnginePause(pause);
StarkGlobal->getCurrent()->getLevel()->onEnginePause(pause);
StarkGlobal->getCurrent()->getLocation()->onEnginePause(pause);
}
if (_frameLimiter) {
_frameLimiter->pause(pause);
}
// Grab a game screen thumbnail in case we need one when writing a save file
if (StarkUserInterface && StarkUserInterface->isInGameScreen()) {
if (pause) {
StarkUserInterface->saveGameScreenThumbnail();
} else {
StarkUserInterface->freeGameScreenThumbnail();
}
}
// The user may have moved the mouse or resized the window while the engine was paused
if (!pause && StarkUserInterface) {
onScreenChanged();
StarkUserInterface->handleMouseMove(_eventMan->getMousePos());
}
}
void StarkEngine::onScreenChanged() const {
bool changed = StarkGfx->computeScreenViewport();
if (changed) {
StarkFontProvider->initFonts();
StarkUserInterface->onScreenChanged();
}
}
uint32 StarkEngine::getGameFlags() const { return _gameDescription->flags; }
} // End of namespace Stark
|