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
33class QThread;
34
35namespace QGBA {
36
37class AudioProcessor;
38class InputController;
39class MultiplayerController;
40class Override;
41
42class GameController : public QObject {
43Q_OBJECT
44
45public:
46 static const bool VIDEO_SYNC = false;
47 static const bool AUDIO_SYNC = true;
48
49 class Interrupter {
50 public:
51 Interrupter(GameController*, bool fromThread = false);
52 ~Interrupter();
53
54 private:
55 GameController* m_parent;
56 bool m_fromThread;
57 };
58
59 GameController(QObject* parent = nullptr);
60 ~GameController();
61
62 const uint32_t* drawContext() const { return m_drawContext; }
63 mCoreThread* thread() { return &m_threadContext; }
64 mCheatDevice* cheatDevice() { return m_threadContext.core ? m_threadContext.core->cheatDevice(m_threadContext.core) : nullptr; }
65
66 void threadInterrupt();
67 void threadContinue();
68
69 bool isPaused();
70 bool isLoaded() { return m_gameOpen && mCoreThreadIsActive(&m_threadContext); }
71 mPlatform platform() const;
72
73 bool audioSync() const { return m_audioSync; }
74 bool videoSync() const { return m_videoSync; }
75 QSize screenDimensions() const;
76
77 void setInputController(InputController* controller) { m_inputController = controller; }
78
79 void setMultiplayerController(MultiplayerController* controller);
80 MultiplayerController* multiplayerController() { return m_multiplayer; }
81 void clearMultiplayerController();
82
83 void setOverride(Override* override);
84 Override* override() { return m_override; }
85 void clearOverride();
86
87 void setConfig(const mCoreConfig*);
88
89 int stateSlot() const { return m_stateSlot; }
90
91#ifdef USE_GDB_STUB
92 mDebugger* debugger();
93 void setDebugger(mDebugger*);
94#endif
95
96 std::shared_ptr<mTileCache> tileCache();
97
98signals:
99 void frameAvailable(const uint32_t*);
100 void gameStarted(mCoreThread*, const QString& fname);
101 void gameStopped(mCoreThread*);
102 void gamePaused(mCoreThread*);
103 void gameUnpaused(mCoreThread*);
104 void gameCrashed(const QString& errorMessage);
105 void gameFailed();
106 void stateLoaded(mCoreThread*);
107 void rewound(mCoreThread*);
108 void unimplementedBiosCall(int);
109
110 void luminanceValueChanged(int);
111
112 void statusPosted(const QString& message);
113 void postLog(int level, int category, const QString& log);
114
115public slots:
116 void loadGame(const QString& path);
117 void loadGame(VFile* vf, const QString& path, const QString& base);
118 void loadBIOS(int platform, const QString& path);
119 void loadSave(const QString& path, bool temporary = true);
120 void yankPak();
121 void replaceGame(const QString& path);
122 void setUseBIOS(bool);
123 void loadPatch(const QString& path);
124 void importSharkport(const QString& path);
125 void exportSharkport(const QString& path);
126 void bootBIOS();
127 void closeGame();
128 void setPaused(bool paused);
129 void reset();
130 void frameAdvance();
131 void setRewind(bool enable, int capacity, bool rewindSave);
132 void rewind(int states = 0);
133 void startRewinding();
134 void stopRewinding();
135 void keyPressed(int key);
136 void keyReleased(int key);
137 void cursorLocation(int x, int y);
138 void cursorDown(bool);
139 void clearKeys();
140 void setAutofire(int key, bool enable);
141 void setAudioBufferSamples(int samples);
142 void setAudioSampleRate(unsigned rate);
143 void setAudioChannelEnabled(int channel, bool enable = true);
144 void startAudio();
145 void setVideoLayerEnabled(int layer, bool enable = true);
146 void setFPSTarget(float fps);
147 void loadState(int slot = 0);
148 void saveState(int slot = 0);
149 void loadBackupState();
150 void saveBackupState();
151 void setTurbo(bool, bool forced = true);
152 void setTurboSpeed(float ratio);
153 void setSync(bool);
154 void setAVStream(mAVStream*);
155 void clearAVStream();
156 void reloadAudioDriver();
157 void setSaveStateExtdata(int flags);
158 void setLoadStateExtdata(int flags);
159
160#ifdef USE_PNG
161 void screenshot();
162#endif
163
164 void setLuminanceValue(uint8_t value);
165 uint8_t luminanceValue() const { return m_luxValue; }
166 void setLuminanceLevel(int level);
167 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
168 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
169
170 void setRealTime();
171 void setFixedTime(const QDateTime& time);
172 void setFakeEpoch(const QDateTime& time);
173
174 void setLogLevel(int);
175 void enableLogLevel(int);
176 void disableLogLevel(int);
177
178private slots:
179 void openGame(bool bios = false);
180 void crashGame(const QString& crashMessage);
181 void cleanGame();
182
183 void pollEvents();
184 void updateAutofire();
185
186private:
187 void updateKeys();
188 void redoSamples(int samples);
189 void enableTurbo();
190
191 uint32_t* m_drawContext;
192 uint32_t* m_frontBuffer;
193 mCoreThread m_threadContext;
194 const mCoreConfig* m_config;
195 mCheatDevice* m_cheatDevice;
196 int m_activeKeys;
197 int m_activeButtons;
198 int m_inactiveKeys;
199 int m_logLevels;
200
201 bool m_gameOpen;
202
203 QString m_fname;
204 QString m_fsub;
205 VFile* m_vf;
206 QString m_bios;
207 bool m_useBios;
208 QString m_patch;
209 Override* m_override;
210
211 QThread* m_audioThread;
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 bool m_audioChannels[6];
229 bool m_videoLayers[5];
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 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