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