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
158#ifdef USE_PNG
159 void screenshot();
160#endif
161
162 void setLuminanceValue(uint8_t value);
163 uint8_t luminanceValue() const { return m_luxValue; }
164 void setLuminanceLevel(int level);
165 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
166 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
167
168 void setRealTime();
169 void setFixedTime(const QDateTime& time);
170 void setFakeEpoch(const QDateTime& time);
171
172 void setLogLevel(int);
173 void enableLogLevel(int);
174 void disableLogLevel(int);
175
176private slots:
177 void openGame(bool bios = false);
178 void crashGame(const QString& crashMessage);
179 void cleanGame();
180
181 void pollEvents();
182 void updateAutofire();
183
184private:
185 void updateKeys();
186 void redoSamples(int samples);
187 void enableTurbo();
188
189 uint32_t* m_drawContext;
190 uint32_t* m_frontBuffer;
191 mCoreThread m_threadContext;
192 const mCoreConfig* m_config;
193 mCheatDevice* m_cheatDevice;
194 int m_activeKeys;
195 int m_activeButtons;
196 int m_inactiveKeys;
197 int m_logLevels;
198
199 bool m_gameOpen;
200
201 QString m_fname;
202 QString m_fsub;
203 VFile* m_vf;
204 QString m_bios;
205 bool m_useBios;
206 QString m_patch;
207 Override* m_override;
208
209 AudioProcessor* m_audioProcessor;
210
211 QAtomicInt m_pauseAfterFrame;
212 QList<std::function<void ()>> m_resetActions;
213
214 bool m_sync;
215 bool m_videoSync;
216 bool m_audioSync;
217 float m_fpsTarget;
218 bool m_turbo;
219 bool m_turboForced;
220 float m_turboSpeed;
221 bool m_wasPaused;
222
223 std::shared_ptr<mTileCache> m_tileCache;
224
225 bool m_audioChannels[6];
226 bool m_videoLayers[5];
227
228 bool m_autofire[GBA_KEY_MAX];
229 int m_autofireStatus[GBA_KEY_MAX];
230
231 int m_stateSlot;
232 struct VFile* m_backupLoadState;
233 QByteArray m_backupSaveState;
234 int m_saveStateFlags;
235 int m_loadStateFlags;
236
237 InputController* m_inputController;
238 MultiplayerController* m_multiplayer;
239
240 mAVStream* m_stream;
241
242 struct GameControllerLux : GBALuminanceSource {
243 GameController* p;
244 uint8_t value;
245 } m_lux;
246 uint8_t m_luxValue;
247 int m_luxLevel;
248};
249
250}
251
252#endif