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
18#include <mgba/core/core.h>
19#include <mgba/core/thread.h>
20#include <mgba/gba/interface.h>
21#include <mgba/internal/gba/input.h>
22#ifdef BUILD_SDL
23#include "platform/sdl/sdl-events.h"
24#endif
25
26struct Configuration;
27struct GBAAudio;
28struct mCoreConfig;
29struct mDebugger;
30struct mTileCache;
31
32class QThread;
33
34namespace QGBA {
35
36class AudioProcessor;
37class InputController;
38class MultiplayerController;
39class Override;
40
41class GameController : public QObject {
42Q_OBJECT
43
44public:
45 static const bool VIDEO_SYNC = false;
46 static const bool AUDIO_SYNC = true;
47
48 class Interrupter {
49 public:
50 Interrupter(GameController*, bool fromThread = false);
51 ~Interrupter();
52
53 private:
54 GameController* m_parent;
55 bool m_fromThread;
56 };
57
58 GameController(QObject* parent = nullptr);
59 ~GameController();
60
61 const uint32_t* drawContext() const { return m_drawContext; }
62 mCoreThread* thread() { return &m_threadContext; }
63 mCheatDevice* cheatDevice() { return m_threadContext.core ? m_threadContext.core->cheatDevice(m_threadContext.core) : nullptr; }
64
65 void threadInterrupt();
66 void threadContinue();
67
68 bool isPaused();
69 bool isLoaded() { return m_gameOpen && mCoreThreadIsActive(&m_threadContext); }
70 mPlatform platform() const;
71
72 bool audioSync() const { return m_audioSync; }
73 bool videoSync() const { return m_videoSync; }
74 QSize screenDimensions() const;
75
76 void setInputController(InputController* controller) { m_inputController = controller; }
77
78 void setMultiplayerController(MultiplayerController* controller);
79 MultiplayerController* multiplayerController() { return m_multiplayer; }
80 void clearMultiplayerController();
81
82 void setOverride(Override* override);
83 Override* override() { return m_override; }
84 void clearOverride();
85
86 void setConfig(const mCoreConfig*);
87
88 int stateSlot() const { return m_stateSlot; }
89
90#ifdef USE_GDB_STUB
91 mDebugger* debugger();
92 void setDebugger(mDebugger*);
93#endif
94
95 std::shared_ptr<mTileCache> tileCache();
96
97signals:
98 void frameAvailable(const uint32_t*);
99 void gameStarted(mCoreThread*, const QString& fname);
100 void gameStopped(mCoreThread*);
101 void gamePaused(mCoreThread*);
102 void gameUnpaused(mCoreThread*);
103 void gameCrashed(const QString& errorMessage);
104 void gameFailed();
105 void stateLoaded(mCoreThread*);
106 void rewound(mCoreThread*);
107 void unimplementedBiosCall(int);
108
109 void luminanceValueChanged(int);
110
111 void statusPosted(const QString& message);
112 void postLog(int level, int category, const QString& log);
113
114public slots:
115 void loadGame(const QString& path);
116 void loadGame(VFile* vf, const QString& path, const QString& base);
117 void loadBIOS(int platform, const QString& path);
118 void loadSave(const QString& path, bool temporary = true);
119 void yankPak();
120 void replaceGame(const QString& path);
121 void setUseBIOS(bool);
122 void loadPatch(const QString& path);
123 void importSharkport(const QString& path);
124 void exportSharkport(const QString& path);
125 void bootBIOS();
126 void closeGame();
127 void setPaused(bool paused);
128 void reset();
129 void frameAdvance();
130 void setRewind(bool enable, int capacity, bool rewindSave);
131 void rewind(int states = 0);
132 void startRewinding();
133 void stopRewinding();
134 void keyPressed(int key);
135 void keyReleased(int key);
136 void cursorLocation(int x, int y);
137 void cursorDown(bool);
138 void clearKeys();
139 void setAutofire(int key, bool enable);
140 void setAudioBufferSamples(int samples);
141 void setAudioSampleRate(unsigned rate);
142 void setAudioChannelEnabled(int channel, bool enable = true);
143 void startAudio();
144 void setVideoLayerEnabled(int layer, bool enable = true);
145 void setFPSTarget(float fps);
146 void loadState(int slot = 0);
147 void saveState(int slot = 0);
148 void loadBackupState();
149 void saveBackupState();
150 void setTurbo(bool, bool forced = true);
151 void setTurboSpeed(float ratio);
152 void setSync(bool);
153 void setAVStream(mAVStream*);
154 void clearAVStream();
155 void reloadAudioDriver();
156 void setSaveStateExtdata(int flags);
157 void setLoadStateExtdata(int flags);
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 QThread* m_audioThread;
211 AudioProcessor* m_audioProcessor;
212
213 QAtomicInt m_pauseAfterFrame;
214 QList<std::function<void ()>> m_resetActions;
215
216 bool m_sync;
217 bool m_videoSync;
218 bool m_audioSync;
219 float m_fpsTarget;
220 bool m_turbo;
221 bool m_turboForced;
222 float m_turboSpeed;
223 bool m_wasPaused;
224
225 std::shared_ptr<mTileCache> m_tileCache;
226
227 bool m_audioChannels[6];
228 bool m_videoLayers[5];
229
230 bool m_autofire[GBA_KEY_MAX];
231 int m_autofireStatus[GBA_KEY_MAX];
232
233 int m_stateSlot;
234 struct VFile* m_backupLoadState;
235 QByteArray m_backupSaveState;
236 int m_saveStateFlags;
237 int m_loadStateFlags;
238
239 InputController* m_inputController;
240 MultiplayerController* m_multiplayer;
241
242 mAVStream* m_stream;
243
244 struct GameControllerLux : GBALuminanceSource {
245 GameController* p;
246 uint8_t value;
247 } m_lux;
248 uint8_t m_luxValue;
249 int m_luxLevel;
250};
251
252}
253
254#endif