1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GLK_GLK_H
24 #define GLK_GLK_H
25 
26 #include "common/scummsys.h"
27 #include "common/random.h"
28 #include "common/system.h"
29 #include "common/serializer.h"
30 #include "engines/engine.h"
31 #include "glk/debugger.h"
32 #include "glk/glk_types.h"
33 #include "glk/streams.h"
34 #include "glk/pc_speaker.h"
35 
36 namespace Glk {
37 
38 class Clipboard;
39 class Blorb;
40 class Conf;
41 class Events;
42 class Pictures;
43 class Screen;
44 class Selection;
45 class Sounds;
46 class Streams;
47 class Windows;
48 
49 enum GlkDebugChannels {
50 	kDebugCore      = 1 << 0,
51 	kDebugScripts   = 1 << 1,
52 	kDebugGraphics  = 1 << 2,
53 	kDebugSound     = 1 << 3
54 };
55 
56 
57 #define GLK_SAVEGAME_VERSION 1
58 
59 struct GlkGameDescription {
60 	Common::String _gameId;
61 	Common::Language _language;
62 	Common::Platform _platform;
63 	Common::String _filename;
64 	Common::String _md5;
65 	uint _options;
66 };
67 
68 /**
69  * Base class for the different interpreters
70  */
71 class GlkEngine : public Engine {
72 private:
73 	/**
74 	 * Handles basic initialization
75 	 */
76 	void initialize();
77 protected:
78 	const GlkGameDescription _gameDescription;
79 	Common::RandomSource _random;
80 	int _loadSaveSlot;
81 	Common::File _gameFile;
82 	PCSpeaker *_pcSpeaker;
83 
84 	// Engine APIs
85 	virtual Common::Error run();
86 
87 	/**
88 	  * Returns true whether a given feature is supported by the engine
89 	  */
90 	virtual bool hasFeature(EngineFeature f) const override;
91 
92 	/**
93 	 * Setup the video mode
94 	 */
95 	virtual void initGraphicsMode();
96 
97 	/**
98 	 * Create the screen
99 	 */
100 	virtual Screen *createScreen();
101 
102 	/**
103 	 * Creates a debugger instance
104 	 */
createDebugger()105 	virtual Debugger *createDebugger() {
106 		return new Debugger();
107 	}
108 
109 	/**
110 	 * Main game loop for the individual interpreters
111 	 */
112 	virtual void runGame() = 0;
113 public:
114 	Blorb *_blorb;
115 	Clipboard *_clipboard;
116 	Conf *_conf;
117 	Debugger *_debugger;
118 	Events *_events;
119 	Pictures *_pictures;
120 	Screen *_screen;
121 	Selection *_selection;
122 	Streams *_streams;
123 	Sounds *_sounds;
124 	Windows *_windows;
125 	bool _copySelect;
126 	bool _terminated;
127 
128 	gidispatch_rock_t(*gli_register_obj)(void *obj, uint objclass);
129 	void(*gli_unregister_obj)(void *obj, uint objclass, gidispatch_rock_t objrock);
130 	gidispatch_rock_t(*gli_register_arr)(void *array, uint len, const char *typecode);
131 	void(*gli_unregister_arr)(void *array, uint len, const char *typecode, gidispatch_rock_t objrock);
132 public:
133 	GlkEngine(OSystem *syst, const GlkGameDescription &gameDesc);
134 	virtual ~GlkEngine();
135 
136 	/**
137 	 * Returns true if a savegame can be loaded
138 	 */
canLoadGameStateCurrently()139 	virtual bool canLoadGameStateCurrently() override {
140 		return true;
141 	}
142 
143 	/**
144 	 * Returns true if the game can be saved
145 	 */
canSaveGameStateCurrently()146 	virtual bool canSaveGameStateCurrently() override {
147 		return true;
148 	}
149 
150 	/**
151 	 * Returns the language
152 	 */
getLanguage()153 	Common::Language getLanguage() const { return _gameDescription._language; };
154 
155 	/**
156 	 * Returns the running interpreter type
157 	 */
158 	virtual InterpreterType getInterpreterType() const = 0;
159 
160 	/**
161 	 * Returns the game's Id
162 	 */
getGameID()163 	const Common::String &getGameID() const { return _gameDescription._gameId; }
164 
165 	/**
166 	 * Returns the game's md5
167 	 */
getGameMD5()168 	const Common::String &getGameMD5() const { return _gameDescription._md5; }
169 
170 	/**
171 	 * Returns the primary filename for the game
172 	 */
getFilename()173 	const Common::String &getFilename() const { return _gameDescription._filename; }
174 
175 	/**
176 	 * Returns any options returned with the game's detection entry
177 	 */
getOptions()178 	uint getOptions() const { return _gameDescription._options; }
179 
180 	/**
181 	 * Return the game engine's target name
182 	 */
getTargetName()183 	const Common::String &getTargetName() const {
184 		return _targetName;
185 	}
186 
187 	/**
188 	 * Return the filename for a given save slot
189 	 */
getSaveName(uint slot)190 	Common::String getSaveName(uint slot) const {
191 		return Common::String::format("%s.%.3u", getTargetName().c_str(), slot);
192 	}
193 
194 	/**
195 	 * Prompt the user for a savegame to load, and then load it
196 	 */
197 	Common::Error loadGame();
198 
199 	/**
200 	 * Prompt the user to save their game, and then save it
201 	 */
202 	Common::Error saveGame();
203 
204 	/**
205 	 * Load a savegame from a given slot
206 	 */
207 	virtual Common::Error loadGameState(int slot) override;
208 
209 	/**
210 	 * Save the game to a given slot
211 	 */
212 	virtual Common::Error saveGameState(int slot, const Common::String &desc) override;
213 
214 	/**
215 	 * Load a savegame from the passed Quetzal file chunk stream
216 	 */
217 	virtual Common::Error readSaveData(Common::SeekableReadStream *rs) = 0;
218 
219 	/**
220 	 * Save the game. The passed write stream represents access to the UMem chunk
221 	 * in the Quetzal save file that will be created
222 	 */
223 	virtual Common::Error writeGameData(Common::WriteStream *ws) = 0;
224 
225 	/**
226 	 * Updates sound settings
227 	 */
228 	virtual void syncSoundSettings() override;
229 
230 	/**
231 	 * Generate a beep
232 	 */
233 	void beep();
234 
235 	/**
236 	 * Get a random number
237 	 */
getRandomNumber(uint max)238 	uint getRandomNumber(uint max) { return _random.getRandomNumber(max); }
239 
240 	/**
241 	 * Set a random number seed
242 	 */
setRandomNumberSeed(uint seed)243 	void setRandomNumberSeed(uint seed) { _random.setSeed(seed); }
244 };
245 
246 extern GlkEngine *g_vm;
247 
248 } // End of namespace Glk
249 
250 #endif
251