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