all repos — mgba @ 802e4b87201c4ade66551f5229cafc790ca61ab7

mGBA Game Boy Advance Emulator

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	if (!m_gameOpen) {
212		return false;
213	}
214	return GBAThreadIsPaused(&m_threadContext);
215}
216
217void GameController::setPaused(bool paused) {
218	if (paused == GBAThreadIsPaused(&m_threadContext)) {
219		return;
220	}
221	if (paused) {
222		GBAThreadPause(&m_threadContext);
223		emit gamePaused(&m_threadContext);
224	} else {
225		GBAThreadUnpause(&m_threadContext);
226		emit gameUnpaused(&m_threadContext);
227	}
228}
229
230void GameController::reset() {
231	GBAThreadReset(&m_threadContext);
232}
233
234void GameController::threadInterrupt() {
235	if (m_gameOpen) {
236		GBAThreadInterrupt(&m_threadContext);
237	}
238}
239
240void GameController::threadContinue() {
241	if (m_gameOpen) {
242		GBAThreadContinue(&m_threadContext);
243	}
244}
245
246void GameController::frameAdvance() {
247	m_pauseMutex.lock();
248	m_pauseAfterFrame = true;
249	setPaused(false);
250	m_pauseMutex.unlock();
251}
252
253void GameController::keyPressed(int key) {
254	int mappedKey = 1 << key;
255	m_activeKeys |= mappedKey;
256	updateKeys();
257}
258
259void GameController::keyReleased(int key) {
260	int mappedKey = 1 << key;
261	m_activeKeys &= ~mappedKey;
262	updateKeys();
263}
264
265void GameController::setAudioBufferSamples(int samples) {
266	if (m_gameOpen) {
267		threadInterrupt();
268		m_threadContext.audioBuffers = samples;
269		GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
270		threadContinue();
271	} else {
272		m_threadContext.audioBuffers = samples;
273
274	}
275	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
276}
277
278void GameController::setFPSTarget(float fps) {
279	threadInterrupt();
280	m_threadContext.fpsTarget = fps;
281	threadContinue();
282	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
283}
284
285void GameController::loadState(int slot) {
286	threadInterrupt();
287	GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
288	threadContinue();
289	emit stateLoaded(&m_threadContext);
290	emit frameAvailable(m_drawContext);
291}
292
293void GameController::saveState(int slot) {
294	threadInterrupt();
295	GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
296	threadContinue();
297}
298
299void GameController::setVideoSync(bool set) {
300	m_videoSync = set;
301	if (!m_turbo) {
302		threadInterrupt();
303		m_threadContext.sync.videoFrameWait = set;
304		threadContinue();
305	}
306}
307
308void GameController::setAudioSync(bool set) {
309	m_audioSync = set;
310	if (!m_turbo) {
311		threadInterrupt();
312		m_threadContext.sync.audioWait = set;
313		threadContinue();
314	}
315}
316
317void GameController::setFrameskip(int skip) {
318	m_threadContext.frameskip = skip;
319}
320
321void GameController::setTurbo(bool set, bool forced) {
322	if (m_turboForced && !forced) {
323		return;
324	}
325	m_turbo = set;
326	if (set) {
327		m_turboForced = forced;
328	} else {
329		m_turboForced = false;
330	}
331	threadInterrupt();
332	m_threadContext.sync.audioWait = set ? false : m_audioSync;
333	m_threadContext.sync.videoFrameWait = set ? false : m_videoSync;
334	threadContinue();
335}
336
337void GameController::setAVStream(GBAAVStream* stream) {
338	threadInterrupt();
339	m_threadContext.stream = stream;
340	threadContinue();
341}
342
343void GameController::clearAVStream() {
344	threadInterrupt();
345	m_threadContext.stream = nullptr;
346	threadContinue();
347}
348
349void GameController::updateKeys() {
350	int activeKeys = m_activeKeys;
351#ifdef BUILD_SDL
352	activeKeys |= m_activeButtons;
353#endif
354	m_threadContext.activeKeys = activeKeys;
355}
356
357void GameController::setLogLevel(int levels) {
358	threadInterrupt();
359	m_logLevels = levels;
360	threadContinue();
361}
362
363void GameController::enableLogLevel(int levels) {
364	threadInterrupt();
365	m_logLevels |= levels;
366	threadContinue();
367}
368
369void GameController::disableLogLevel(int levels) {
370	threadInterrupt();
371	m_logLevels &= ~levels;
372	threadContinue();
373}
374
375#ifdef BUILD_SDL
376void GameController::testSDLEvents() {
377	if (!m_inputController) {
378		return;
379	}
380
381	m_activeButtons = m_inputController->testSDLEvents();
382	updateKeys();
383}
384#endif