all repos — mgba @ 467fbcf54d0942b2732aec8c6643f2616bb0174b

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