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
|
/* 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 "common/scummsys.h"
#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/events.h"
#include "common/file.h"
#include "common/language.h"
#include "engines/util.h"
#include "graphics/scaler.h"
#include "graphics/thumbnail.h"
#include "glk/glk.h"
#include "glk/blorb.h"
#include "glk/conf.h"
#include "glk/debugger.h"
#include "glk/events.h"
#include "glk/picture.h"
#include "glk/screen.h"
#include "glk/selection.h"
#include "glk/sound.h"
#include "glk/speech.h"
#include "glk/streams.h"
#include "glk/windows.h"
namespace Glk {
GlkEngine *g_vm;
GlkEngine::GlkEngine(OSystem *syst, const GlkGameDescription &gameDesc) :
_gameDescription(gameDesc), Engine(syst), _random("Glk"), _quitFlag(false), _blorb(nullptr),
_clipboard(nullptr), _conf(nullptr),_events(nullptr), _pictures(nullptr), _screen(nullptr),
_selection(nullptr), _sounds(nullptr), _streams(nullptr), _windows(nullptr),
_copySelect(false), _terminated(false), _pcSpeaker(nullptr), _loadSaveSlot(-1),
gli_register_obj(nullptr), gli_unregister_obj(nullptr), gli_register_arr(nullptr),
gli_unregister_arr(nullptr) {
g_vm = this;
}
GlkEngine::~GlkEngine() {
delete _blorb;
delete _clipboard;
delete _events;
delete _pcSpeaker;
delete _pictures;
delete _screen;
delete _selection;
delete _sounds;
delete _streams;
delete _windows;
delete _conf;
}
void GlkEngine::initialize() {
createConfiguration();
_conf->load();
//_conf->flush();
initGraphicsMode();
createDebugger();
_screen = createScreen();
_screen->initialize();
_clipboard = new Clipboard();
_events = new Events();
_pcSpeaker = new PCSpeaker(_mixer);
_pictures = new Pictures();
_selection = new Selection();
_sounds = new Sounds();
_streams = new Streams();
_windows = new Windows(_screen);
// Setup mixer
syncSoundSettings();
}
Screen *GlkEngine::createScreen() {
return new Screen();
}
void GlkEngine::initGraphicsMode() {
initGraphics(_conf->_width, _conf->_height, &_conf->_screenFormat);
}
void GlkEngine::createDebugger() {
setDebugger(new Debugger());
}
void GlkEngine::createConfiguration() {
_conf = new Conf(getInterpreterType());
}
Common::Error GlkEngine::run() {
// Open up the game file
Common::Path filename(getFilename());
if (!Common::File::exists(filename))
return Common::kNoGameDataFoundError;
if (Blorb::isBlorb(filename)) {
// Blorb archive
_blorb = new Blorb(filename, getInterpreterType());
SearchMan.add("blorb", _blorb, 99, false);
if (!_gameFile.open("game", *_blorb))
return Common::kNoGameDataFoundError;
} else {
// Check for a secondary blorb file with the same filename
Common::Array<Common::Path> blorbFilenames;
Blorb::getBlorbFilenames(filename, blorbFilenames, getInterpreterType(), getGameID());
for (uint idx = 0; idx < blorbFilenames.size(); ++idx) {
if (Common::File::exists(blorbFilenames[idx])) {
_blorb = new Blorb(blorbFilenames[idx], getInterpreterType());
SearchMan.add("blorb", _blorb, 99, false);
break;
}
}
// Open up the game file
if (!_gameFile.open(filename))
return Common::kNoGameDataFoundError;
}
// Perform initialization
initialize();
// Play the game
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, true);
runGame();
g_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, false);
return Common::kNoError;
}
bool GlkEngine::canLoadGameStateCurrently(Common::U32String *msg) {
// Only allow savegames by default when sub-engines are waiting for a line
Window *win = _windows->getFocusWindow();
return win && (win->_lineRequest || win->_lineRequestUni);
}
bool GlkEngine::canSaveGameStateCurrently(Common::U32String *msg) {
// Only allow savegames by default when sub-engines are waiting for a line
Window *win = _windows->getFocusWindow();
return win && (win->_lineRequest || win->_lineRequestUni);
}
Common::Error GlkEngine::loadGame() {
frefid_t ref = _streams->createByPrompt(fileusage_BinaryMode | fileusage_SavedGame,
filemode_Read, 0);
if (ref == nullptr)
return Common::kReadingFailed;
int slotNumber = ref->_slotNumber;
_streams->deleteRef(ref);
return loadGameState(slotNumber);
}
Common::Error GlkEngine::saveGame() {
frefid_t ref = _streams->createByPrompt(fileusage_BinaryMode | fileusage_SavedGame,
filemode_Write, 0);
if (ref == nullptr)
return Common::kWritingFailed;
int slot = ref->_slotNumber;
Common::String desc = ref->_description;
_streams->deleteRef(ref);
return saveGameState(slot, desc);
}
Common::Error GlkEngine::loadGameState(int slot) {
FileReference ref(slot, "", fileusage_SavedGame | fileusage_TextMode);
strid_t file = _streams->openFileStream(&ref, filemode_Read);
if (file == nullptr)
return Common::kReadingFailed;
Common::ErrorCode errCode = Common::kNoError;
QuetzalReader r;
if (r.open(*file, ID_IFSF))
// Load in the savegame chunks
errCode = loadGameChunks(r).getCode();
file->close();
return errCode;
}
Common::Error GlkEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
Common::String msg;
FileReference ref(slot, desc, fileusage_BinaryMode | fileusage_SavedGame);
strid_t file = _streams->openFileStream(&ref, filemode_Write);
if (file == nullptr)
return Common::kWritingFailed;
// Write out savegame chunks
QuetzalWriter w;
Common::ErrorCode errCode = saveGameChunks(w).getCode();
if (errCode == Common::kNoError) {
w.save(*file, desc);
}
file->close();
return errCode;
}
Common::Error GlkEngine::loadGameChunks(QuetzalReader &quetzal) {
// First scan for a SCVM chunk. It has information of the game the save is for,
// so if present we can validate the save is for this game
for (QuetzalReader::Iterator it = quetzal.begin(); it != quetzal.end(); ++it) {
if ((*it)._id == ID_SCVM) {
// Skip over date/time & playtime
Common::SeekableReadStream *rs = it.getStream();
rs->skip(14);
uint32 interpType = rs->readUint32BE();
Common::String langCode = QuetzalReader::readString(rs);
Common::String md5 = QuetzalReader::readString(rs);
delete rs;
if (interpType != QuetzalBase::getInterpreterTag(getInterpreterType()) ||
parseLanguage(langCode) != getLanguage() || md5 != getGameMD5())
return Common::kReadingFailed;
}
}
// Scan for an uncompressed memory chunk
for (QuetzalReader::Iterator it = quetzal.begin(); it != quetzal.end(); ++it) {
if ((*it)._id == ID_UMem) {
Common::SeekableReadStream *rs = it.getStream();
Common::Error err = readSaveData(rs);
delete rs;
return err;
}
}
// Couldn't find any data chunk, so reading failed
return Common::kReadingFailed;
}
Common::Error GlkEngine::saveGameChunks(QuetzalWriter &quetzal) {
// Add the uncompressed memory chunk with the game's save data
Common::WriteStream &ws = quetzal.add(ID_UMem);
return writeGameData(&ws);
}
void GlkEngine::syncSoundSettings() {
Engine::syncSoundSettings();
int volume = ConfMan.getBool("sfx_mute") ? 0 : CLIP(ConfMan.getInt("sfx_volume"), 0, 255);
_mixer->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, volume);
SpeechManager::syncSoundSettings();
}
void GlkEngine::beep() {
_pcSpeaker->speakerOn(50, 50);
}
void GlkEngine::switchToWhiteOnBlack() {
const uint WHITE = _conf->parseColor("ffffff");
const uint BLACK = _conf->parseColor("000000");
_conf->_wMarginX = 0;
_conf->_wMarginY = 0;
_conf->_tMarginY = 4;
_conf->_propInfo._caretColor = WHITE;
_conf->_monoInfo._caretColor = WHITE;
_conf->_windowColor = _conf->_windowSave = 0;
WindowStyle &ws1 = _conf->_tStyles[style_Normal];
ws1.bg = BLACK;
ws1.fg = WHITE;
WindowStyle &ws2 = _conf->_tStyles[style_Input];
ws2.bg = BLACK;
ws2.fg = WHITE;
WindowStyle &ws3 = _conf->_gStyles[style_Normal];
ws3.bg = BLACK;
ws3.fg = WHITE;
}
} // End of namespace Glk
|