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
18extern "C" {
19#include "core/core.h"
20#include "core/thread.h"
21#include "gba/cheats.h"
22#include "gba/hardware.h"
23#include "gba/input.h"
24#include "gba/overrides.h"
25#ifdef BUILD_SDL
26#include "platform/sdl/sdl-events.h"
27#endif
28}
29
30struct Configuration;
31struct GBAAudio;
32struct mCoreConfig;
33struct mDebugger;
34struct mTileCache;
35
36class QThread;
37
38namespace QGBA {
39
40class AudioProcessor;
41class InputController;
42class MultiplayerController;
43class Override;
44
45class GameController : public QObject {
46Q_OBJECT
47
48public:
49 static const bool VIDEO_SYNC = false;
50 static const bool AUDIO_SYNC = true;
51
52 class Interrupter {
53 public:
54 Interrupter(GameController*, bool fromThread = false);
55 ~Interrupter();
56
57 private:
58 GameController* m_parent;
59 bool m_fromThread;
60 };
61
62 GameController(QObject* parent = nullptr);
63 ~GameController();
64
65 const uint32_t* drawContext() const { return m_drawContext; }
66 mCoreThread* thread() { return &m_threadContext; }
67 mCheatDevice* cheatDevice() { return m_threadContext.core ? m_threadContext.core->cheatDevice(m_threadContext.core) : nullptr; }
68
69 void threadInterrupt();
70 void threadContinue();
71
72 bool isPaused();
73 bool isLoaded() { return m_gameOpen && mCoreThreadIsActive(&m_threadContext); }
74 mPlatform platform() const;
75
76 bool audioSync() const { return m_audioSync; }
77 bool videoSync() const { return m_videoSync; }
78 QSize screenDimensions() const;
79
80 void setInputController(InputController* controller) { m_inputController = controller; }
81
82 void setMultiplayerController(MultiplayerController* controller);
83 MultiplayerController* multiplayerController() { return m_multiplayer; }
84 void clearMultiplayerController();
85
86 void setOverride(Override* override);
87 Override* override() { return m_override; }
88 void clearOverride();
89
90 void setConfig(const mCoreConfig*);
91
92 int stateSlot() const { return m_stateSlot; }
93
94#ifdef USE_GDB_STUB
95 mDebugger* debugger();
96 void setDebugger(mDebugger*);
97#endif
98
99 std::shared_ptr<mTileCache> tileCache();
100
101signals:
102 void frameAvailable(const uint32_t*);
103 void gameStarted(mCoreThread*, const QString& fname);
104 void gameStopped(mCoreThread*);
105 void gamePaused(mCoreThread*);
106 void gameUnpaused(mCoreThread*);
107 void gameCrashed(const QString& errorMessage);
108 void gameFailed();
109 void stateLoaded(mCoreThread*);
110 void rewound(mCoreThread*);
111 void unimplementedBiosCall(int);
112
113 void luminanceValueChanged(int);
114
115 void statusPosted(const QString& message);
116 void postLog(int level, int category, const QString& log);
117
118public slots:
119 void loadGame(const QString& path);
120 void loadGame(VFile* vf, const QString& base);
121 void loadBIOS(const QString& path);
122 void loadSave(const QString& path, bool temporary = true);
123 void yankPak();
124 void replaceGame(const QString& path);
125 void setUseBIOS(bool);
126 void loadPatch(const QString& path);
127 void importSharkport(const QString& path);
128 void exportSharkport(const QString& path);
129 void bootBIOS();
130 void closeGame();
131 void setPaused(bool paused);
132 void reset();
133 void frameAdvance();
134 void setRewind(bool enable, int capacity);
135 void rewind(int states = 0);
136 void startRewinding();
137 void stopRewinding();
138 void keyPressed(int key);
139 void keyReleased(int key);
140 void clearKeys();
141 void setAutofire(int key, bool enable);
142 void setAudioBufferSamples(int samples);
143 void setAudioSampleRate(unsigned rate);
144 void setAudioChannelEnabled(int channel, bool enable = true);
145 void startAudio();
146 void setVideoLayerEnabled(int layer, bool enable = true);
147 void setFPSTarget(float fps);
148 void loadState(int slot = 0);
149 void saveState(int slot = 0);
150 void loadBackupState();
151 void saveBackupState();
152 void setTurbo(bool, bool forced = true);
153 void setTurboSpeed(float ratio);
154 void setSync(bool);
155 void setAVStream(mAVStream*);
156 void clearAVStream();
157 void reloadAudioDriver();
158 void setSaveStateExtdata(int flags);
159 void setLoadStateExtdata(int flags);
160
161#ifdef USE_PNG
162 void screenshot();
163#endif
164
165 void setLuminanceValue(uint8_t value);
166 uint8_t luminanceValue() const { return m_luxValue; }
167 void setLuminanceLevel(int level);
168 void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
169 void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
170
171 void setRealTime();
172 void setFixedTime(const QDateTime& time);
173 void setFakeEpoch(const QDateTime& time);
174
175 void setLogLevel(int);
176 void enableLogLevel(int);
177 void disableLogLevel(int);
178
179private slots:
180 void openGame(bool bios = false);
181 void crashGame(const QString& crashMessage);
182 void cleanGame();
183
184 void pollEvents();
185 void updateAutofire();
186
187private:
188 void updateKeys();
189 void redoSamples(int samples);
190 void enableTurbo();
191
192 uint32_t* m_drawContext;
193 uint32_t* m_frontBuffer;
194 mCoreThread m_threadContext;
195 const mCoreConfig* m_config;
196 mCheatDevice* m_cheatDevice;
197 int m_activeKeys;
198 int m_activeButtons;
199 int m_inactiveKeys;
200 int m_logLevels;
201
202 bool m_gameOpen;
203
204 QString m_fname;
205 VFile* m_vf;
206 QString m_bios;
207 bool m_useBios;
208 QString m_patch;
209 Override* m_override;
210
211 QThread* m_audioThread;
212 AudioProcessor* m_audioProcessor;
213
214 QAtomicInt m_pauseAfterFrame;
215 QList<std::function<void ()>> m_resetActions;
216
217 bool m_sync;
218 bool m_videoSync;
219 bool m_audioSync;
220 float m_fpsTarget;
221 bool m_turbo;
222 bool m_turboForced;
223 float m_turboSpeed;
224 bool m_wasPaused;
225
226 std::shared_ptr<mTileCache> m_tileCache;
227
228 bool m_audioChannels[6];
229 bool m_videoLayers[5];
230
231 bool m_autofire[GBA_KEY_MAX];
232 int m_autofireStatus[GBA_KEY_MAX];
233
234 int m_stateSlot;
235 struct VFile* m_backupLoadState;
236 QByteArray m_backupSaveState;
237 int m_saveStateFlags;
238 int m_loadStateFlags;
239
240 InputController* m_inputController;
241 MultiplayerController* m_multiplayer;
242
243 mAVStream* m_stream;
244
245 struct GameControllerLux : GBALuminanceSource {
246 GameController* p;
247 uint8_t value;
248 } m_lux;
249 uint8_t m_luxValue;
250 int m_luxLevel;
251
252 mRTCGenericSource m_rtc;
253};
254
255}
256
257#endif