all repos — mgba @ eb2315cdd8197009372e18170fb12afe2381db46

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