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