all repos — mgba @ b51e72fcab27f8ca85c3c535cd1f06077bb59bae

mGBA Game Boy Advance Emulator

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

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