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 clearKeys();
136 void setAutofire(int key, bool enable);
137 void setAudioBufferSamples(int samples);
138 void setAudioSampleRate(unsigned rate);
139 void setAudioChannelEnabled(int channel, bool enable = true);
140 void startAudio();
141 void setVideoLayerEnabled(int layer, bool enable = true);
142 void setFPSTarget(float fps);
143 void loadState(int slot = 0);
144 void saveState(int slot = 0);
145 void loadBackupState();
146 void saveBackupState();
147 void setTurbo(bool, bool forced = true);
148 void setTurboSpeed(float ratio);
149 void setSync(bool);
150 void setAudioSync(bool);
151 void setVideoSync(bool);
152 void setAVStream(mAVStream*);
153 void clearAVStream();
154 void reloadAudioDriver();
155 void setSaveStateExtdata(int flags);
156 void setLoadStateExtdata(int flags);
157 void setPreload(bool);
158
159#ifdef USE_PNG
160 void screenshot();
161#endif
162
163 void setLuminanceValue(uint8_t value);
164 uint8_t luminanceValue() const { return m_luxValue; }
165 void setLuminanceLevel(int level);
166 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
167 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
168
169 void setRealTime();
170 void setFixedTime(const QDateTime& time);
171 void setFakeEpoch(const QDateTime& time);
172
173 void setLogLevel(int);
174 void enableLogLevel(int);
175 void disableLogLevel(int);
176
177private slots:
178 void openGame(bool bios = false);
179 void crashGame(const QString& crashMessage);
180 void cleanGame();
181
182 void pollEvents();
183 void updateAutofire();
184
185private:
186 void updateKeys();
187 void redoSamples(int samples);
188 void enableTurbo();
189
190 uint32_t* m_drawContext;
191 uint32_t* m_frontBuffer;
192 mCoreThread m_threadContext;
193 const mCoreConfig* m_config;
194 mCheatDevice* m_cheatDevice;
195 int m_activeKeys;
196 int m_activeButtons;
197 int m_inactiveKeys;
198 int m_logLevels;
199
200 bool m_gameOpen;
201
202 QString m_fname;
203 QString m_fsub;
204 VFile* m_vf;
205 QString m_bios;
206 bool m_useBios;
207 QString m_patch;
208 Override* m_override;
209
210 AudioProcessor* m_audioProcessor;
211
212 QAtomicInt m_pauseAfterFrame;
213 QList<std::function<void ()>> m_resetActions;
214
215 bool m_sync;
216 bool m_videoSync;
217 bool m_audioSync;
218 float m_fpsTarget;
219 bool m_turbo;
220 bool m_turboForced;
221 float m_turboSpeed;
222 bool m_wasPaused;
223
224 std::shared_ptr<mTileCache> m_tileCache;
225
226 QList<bool> m_audioChannels;
227 QList<bool> m_videoLayers;
228
229 bool m_autofire[GBA_KEY_MAX];
230 int m_autofireStatus[GBA_KEY_MAX];
231
232 int m_stateSlot;
233 struct VFile* m_backupLoadState;
234 QByteArray m_backupSaveState;
235 int m_saveStateFlags;
236 int m_loadStateFlags;
237
238 bool m_preload;
239
240 InputController* m_inputController;
241 MultiplayerController* m_multiplayer;
242
243 mAVStream* m_stream;
244
245 struct GameControllerLux : GBALuminanceSource {
246 GameController* p;
247 uint8_t value;
248 } m_lux;
249 uint8_t m_luxValue;
250 int m_luxLevel;
251};
252
253}
254
255#endif