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