all repos — mgba @ 25f5520b0b59a5235dae0c9c476baa9e2753269f

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