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}
99
100ARMDebugger* GameController::debugger() {
101 return m_threadContext.debugger;
102}
103
104void GameController::setDebugger(ARMDebugger* debugger) {
105 bool wasPaused = isPaused();
106 setPaused(true);
107 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
108 GBADetachDebugger(m_threadContext.gba);
109 }
110 m_threadContext.debugger = debugger;
111 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
112 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
113 }
114 setPaused(wasPaused);
115}
116
117void GameController::loadGame(const QString& path, bool dirmode) {
118 closeGame();
119 if (!dirmode) {
120 QFile file(path);
121 if (!file.open(QIODevice::ReadOnly)) {
122 return;
123 }
124 file.close();
125 }
126
127 m_fname = path;
128 m_dirmode = dirmode;
129 openGame();
130}
131
132void GameController::openGame() {
133 m_gameOpen = true;
134
135 m_pauseAfterFrame = false;
136
137 if (m_turbo) {
138 m_threadContext.sync.videoFrameWait = false;
139 m_threadContext.sync.audioWait = false;
140 } else {
141 m_threadContext.sync.videoFrameWait = m_videoSync;
142 m_threadContext.sync.audioWait = m_audioSync;
143 }
144
145 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
146 if (m_dirmode) {
147 m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
148 m_threadContext.stateDir = m_threadContext.gameDir;
149 } else {
150 m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
151#if ENABLE_LIBZIP
152 m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
153#endif
154 }
155
156 if (!m_bios.isNull()) {
157 m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
158 }
159
160 if (!m_patch.isNull()) {
161 m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
162 }
163
164 if (!GBAThreadStart(&m_threadContext)) {
165 m_gameOpen = false;
166 }
167}
168
169void GameController::loadBIOS(const QString& path) {
170 m_bios = path;
171 if (m_gameOpen) {
172 closeGame();
173 openGame();
174 }
175}
176
177void GameController::loadPatch(const QString& path) {
178 m_patch = path;
179 if (m_gameOpen) {
180 closeGame();
181 openGame();
182 }
183}
184
185void GameController::closeGame() {
186 if (!m_gameOpen) {
187 return;
188 }
189 if (GBAThreadIsPaused(&m_threadContext)) {
190 GBAThreadUnpause(&m_threadContext);
191 }
192 GBAThreadEnd(&m_threadContext);
193 GBAThreadJoin(&m_threadContext);
194 if (m_threadContext.fname) {
195 free(const_cast<char*>(m_threadContext.fname));
196 m_threadContext.fname = nullptr;
197 }
198
199 m_patch = QString();
200
201 m_gameOpen = false;
202 emit gameStopped(&m_threadContext);
203}
204
205bool GameController::isPaused() {
206 return GBAThreadIsPaused(&m_threadContext);
207}
208
209void GameController::setPaused(bool paused) {
210 if (paused == GBAThreadIsPaused(&m_threadContext)) {
211 return;
212 }
213 if (paused) {
214 GBAThreadPause(&m_threadContext);
215 emit gamePaused(&m_threadContext);
216 } else {
217 GBAThreadUnpause(&m_threadContext);
218 emit gameUnpaused(&m_threadContext);
219 }
220}
221
222void GameController::reset() {
223 GBAThreadReset(&m_threadContext);
224}
225
226void GameController::frameAdvance() {
227 m_pauseMutex.lock();
228 m_pauseAfterFrame = true;
229 setPaused(false);
230 m_pauseMutex.unlock();
231}
232
233void GameController::keyPressed(int key) {
234 int mappedKey = 1 << key;
235 m_activeKeys |= mappedKey;
236 updateKeys();
237}
238
239void GameController::keyReleased(int key) {
240 int mappedKey = 1 << key;
241 m_activeKeys &= ~mappedKey;
242 updateKeys();
243}
244
245void GameController::setAudioBufferSamples(int samples) {
246 if (m_gameOpen) {
247 GBAThreadInterrupt(&m_threadContext);
248 m_threadContext.audioBuffers = samples;
249 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
250 GBAThreadContinue(&m_threadContext);
251 } else {
252 m_threadContext.audioBuffers = samples;
253
254 }
255 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
256}
257
258void GameController::setFPSTarget(float fps) {
259 if (m_gameOpen) {
260 GBAThreadInterrupt(&m_threadContext);
261 m_threadContext.fpsTarget = fps;
262 GBAThreadContinue(&m_threadContext);
263 } else {
264 m_threadContext.fpsTarget = fps;
265 }
266 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
267}
268
269void GameController::loadState(int slot) {
270 GBAThreadInterrupt(&m_threadContext);
271 GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
272 GBAThreadContinue(&m_threadContext);
273 emit stateLoaded(&m_threadContext);
274 emit frameAvailable(m_drawContext);
275}
276
277void GameController::saveState(int slot) {
278 GBAThreadInterrupt(&m_threadContext);
279 GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
280 GBAThreadContinue(&m_threadContext);
281}
282
283void GameController::setVideoSync(bool set) {
284 m_videoSync = set;
285 if (!m_turbo && m_gameOpen) {
286 GBAThreadInterrupt(&m_threadContext);
287 m_threadContext.sync.videoFrameWait = set;
288 GBAThreadContinue(&m_threadContext);
289 }
290}
291
292void GameController::setAudioSync(bool set) {
293 m_audioSync = set;
294 if (!m_turbo && m_gameOpen) {
295 GBAThreadInterrupt(&m_threadContext);
296 m_threadContext.sync.audioWait = set;
297 GBAThreadContinue(&m_threadContext);
298 }
299}
300
301void GameController::setFrameskip(int skip) {
302 m_threadContext.frameskip = skip;
303}
304
305void GameController::setTurbo(bool set, bool forced) {
306 if (m_turboForced && !forced) {
307 return;
308 }
309 m_turbo = set;
310 if (set) {
311 m_turboForced = forced;
312 } else {
313 m_turboForced = false;
314 }
315 if (m_gameOpen) {
316 GBAThreadInterrupt(&m_threadContext);
317 m_threadContext.sync.audioWait = set ? false : m_audioSync;
318 m_threadContext.sync.videoFrameWait = set ? false : m_videoSync;
319 GBAThreadContinue(&m_threadContext);
320 }
321}
322
323void GameController::setAVStream(GBAAVStream* stream) {
324 if (m_gameOpen) {
325 GBAThreadInterrupt(&m_threadContext);
326 m_threadContext.stream = stream;
327 GBAThreadContinue(&m_threadContext);
328 } else {
329 m_threadContext.stream = stream;
330 }
331}
332
333void GameController::clearAVStream() {
334 if (m_gameOpen) {
335 GBAThreadInterrupt(&m_threadContext);
336 m_threadContext.stream = nullptr;
337 GBAThreadContinue(&m_threadContext);
338 } else {
339 m_threadContext.stream = nullptr;
340 }
341}
342
343void GameController::updateKeys() {
344 int activeKeys = m_activeKeys;
345#ifdef BUILD_SDL
346 activeKeys |= m_activeButtons;
347#endif
348 m_threadContext.activeKeys = activeKeys;
349}
350
351#ifdef BUILD_SDL
352void GameController::testSDLEvents() {
353 SDL_Joystick* joystick = m_sdlEvents.joystick;
354 SDL_JoystickUpdate();
355 int numButtons = SDL_JoystickNumButtons(joystick);
356 m_activeButtons = 0;
357 int i;
358 for (i = 0; i < numButtons; ++i) {
359 GBAKey key = GBAInputMapKey(&m_threadContext.inputMap, SDL_BINDING_BUTTON, i);
360 if (key == GBA_KEY_NONE) {
361 continue;
362 }
363 if (SDL_JoystickGetButton(joystick, i)) {
364 m_activeButtons |= 1 << key;
365 }
366 }
367 int numHats = SDL_JoystickNumHats(joystick);
368 for (i = 0; i < numHats; ++i) {
369 int hat = SDL_JoystickGetHat(joystick, i);
370 if (hat & SDL_HAT_UP) {
371 m_activeButtons |= 1 << GBA_KEY_UP;
372 }
373 if (hat & SDL_HAT_LEFT) {
374 m_activeButtons |= 1 << GBA_KEY_LEFT;
375 }
376 if (hat & SDL_HAT_DOWN) {
377 m_activeButtons |= 1 << GBA_KEY_DOWN;
378 }
379 if (hat & SDL_HAT_RIGHT) {
380 m_activeButtons |= 1 << GBA_KEY_RIGHT;
381 }
382 }
383 updateKeys();
384}
385#endif