src/platform/qt/GameController.cpp (view raw)
1#include "GameController.h"
2
3#include "AudioProcessor.h"
4#include "InputController.h"
5
6#include <QThread>
7
8extern "C" {
9#include "gba.h"
10#include "gba-audio.h"
11#include "gba-serialize.h"
12#include "renderers/video-software.h"
13#include "util/vfs.h"
14}
15
16using namespace QGBA;
17
18GameController::GameController(QObject* parent)
19 : QObject(parent)
20 , m_drawContext(new uint32_t[256 * 256])
21 , m_threadContext()
22 , m_activeKeys(0)
23 , m_gameOpen(false)
24 , m_audioThread(new QThread(this))
25 , m_audioProcessor(new AudioProcessor)
26 , m_videoSync(VIDEO_SYNC)
27 , m_audioSync(AUDIO_SYNC)
28 , m_turbo(false)
29 , m_turboForced(false)
30 , m_inputController(nullptr)
31{
32 m_renderer = new GBAVideoSoftwareRenderer;
33 GBAVideoSoftwareRendererCreate(m_renderer);
34 m_renderer->outputBuffer = (color_t*) m_drawContext;
35 m_renderer->outputBufferStride = 256;
36 m_threadContext.state = THREAD_INITIALIZED;
37 m_threadContext.debugger = 0;
38 m_threadContext.frameskip = 0;
39 m_threadContext.bios = 0;
40 m_threadContext.renderer = &m_renderer->d;
41 m_threadContext.userData = this;
42 m_threadContext.rewindBufferCapacity = 0;
43 m_threadContext.logLevel = -1;
44
45 m_threadContext.startCallback = [] (GBAThread* context) {
46 GameController* controller = static_cast<GameController*>(context->userData);
47 controller->m_audioProcessor->setInput(context);
48 controller->gameStarted(context);
49 };
50
51 m_threadContext.cleanCallback = [] (GBAThread* context) {
52 GameController* controller = static_cast<GameController*>(context->userData);
53 controller->gameStopped(context);
54 };
55
56 m_threadContext.frameCallback = [] (GBAThread* context) {
57 GameController* controller = static_cast<GameController*>(context->userData);
58 controller->m_pauseMutex.lock();
59 if (controller->m_pauseAfterFrame) {
60 GBAThreadPauseFromThread(context);
61 controller->m_pauseAfterFrame = false;
62 controller->gamePaused(&controller->m_threadContext);
63 }
64 controller->m_pauseMutex.unlock();
65 controller->frameAvailable(controller->m_drawContext);
66 };
67
68 m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
69 GameController* controller = static_cast<GameController*>(context->userData);
70 controller->postLog(level, QString().vsprintf(format, args));
71 };
72
73 m_audioThread->start(QThread::TimeCriticalPriority);
74 m_audioProcessor->moveToThread(m_audioThread);
75 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
76 connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
77 connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
78 connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
79
80#ifdef BUILD_SDL
81 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents()));
82#endif
83}
84
85GameController::~GameController() {
86 m_audioThread->quit();
87 m_audioThread->wait();
88 disconnect();
89 closeGame();
90 delete m_renderer;
91 delete[] m_drawContext;
92}
93
94ARMDebugger* GameController::debugger() {
95 return m_threadContext.debugger;
96}
97
98void GameController::setDebugger(ARMDebugger* debugger) {
99 bool wasPaused = isPaused();
100 setPaused(true);
101 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
102 GBADetachDebugger(m_threadContext.gba);
103 }
104 m_threadContext.debugger = debugger;
105 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
106 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
107 }
108 setPaused(wasPaused);
109}
110
111void GameController::loadGame(const QString& path, bool dirmode) {
112 closeGame();
113 if (!dirmode) {
114 QFile file(path);
115 if (!file.open(QIODevice::ReadOnly)) {
116 return;
117 }
118 file.close();
119 }
120
121 m_fname = path;
122 m_dirmode = dirmode;
123 openGame();
124}
125
126void GameController::openGame() {
127 m_gameOpen = true;
128
129 m_pauseAfterFrame = false;
130
131 if (m_turbo) {
132 m_threadContext.sync.videoFrameWait = false;
133 m_threadContext.sync.audioWait = false;
134 } else {
135 m_threadContext.sync.videoFrameWait = m_videoSync;
136 m_threadContext.sync.audioWait = m_audioSync;
137 }
138
139 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
140 if (m_dirmode) {
141 m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
142 m_threadContext.stateDir = m_threadContext.gameDir;
143 } else {
144 m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
145#if ENABLE_LIBZIP
146 m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
147#endif
148 }
149
150 if (!m_bios.isNull()) {
151 m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
152 }
153
154 if (!m_patch.isNull()) {
155 m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
156 }
157
158 if (!GBAThreadStart(&m_threadContext)) {
159 m_gameOpen = false;
160 }
161}
162
163void GameController::loadBIOS(const QString& path) {
164 m_bios = path;
165 if (m_gameOpen) {
166 closeGame();
167 openGame();
168 }
169}
170
171void GameController::loadPatch(const QString& path) {
172 m_patch = path;
173 if (m_gameOpen) {
174 closeGame();
175 openGame();
176 }
177}
178
179void GameController::closeGame() {
180 if (!m_gameOpen) {
181 return;
182 }
183 if (GBAThreadIsPaused(&m_threadContext)) {
184 GBAThreadUnpause(&m_threadContext);
185 }
186 GBAThreadEnd(&m_threadContext);
187 GBAThreadJoin(&m_threadContext);
188 if (m_threadContext.fname) {
189 free(const_cast<char*>(m_threadContext.fname));
190 m_threadContext.fname = nullptr;
191 }
192
193 m_patch = QString();
194
195 m_gameOpen = false;
196 emit gameStopped(&m_threadContext);
197}
198
199bool GameController::isPaused() {
200 return GBAThreadIsPaused(&m_threadContext);
201}
202
203void GameController::setPaused(bool paused) {
204 if (paused == GBAThreadIsPaused(&m_threadContext)) {
205 return;
206 }
207 if (paused) {
208 GBAThreadPause(&m_threadContext);
209 emit gamePaused(&m_threadContext);
210 } else {
211 GBAThreadUnpause(&m_threadContext);
212 emit gameUnpaused(&m_threadContext);
213 }
214}
215
216void GameController::reset() {
217 GBAThreadReset(&m_threadContext);
218}
219
220void GameController::frameAdvance() {
221 m_pauseMutex.lock();
222 m_pauseAfterFrame = true;
223 setPaused(false);
224 m_pauseMutex.unlock();
225}
226
227void GameController::keyPressed(int key) {
228 int mappedKey = 1 << key;
229 m_activeKeys |= mappedKey;
230 updateKeys();
231}
232
233void GameController::keyReleased(int key) {
234 int mappedKey = 1 << key;
235 m_activeKeys &= ~mappedKey;
236 updateKeys();
237}
238
239void GameController::setAudioBufferSamples(int samples) {
240 if (m_gameOpen) {
241 GBAThreadInterrupt(&m_threadContext);
242 m_threadContext.audioBuffers = samples;
243 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
244 GBAThreadContinue(&m_threadContext);
245 } else {
246 m_threadContext.audioBuffers = samples;
247
248 }
249 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
250}
251
252void GameController::setFPSTarget(float fps) {
253 GBAThreadInterrupt(&m_threadContext);
254 m_threadContext.fpsTarget = fps;
255 GBAThreadContinue(&m_threadContext);
256 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
257}
258
259void GameController::loadState(int slot) {
260 GBAThreadInterrupt(&m_threadContext);
261 GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
262 GBAThreadContinue(&m_threadContext);
263 emit stateLoaded(&m_threadContext);
264 emit frameAvailable(m_drawContext);
265}
266
267void GameController::saveState(int slot) {
268 GBAThreadInterrupt(&m_threadContext);
269 GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
270 GBAThreadContinue(&m_threadContext);
271}
272
273void GameController::setVideoSync(bool set) {
274 m_videoSync = set;
275 if (!m_turbo && m_gameOpen) {
276 GBAThreadInterrupt(&m_threadContext);
277 m_threadContext.sync.videoFrameWait = set;
278 GBAThreadContinue(&m_threadContext);
279 }
280}
281
282void GameController::setAudioSync(bool set) {
283 m_audioSync = set;
284 if (!m_turbo && m_gameOpen) {
285 GBAThreadInterrupt(&m_threadContext);
286 m_threadContext.sync.audioWait = set;
287 GBAThreadContinue(&m_threadContext);
288 }
289}
290
291void GameController::setFrameskip(int skip) {
292 m_threadContext.frameskip = skip;
293}
294
295void GameController::setTurbo(bool set, bool forced) {
296 if (m_turboForced && !forced) {
297 return;
298 }
299 m_turbo = set;
300 if (set) {
301 m_turboForced = forced;
302 } else {
303 m_turboForced = false;
304 }
305 if (m_gameOpen) {
306 GBAThreadInterrupt(&m_threadContext);
307 m_threadContext.sync.audioWait = set ? false : m_audioSync;
308 m_threadContext.sync.videoFrameWait = set ? false : m_videoSync;
309 GBAThreadContinue(&m_threadContext);
310 }
311}
312
313void GameController::setAVStream(GBAAVStream* stream) {
314 GBAThreadInterrupt(&m_threadContext);
315 m_threadContext.stream = stream;
316 GBAThreadContinue(&m_threadContext);
317}
318
319void GameController::clearAVStream() {
320 GBAThreadInterrupt(&m_threadContext);
321 m_threadContext.stream = nullptr;
322 GBAThreadContinue(&m_threadContext);
323}
324
325void GameController::updateKeys() {
326 int activeKeys = m_activeKeys;
327#ifdef BUILD_SDL
328 activeKeys |= m_activeButtons;
329#endif
330 m_threadContext.activeKeys = activeKeys;
331}
332
333#ifdef BUILD_SDL
334void GameController::testSDLEvents() {
335 if (!m_inputController) {
336 return;
337 }
338
339 m_activeButtons = m_inputController->testSDLEvents();
340 updateKeys();
341}
342#endif