scummvm/engines/stark/stark.cpp

279 lines
7.8 KiB
C++
Raw Normal View History

2014-09-21 18:19:07 +02:00
/* ResidualVM - A 3D game interpreter
*
2014-09-21 18:19:07 +02:00
* ResidualVM is the legal property of its developers, whose names
* 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,
* 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.
*
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.
*
*/
#include "engines/stark/stark.h"
#include "engines/stark/archiveloader.h"
#include "engines/stark/console.h"
#include "engines/stark/debug.h"
#include "engines/stark/resourceprovider.h"
#include "engines/stark/resources/level.h"
#include "engines/stark/resources/location.h"
2014-09-21 17:26:20 +02:00
#include "engines/stark/scene.h"
2014-12-28 18:22:25 +01:00
#include "engines/stark/stateprovider.h"
2014-09-21 17:26:20 +02:00
#include "engines/stark/gfx/driver.h"
#include "common/config-manager.h"
#include "common/events.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"
#include "audio/mixer.h"
namespace Common {
DECLARE_SINGLETON(Stark::StarkServices);
}
namespace Stark {
StarkEngine::StarkEngine(OSystem *syst, const ADGameDescription *gameDesc) :
Engine(syst),
_gameDescription(gameDesc),
_gfx(nullptr),
_scene(nullptr),
_console(nullptr),
_global(nullptr),
_archiveLoader(nullptr),
2014-12-28 18:22:25 +01:00
_stateProvider(nullptr),
2015-01-02 10:33:42 +01:00
_resourceProvider(nullptr),
_randomSource(nullptr) {
_mixer->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, 127);
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, ConfMan.getInt("sfx_volume"));
_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, ConfMan.getInt("speech_volume"));
_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, ConfMan.getInt("music_volume"));
// 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");
DebugMan.addDebugChannel(kDebugUnknown, "Unknown", "Debug unknown values on the data");
}
StarkEngine::~StarkEngine() {
2015-01-02 10:33:42 +01:00
delete _randomSource;
delete _scene;
delete _console;
delete _gfx;
delete _resourceProvider;
delete _global;
2014-12-28 18:22:25 +01:00
delete _stateProvider;
delete _archiveLoader;
StarkServices::destroy();
}
Common::Error StarkEngine::run() {
_console = new Console(this);
_gfx = GfxDriver::create();
2010-01-21 20:29:34 +10:30
2010-01-21 21:26:06 +10:30
// Get the screen prepared
_gfx->setupScreen(640, 480, ConfMan.getBool("fullscreen"));
2010-01-21 20:29:34 +10:30
_archiveLoader = new ArchiveLoader();
2014-12-28 18:22:25 +01:00
_stateProvider = new StateProvider();
_global = new Global();
2014-12-28 18:22:25 +01:00
_resourceProvider = new ResourceProvider(_archiveLoader, _stateProvider, _global);
2015-01-02 10:33:42 +01:00
_randomSource = new Common::RandomSource("stark");
// Setup the public services
StarkServices &services = StarkServices::instance();
services.archiveLoader = _archiveLoader;
services.resourceProvider = _resourceProvider;
services.global = _global;
2015-01-02 10:33:42 +01:00
services.randomSource = _randomSource;
// Load global resources
_resourceProvider->initGlobal();
// Start us up at the house of all worlds
_resourceProvider->requestLocationChange(0x45, 0x00);
2010-01-21 21:26:06 +10:30
// Start running
mainLoop();
_resourceProvider->shutdown();
return Common::kNoError;
}
void StarkEngine::mainLoop() {
// Load the initial scene
_scene = new Scene(_gfx);
while (!shouldQuit()) {
2010-01-21 20:29:34 +10:30
// Process events
Common::Event e;
while (g_system->getEventManager()->pollEvent(e)) {
// Handle any buttons, keys and joystick operations
if (e.type == Common::EVENT_KEYDOWN) {
if (e.kbd.ascii == 'q') {
quitGame();
2010-01-21 20:29:34 +10:30
break;
} else if (e.kbd.keycode == Common::KEYCODE_d) {
if (e.kbd.flags & Common::KBD_CTRL) {
_console->attach();
_console->onFrame();
}
2010-01-21 20:29:34 +10:30
} else {
//handleChars(event.type, event.kbd.keycode, event.kbd.flags, event.kbd.ascii);
}
2010-01-21 20:29:34 +10:30
}
/*if (event.type == Common::EVENT_KEYDOWN || event.type == Common::EVENT_KEYUP) {
handleControls(event.type, event.kbd.keycode, event.kbd.flags, event.kbd.ascii);
}*/
// Check for "Hard" quit"
//if (e.type == Common::EVENT_QUIT)
// return;
2010-01-21 20:29:34 +10:30
/*if (event.type == Common::EVENT_SCREEN_CHANGED)
_refreshDrawNeeded = true;*/
}
2014-12-29 23:11:22 +01:00
if (_resourceProvider->hasLocationChangeRequest()) {
_resourceProvider->performLocationChange();
}
2010-01-21 20:29:34 +10:30
updateDisplayScene();
g_system->delayMillis(50);
}
}
void StarkEngine::updateDisplayScene() {
// Get frame delay
static uint32 lastFrame = g_system->getMillis();
uint32 delta = g_system->getMillis() - lastFrame;
lastFrame += delta;
// Clear the screen
_gfx->clearScreen();
2010-01-21 20:29:34 +10:30
// Update the game resources
_global->getLevel()->onGameLoop(delta);
_global->getCurrent()->getLevel()->onGameLoop(delta);
_global->getCurrent()->getLocation()->onGameLoop(delta);
// Render the current scene
_scene->render(delta);
2010-01-21 20:29:34 +10:30
// Swap buffers
_gfx->flipBuffer();
2010-01-21 20:29:34 +10:30
}
2014-12-29 23:11:22 +01:00
bool StarkEngine::hasFeature(EngineFeature f) const {
return
(f == kSupportsLoadingDuringRuntime) ||
(f == kSupportsSavingDuringRuntime);
}
bool StarkEngine::canLoadGameStateCurrently() {
return true;
}
Common::Error StarkEngine::loadGameState(int slot) {
// Open the save file
Common::String filename = Common::String::format("Save%02d.tlj", slot);
Common::InSaveFile *save = _saveFileMan->openForLoading(filename);
if (!save) {
return _saveFileMan->getError();
}
StateReadStream *stream = new StateReadStream(save);
// 1. Read the header
// Save description
Common::String desc = stream->readString();
2014-12-29 23:11:22 +01:00
// Level
Common::String level = stream->readString();
uint levelIndex = strtol(level.c_str(), nullptr, 16);
2014-12-29 23:11:22 +01:00
// Location
Common::String location = stream->readString();
uint locationIndex = strtol(location.c_str(), nullptr, 16);
// Version
Common::String version = stream->readString();
//TODO: Check the version
// 2. Read the resource trees state
_stateProvider->readStateFromStream(stream);
2014-12-29 23:11:22 +01:00
//TODO: Read the rest of the state
delete stream;
2014-12-29 23:11:22 +01:00
_resourceProvider->shutdown();
_resourceProvider->initGlobal();
//TODO: Restore to the correct location
_resourceProvider->requestLocationChange(levelIndex, locationIndex);
2014-12-29 23:11:22 +01:00
return Common::kNoError;
}
bool StarkEngine::canSaveGameStateCurrently() {
return true;
}
Common::Error StarkEngine::saveGameState(int slot, const Common::String &desc) {
// Ensure the state store is up to date
_resourceProvider->commitActiveLocationsState();
// Open the save file
Common::String filename = Common::String::format("Save%02d.tlj", slot);
Common::OutSaveFile *save = _saveFileMan->openForSaving(filename);
if (!save) {
return _saveFileMan->getError();
}
// 1. Write the header
// Save description
save->writeUint32LE(desc.size());
save->writeString(desc);
// Level
Common::String level = _global->getCurrent()->getLevel()->getIndexAsString();
save->writeUint32LE(level.size());
save->writeString(level);
// Location
Common::String location = _global->getCurrent()->getLocation()->getIndexAsString();
save->writeUint32LE(location.size());
save->writeString(location);
2014-12-29 23:11:22 +01:00
// Version
Common::String version = "Version:\t06";
save->writeUint32LE(version.size());
save->writeString(version);
2014-12-29 23:11:22 +01:00
// 2. Write the resource trees state
2014-12-29 23:11:22 +01:00
_stateProvider->writeStateToStream(save);
//TODO: Write the rest of the state
//TODO: Write a screenshot
delete save;
return Common::kNoError;
}
} // End of namespace Stark