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