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 cursorLocation(int x, int y);
136 void cursorDown(bool);
137 void clearKeys();
138 void setAutofire(int key, bool enable);
139 void setAudioBufferSamples(int samples);
140 void setAudioSampleRate(unsigned rate);
141 void setAudioChannelEnabled(int channel, bool enable = true);
142 void startAudio();
143 void setVideoLayerEnabled(int layer, bool enable = true);
144 void setFPSTarget(float fps);
145 void loadState(int slot = 0);
146 void saveState(int slot = 0);
147 void loadBackupState();
148 void saveBackupState();
149 void setTurbo(bool, bool forced = true);
150 void setTurboSpeed(float ratio);
151 void setSync(bool);
152 void setAudioSync(bool);
153 void setVideoSync(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 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