src/platform/qt/GameController.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef QGBA_GAME_CONTROLLER
7#define QGBA_GAME_CONTROLLER
8
9#include <QAtomicInt>
10#include <QFile>
11#include <QImage>
12#include <QObject>
13#include <QString>
14#include <QTimer>
15
16#include <memory>
17#include <functional>
18
19#include <mgba/core/core.h>
20#include <mgba/core/thread.h>
21#include <mgba/gba/interface.h>
22#include <mgba/internal/gba/input.h>
23#ifdef BUILD_SDL
24#include "platform/sdl/sdl-events.h"
25#endif
26
27struct Configuration;
28struct GBAAudio;
29struct mCoreConfig;
30struct mDebugger;
31struct mTileCache;
32
33namespace QGBA {
34
35class AudioProcessor;
36class InputController;
37class MultiplayerController;
38class Override;
39
40class GameController : public QObject {
41Q_OBJECT
42
43public:
44 static const bool VIDEO_SYNC = false;
45 static const bool AUDIO_SYNC = true;
46
47 class Interrupter {
48 public:
49 Interrupter(GameController*, bool fromThread = false);
50 ~Interrupter();
51
52 private:
53 GameController* m_parent;
54 bool m_fromThread;
55 };
56
57 GameController(QObject* parent = nullptr);
58 ~GameController();
59
60 const uint32_t* drawContext() const { return m_drawContext; }
61 mCoreThread* thread() { return &m_threadContext; }
62 mCheatDevice* cheatDevice() { return m_threadContext.core ? m_threadContext.core->cheatDevice(m_threadContext.core) : nullptr; }
63
64 void threadInterrupt();
65 void threadContinue();
66
67 bool isPaused();
68 bool isLoaded() { return m_gameOpen && mCoreThreadIsActive(&m_threadContext); }
69 mPlatform platform() const;
70
71 bool audioSync() const { return m_audioSync; }
72 bool videoSync() const { return m_videoSync; }
73 QSize screenDimensions() const;
74
75 void setInputController(InputController* controller) { m_inputController = controller; }
76
77 void setMultiplayerController(MultiplayerController* controller);
78 MultiplayerController* multiplayerController() { return m_multiplayer; }
79 void clearMultiplayerController();
80
81 void setOverride(Override* override);
82 Override* override() { return m_override; }
83 void clearOverride();
84
85 void setConfig(const mCoreConfig*);
86
87 int stateSlot() const { return m_stateSlot; }
88
89#ifdef USE_GDB_STUB
90 mDebugger* debugger();
91 void setDebugger(mDebugger*);
92#endif
93
94 std::shared_ptr<mTileCache> tileCache();
95
96signals:
97 void frameAvailable(const uint32_t*);
98 void gameStarted(mCoreThread*, const QString& fname);
99 void gameStopped(mCoreThread*);
100 void gamePaused(mCoreThread*);
101 void gameUnpaused(mCoreThread*);
102 void gameCrashed(const QString& errorMessage);
103 void gameFailed();
104 void stateLoaded(mCoreThread*);
105 void rewound(mCoreThread*);
106 void unimplementedBiosCall(int);
107
108 void luminanceValueChanged(int);
109
110 void statusPosted(const QString& message);
111 void postLog(int level, int category, const QString& log);
112
113public slots:
114 void loadGame(const QString& path);
115 void loadGame(VFile* vf, const QString& path, const QString& base);
116 void loadBIOS(int platform, const QString& path);
117 void loadSave(const QString& path, bool temporary = true);
118 void yankPak();
119 void replaceGame(const QString& path);
120 void setUseBIOS(bool);
121 void loadPatch(const QString& path);
122 void importSharkport(const QString& path);
123 void exportSharkport(const QString& path);
124 void bootBIOS();
125 void closeGame();
126 void setPaused(bool paused);
127 void reset();
128 void frameAdvance();
129 void setRewind(bool enable, int capacity, bool rewindSave);
130 void rewind(int states = 0);
131 void startRewinding();
132 void stopRewinding();
133 void keyPressed(int key);
134 void keyReleased(int key);
135 void cursorLocation(int x, int y);
136 void cursorDown(bool);
137 void clearKeys();
138 void setAutofire(int key, bool enable);
139 void setAudioBufferSamples(int samples);
140 void setAudioSampleRate(unsigned rate);
141 void setAudioChannelEnabled(int channel, bool enable = true);
142 void startAudio();
143 void setVideoLayerEnabled(int layer, bool enable = true);
144 void setFPSTarget(float fps);
145 void loadState(int slot = 0);
146 void saveState(int slot = 0);
147 void loadBackupState();
148 void saveBackupState();
149 void setTurbo(bool, bool forced = true);
150 void setTurboSpeed(float ratio);
151 void setSync(bool);
152 void setAudioSync(bool);
153 void setVideoSync(bool);
154 void setAVStream(mAVStream*);
155 void clearAVStream();
156 void reloadAudioDriver();
157 void setSaveStateExtdata(int flags);
158 void setLoadStateExtdata(int flags);
159 void setPreload(bool);
160
161#ifdef USE_PNG
162 void screenshot();
163#endif
164
165 void setLuminanceValue(uint8_t value);
166 uint8_t luminanceValue() const { return m_luxValue; }
167 void setLuminanceLevel(int level);
168 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
169 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
170
171 void setRealTime();
172 void setFixedTime(const QDateTime& time);
173 void setFakeEpoch(const QDateTime& time);
174
175 void setLogLevel(int);
176 void enableLogLevel(int);
177 void disableLogLevel(int);
178
179private slots:
180 void openGame(bool bios = false);
181 void crashGame(const QString& crashMessage);
182 void cleanGame();
183
184 void pollEvents();
185 void updateAutofire();
186
187private:
188 void updateKeys();
189 void redoSamples(int samples);
190 void enableTurbo();
191
192 uint32_t* m_drawContext;
193 uint32_t* m_frontBuffer;
194 mCoreThread m_threadContext;
195 const mCoreConfig* m_config;
196 mCheatDevice* m_cheatDevice;
197 int m_activeKeys;
198 int m_activeButtons;
199 int m_inactiveKeys;
200 int m_logLevels;
201
202 bool m_gameOpen;
203
204 QString m_fname;
205 QString m_fsub;
206 VFile* m_vf;
207 QString m_bios;
208 bool m_useBios;
209 QString m_patch;
210 Override* m_override;
211
212 AudioProcessor* m_audioProcessor;
213
214 QAtomicInt m_pauseAfterFrame;
215 QList<std::function<void ()>> m_resetActions;
216
217 bool m_sync;
218 bool m_videoSync;
219 bool m_audioSync;
220 float m_fpsTarget;
221 bool m_turbo;
222 bool m_turboForced;
223 float m_turboSpeed;
224 bool m_wasPaused;
225
226 std::shared_ptr<mTileCache> m_tileCache;
227
228 QList<bool> m_audioChannels;
229 QList<bool> m_videoLayers;
230
231 bool m_autofire[GBA_KEY_MAX];
232 int m_autofireStatus[GBA_KEY_MAX];
233
234 int m_stateSlot;
235 struct VFile* m_backupLoadState;
236 QByteArray m_backupSaveState;
237 int m_saveStateFlags;
238 int m_loadStateFlags;
239
240 bool m_preload;
241
242 InputController* m_inputController;
243 MultiplayerController* m_multiplayer;
244
245 mAVStream* m_stream;
246
247 struct GameControllerLux : GBALuminanceSource {
248 GameController* p;
249 uint8_t value;
250 } m_lux;
251 uint8_t m_luxValue;
252 int m_luxLevel;
253};
254
255}
256
257#endif