all repos — mgba @ 28ac288d2cc753aab0493a471726cc5795a09363

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