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