File: engine.h

package info (click to toggle)
scummvm 2.2.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 227,768 kB
  • sloc: cpp: 2,525,134; ansic: 144,108; asm: 28,422; sh: 9,109; python: 8,774; xml: 6,003; perl: 3,523; java: 1,547; makefile: 948; yacc: 720; lex: 437; javascript: 336; objc: 81; sed: 22; php: 1
file content (506 lines) | stat: -rw-r--r-- 13,617 bytes parent folder | download
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
/* 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 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
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef ENGINES_ENGINE_H
#define ENGINES_ENGINE_H

#include "common/scummsys.h"
#include "common/str.h"
#include "common/language.h"
#include "common/platform.h"
#include "common/queue.h"
#include "common/singleton.h"

class OSystem;
class MetaEngine;

namespace Audio {
class Mixer;
}
namespace Common {
class Error;
class EventManager;
class SaveFileManager;
class TimerManager;
class FSNode;
class SeekableReadStream;
class WriteStream;
}
namespace GUI {
class Debugger;
class Dialog;
}

/**
 * Initializes graphics and shows error message.
 */
void GUIErrorMessage(const Common::String &msg, const char *url = nullptr);
void GUIErrorMessageWithURL(const Common::String &msg, const char *url);
void GUIErrorMessageFormat(const char *fmt, ...) GCC_PRINTF(1, 2);

class Engine;


/**
* Manages pausing by Engine::pauseEngine handing out tokens that
* each represent one requested level of pause.
*/
class PauseToken {
public:
	PauseToken();
	PauseToken(const PauseToken &);
#if __cplusplus >= 201103L
	PauseToken(PauseToken &&);
#endif
	~PauseToken();

	void operator=(const PauseToken &);
#if __cplusplus >= 201103L
	void operator=(PauseToken &&);
#endif
	/** Manually releases the PauseToken. Only allowed if the token
	* currently represents a pause request.
	*/
	void clear();

	/**
	 * Returns true if the PauseToken represents a pause level,
	 * false if it is empty.
	 */
	bool isActive() const { return _engine != nullptr; }

private:
	PauseToken(Engine *);

	Engine *_engine;

	friend class Engine;
};

class Engine {
public:
	OSystem *_system;
	Audio::Mixer *_mixer;

protected:
	Common::TimerManager *_timer;
	Common::EventManager *_eventMan;
	Common::SaveFileManager *_saveFileMan;

	GUI::Dialog *_mainMenuDialog;
	virtual int runDialog(GUI::Dialog &dialog);

	const Common::String _targetName; // target name for saves

private:
	/**
	 * The pause level, 0 means 'running', a positive value indicates
	 * how often the engine has been paused (and hence how often it has
	 * to be un-paused before it resumes running). This makes it possible
	 * to nest code which pauses the engine.
	 */
	int _pauseLevel;

	/**
	 * The time when the pause was started.
	 */
	uint32 _pauseStartTime;

	/**
	 * The time when the engine was started. This value is used to calculate
	 * the current play time of the game running.
	 */
	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
	 * the menu loop, to avoid bugs like #2822778).
	 */
	int _saveSlotToLoad;

	/**
	 * Optional debugger for the engine
	 */
	GUI::Debugger *_debugger;
public:


	/**
	 * A feature in this context means an ability of the engine which can be
	 * either available or not.
	 * @see Engine::hasFeature()
	 */
	enum EngineFeature {
		/**
		 * Enables the subtitle speed and toggle items in the Options section
		 * of the global main menu.
		 */
		kSupportsSubtitleOptions,

		/**
		 * 'Return to launcher' feature is supported, i.e., EVENT_RETURN_TO_LAUNCHER is handled
		 * either directly, or indirectly (that is, the engine calls and honors
		 * the result of the Engine::shouldQuit() method appropriately).
		 */
		kSupportsReturnToLauncher,

		/**
		 * Loading savestates during runtime is supported, that is, this engine
		 * implements loadGameState() and canLoadGameStateCurrently().
		 * If this feature is supported, then the corresponding MetaEngine *must*
		 * support the kSupportsListSaves feature.
		 */
		kSupportsLoadingDuringRuntime,

		/**
		 * Loading savestates during runtime is supported, that is, this engine
		 * implements saveGameState() and canSaveGameStateCurrently().
		 * If this feature is supported, then the corresponding MetaEngine *must*
		 * support the kSupportsListSaves feature.
		 */
		kSupportsSavingDuringRuntime,

		/**
		 * Changing the game settings during runtime is supported. This enables
		 * showing the engine options tab in the config dialog accessed through
		 * the Global Main Menu.
		 */
		kSupportsChangingOptionsDuringRuntime
	};



	/** @name Overloadable methods
	 *
	 *  All Engine subclasses should consider overloading some or all of the following methods.
	 */
	//@{

	Engine(OSystem *syst);
	virtual ~Engine();

	/**
	 * Init SearchMan according to the game path.
	 *
	 * By default it adds the directory in non-flat mode with a depth of 4 as
	 * priority 0 to SearchMan.
	 *
	 * @param gamePath The base directory of the game data.
	 */
	virtual void initializePath(const Common::FSNode &gamePath);

	/**
	 * Init the engine and start its main loop.
	 * @return returns kNoError on success, else an error code.
	 */
	virtual Common::Error run() = 0;

	/**
	 * Prepare an error string, which is printed by the error() function.
	 */
	virtual void errorString(const char *buf_input, char *buf_output, int buf_output_size);

	/**
	 * Return the engine's debugger instance, if any.
	 */
	virtual GUI::Debugger *getDebugger() { return _debugger; }

	/**
	 * Sets the engine's debugger. Once set, the Engine class is responsible for managing
	 * the debugger, and freeing it on exit
	 */
	void setDebugger(GUI::Debugger *debugger) {
		assert(!_debugger);
		_debugger = debugger;
	}

	/**
	 * Return the engine's debugger instance, or create one if none is present.
	 * Used by error() to invoke the debugger when a severe error is reported.
	 */
	GUI::Debugger *getOrCreateDebugger();

	/**
	 * Determine whether the engine supports the specified feature.
	 */
	virtual bool hasFeature(EngineFeature f) const { return false; }

	/**
	 * Notify the engine that the sound settings in the config manager may have
	 * changed and that it hence should adjust any internal volume etc. values
	 * accordingly.
	 * The default implementation sets the volume levels of all mixer sound
	 * types according to the config entries of the active domain.
	 * When overwriting, call the default implementation first, then adjust the
	 * volumes further (if required).
	 *
	 * @note When setting volume levels, respect the "mute" config entry.
	 * @note The volume for the plain sound type is reset to the maximum
	 *       volume. If the engine can associate its own value for this
	 *       type, it needs to overwrite this member and set it accordingly.
	 * @todo find a better name for this
	 */
	virtual void syncSoundSettings();

	/**
	 * Notify the engine that the settings editable from the game tab in the
	 * in-game options dialog may have changed and that they need to be applied
	 * if necessary.
	 */
	virtual void applyGameSettings() {}

	/**
	 * Flip mute all sound option.
	 */
	virtual void flipMute();

	/**
	 * Generates the savegame filename
	 */
	virtual Common::String getSaveStateName(int slot) const {
		return Common::String::format("%s.%03d", _targetName.c_str(), slot);
	}

	/**
	 * Load a game state.
	 * @param slot	the slot from which a savestate should be loaded
	 * @return returns kNoError on success, else an error code.
	 */
	virtual Common::Error loadGameState(int slot);

	/**
	 * Load a game state.
	 * @param stream	the stream to load the savestate from
	 * @return returns kNoError on success, else an error code.
	 */
	virtual Common::Error loadGameStream(Common::SeekableReadStream *stream);

	/**
	 * Sets the game slot for a savegame to be loaded after global
	 * main menu execution. This is to avoid loading a savegame from
	 * inside the menu loop which causes bugs like #2822778.
	 *
	 * @param slot	the slot from which a savestate should be loaded.
	 */
	void setGameToLoadSlot(int slot);

	/**
	 * Indicates whether a game state can be loaded.
	 */
	virtual bool canLoadGameStateCurrently();

	/**
	 * Save a game state.
	 * @param slot	the slot into which the savestate should be stored
	 * @param desc	a description for the savestate, entered by the user
	 * @param isAutosave	Expected to be true if an autosave is being created
	 * @return returns kNoError on success, else an error code.
	 */
	virtual Common::Error saveGameState(int slot, const Common::String &desc, bool isAutosave = false);

	/**
	 * Save a game state.
	 * @param stream	The write stream to save the savegame data to
	 * @param isAutosave	Expected to be true if an autosave is being created
	 * @return returns kNoError on success, else an error code.
	 */
	virtual Common::Error saveGameStream(Common::WriteStream *stream, bool isAutosave = false);

	/**
	 * Indicates whether a game state can be saved.
	 */
	virtual bool canSaveGameStateCurrently();

	/**
	 * Shows the ScummVM save dialog, allowing users to save their game
	 */
	bool saveGameDialog();

	/**
	 * Shows the ScummVM Restore dialog, allowing users to load a game
	 */
	bool loadGameDialog();

protected:
	/**
	 * Actual implementation of pauseEngine by subclasses. See there
	 * for details.
	 */
	virtual void pauseEngineIntern(bool pause);

	//@}


public:

	/**
	 * Request the engine to quit. Sends a EVENT_QUIT event to the Event
	 * Manager.
	 */
	static void quitGame();

	/**
	 * Return whether the ENGINE should quit respectively should return to the
	 * launcher.
	 */
	static bool shouldQuit();

	static MetaEngine &getMetaEngine();

	/**
	 * Pause the engine. This should stop any audio playback
	 * and other stuff. Called right before the system runs a global dialog
	 * (like a global pause, main menu, options or 'confirm exit' dialog).
	 *
	 * Returns a PauseToken. Multiple pause tokens may exist. The engine will
	 * be resumed when all associated pause tokens reach the end of their lives.
	 */
	PauseToken pauseEngine();
private:
	/** Resume the engine. This should resume any audio playback and other stuff.
	*
	* Only PauseToken is allowed to call this member function. Use the PauseToken
	* that you got from pauseEngine to resume the engine.
	*/
	void resumeEngine();

	friend class PauseToken;

public:

	/**
	 * Return whether the engine is currently paused or not.
	 */
	bool isPaused() const { return _pauseLevel != 0; }

	/**
	 * Run the Global Main Menu Dialog
	 */
	void openMainMenuDialog();

	/**
	 * Display a warning to the user that the game is not fully supported.
	 *
	 * @return true if the user chose to start anyway, false otherwise
	 */
	static bool warnUserAboutUnsupportedGame();

	/**
	 * Get the total play time.
	 *
	 * @return How long the player has been playing in ms.
	 */
	uint32 getTotalPlayTime() const;

	/**
	 * Set the game time counter to the specified time.
	 *
	 * This can be used to set the play time counter after loading a savegame
	 * for example. Another use case is in case the engine wants to exclude
	 * time from the counter the user spent in original engine dialogs.
	 *
	 * @param time Play time to set up in ms.
	 */
	void setTotalPlayTime(uint32 time = 0);

	inline Common::TimerManager *getTimerManager() { return _timer; }
	inline Common::EventManager *getEventManager() { return _eventMan; }
	inline Common::SaveFileManager *getSaveFileManager() { return _saveFileMan; }

public:
	/** On some systems, check if the game appears to be run from CD. */
	void checkCD();


	/**
	 * Checks for whether it's time to do an autosave, and if so, does it.
	 */
	void handleAutoSave();

	/**
	 * Does an autosave immediately if autosaves are turned on
	 */
	void saveAutosaveIfEnabled();

	/**
	 * Indicates whether an autosave can currently be saved.
	 */
	virtual bool canSaveAutosaveCurrently() {
		return canSaveGameStateCurrently();
	}

	/**
	 * Returns the slot that should be used for autosaves
	 * @note	This should match the meta engine getAutosaveSlot() method
	 */
	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

/**
 * Singleton class which manages chained games. A chained game is one that
 * starts automatically, optionally loading a saved game, instead of returning
 * to the launcher.
 */
class ChainedGamesManager : public Common::Singleton<ChainedGamesManager> {
private:
	struct Game {
		Common::String target;
		int slot;
	};

	Common::Queue<Game> _chainedGames;

public:
	ChainedGamesManager();
	void clear();
	void push(const Common::String target, const int slot = -1);
	bool pop(Common::String &target, int &slot);
};

/** Convenience shortcut for accessing the chained games manager. */
#define ChainedGamesMan ChainedGamesManager::instance()

// FIXME: HACK for MidiEmu & error()
extern Engine *g_engine;

#endif