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