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 clearKeys();
136 void setAutofire(int key, bool enable);
137 void setAudioBufferSamples(int samples);
138 void setAudioSampleRate(unsigned rate);
139 void setAudioChannelEnabled(int channel, bool enable = true);
140 void startAudio();
141 void setVideoLayerEnabled(int layer, bool enable = true);
142 void setFPSTarget(float fps);
143 void loadState(int slot = 0);
144 void saveState(int slot = 0);
145 void loadBackupState();
146 void saveBackupState();
147 void setTurbo(bool, bool forced = true);
148 void setTurboSpeed(float ratio);
149 void setSync(bool);
150 void setAVStream(mAVStream*);
151 void clearAVStream();
152 void reloadAudioDriver();
153 void setSaveStateExtdata(int flags);
154 void setLoadStateExtdata(int flags);
155
156#ifdef USE_PNG
157 void screenshot();
158#endif
159
160 void setLuminanceValue(uint8_t value);
161 uint8_t luminanceValue() const { return m_luxValue; }
162 void setLuminanceLevel(int level);
163 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
164 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
165
166 void setRealTime();
167 void setFixedTime(const QDateTime& time);
168 void setFakeEpoch(const QDateTime& time);
169
170 void setLogLevel(int);
171 void enableLogLevel(int);
172 void disableLogLevel(int);
173
174private slots:
175 void openGame(bool bios = false);
176 void crashGame(const QString& crashMessage);
177 void cleanGame();
178
179 void pollEvents();
180 void updateAutofire();
181
182private:
183 void updateKeys();
184 void redoSamples(int samples);
185 void enableTurbo();
186
187 uint32_t* m_drawContext;
188 uint32_t* m_frontBuffer;
189 mCoreThread m_threadContext;
190 const mCoreConfig* m_config;
191 mCheatDevice* m_cheatDevice;
192 int m_activeKeys;
193 int m_activeButtons;
194 int m_inactiveKeys;
195 int m_logLevels;
196
197 bool m_gameOpen;
198
199 QString m_fname;
200 QString m_fsub;
201 VFile* m_vf;
202 QString m_bios;
203 bool m_useBios;
204 QString m_patch;
205 Override* m_override;
206
207 AudioProcessor* m_audioProcessor;
208
209 QAtomicInt m_pauseAfterFrame;
210 QList<std::function<void ()>> m_resetActions;
211
212 bool m_sync;
213 bool m_videoSync;
214 bool m_audioSync;
215 float m_fpsTarget;
216 bool m_turbo;
217 bool m_turboForced;
218 float m_turboSpeed;
219 bool m_wasPaused;
220
221 std::shared_ptr<mTileCache> m_tileCache;
222
223 bool m_audioChannels[6];
224 bool m_videoLayers[5];
225
226 bool m_autofire[GBA_KEY_MAX];
227 int m_autofireStatus[GBA_KEY_MAX];
228
229 int m_stateSlot;
230 struct VFile* m_backupLoadState;
231 QByteArray m_backupSaveState;
232 int m_saveStateFlags;
233 int m_loadStateFlags;
234
235 InputController* m_inputController;
236 MultiplayerController* m_multiplayer;
237
238 mAVStream* m_stream;
239
240 struct GameControllerLux : GBALuminanceSource {
241 GameController* p;
242 uint8_t value;
243 } m_lux;
244 uint8_t m_luxValue;
245 int m_luxLevel;
246};
247
248}
249
250#endif