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 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 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 QThread* m_audioThread;
210 AudioProcessor* m_audioProcessor;
211
212 QAtomicInt m_pauseAfterFrame;
213 QList<std::function<void ()>> m_resetActions;
214
215 bool m_sync;
216 bool m_videoSync;
217 bool m_audioSync;
218 float m_fpsTarget;
219 bool m_turbo;
220 bool m_turboForced;
221 float m_turboSpeed;
222 bool m_wasPaused;
223
224 std::shared_ptr<mTileCache> m_tileCache;
225
226 bool m_audioChannels[6];
227 bool m_videoLayers[5];
228
229 bool m_autofire[GBA_KEY_MAX];
230 int m_autofireStatus[GBA_KEY_MAX];
231
232 int m_stateSlot;
233 struct VFile* m_backupLoadState;
234 QByteArray m_backupSaveState;
235 int m_saveStateFlags;
236 int m_loadStateFlags;
237
238 InputController* m_inputController;
239 MultiplayerController* m_multiplayer;
240
241 mAVStream* m_stream;
242
243 struct GameControllerLux : GBALuminanceSource {
244 GameController* p;
245 uint8_t value;
246 } m_lux;
247 uint8_t m_luxValue;
248 int m_luxLevel;
249};
250
251}
252
253#endif