all repos — mgba @ e6ea94d2296eae963a48a18d009217a38d92bf9b

mGBA Game Boy Advance Emulator

src/platform/qt/GameController.cpp (view raw)

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