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