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