all repos — mgba @ 422e2e2f62fb536ab8c9a2155999287cef5454ea

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	if (GBAThreadIsPaused(&m_threadContext)) {
 96		GBAThreadUnpause(&m_threadContext);
 97	}
 98	disconnect();
 99	closeGame();
100	delete m_renderer;
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	m_threadContext.sync.videoFrameWait = m_videoSync;
123	m_threadContext.sync.audioWait = m_audioSync;
124	if (!dirmode) {
125		QFile file(path);
126		if (!file.open(QIODevice::ReadOnly)) {
127			return;
128		}
129		file.close();
130	}
131	m_gameOpen = true;
132
133	m_pauseAfterFrame = false;
134
135	m_threadContext.fname = strdup(path.toLocal8Bit().constData());
136	if (dirmode) {
137		m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
138		m_threadContext.stateDir = m_threadContext.gameDir;
139	} else {
140		m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
141#if ENABLE_LIBZIP
142		m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
143#endif
144	}
145
146	if (!m_bios.isNull()) {
147		m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
148	}
149
150	if (!m_patch.isNull()) {
151		m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
152	}
153
154	GBAThreadStart(&m_threadContext);
155}
156
157void GameController::loadBIOS(const QString& path) {
158	m_bios = path;
159}
160
161void GameController::loadPatch(const QString& path) {
162	m_patch = path;
163}
164
165void GameController::closeGame() {
166	if (!m_gameOpen) {
167		return;
168	}
169	GBAThreadEnd(&m_threadContext);
170	GBAThreadJoin(&m_threadContext);
171	if (m_threadContext.fname) {
172		free(const_cast<char*>(m_threadContext.fname));
173		m_threadContext.fname = nullptr;
174	}
175
176	m_gameOpen = false;
177	emit gameStopped(&m_threadContext);
178}
179
180bool GameController::isPaused() {
181	return GBAThreadIsPaused(&m_threadContext);
182}
183
184void GameController::setPaused(bool paused) {
185	if (paused == GBAThreadIsPaused(&m_threadContext)) {
186		return;
187	}
188	if (paused) {
189		GBAThreadPause(&m_threadContext);
190		emit gamePaused(&m_threadContext);
191	} else {
192		GBAThreadUnpause(&m_threadContext);
193		emit gameUnpaused(&m_threadContext);
194	}
195}
196
197void GameController::reset() {
198	GBAThreadReset(&m_threadContext);
199}
200
201void GameController::frameAdvance() {
202	m_pauseMutex.lock();
203	m_pauseAfterFrame = true;
204	setPaused(false);
205	m_pauseMutex.unlock();
206}
207
208void GameController::keyPressed(int key) {
209	int mappedKey = 1 << key;
210	m_activeKeys |= mappedKey;
211	updateKeys();
212}
213
214void GameController::keyReleased(int key) {
215	int mappedKey = 1 << key;
216	m_activeKeys &= ~mappedKey;
217	updateKeys();
218}
219
220void GameController::setAudioBufferSamples(int samples) {
221	GBAThreadInterrupt(&m_threadContext);
222	m_threadContext.audioBuffers = samples;
223	GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
224	GBAThreadContinue(&m_threadContext);
225	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
226}
227
228void GameController::setFPSTarget(float fps) {
229	GBAThreadInterrupt(&m_threadContext);
230	m_threadContext.fpsTarget = fps;
231	GBAThreadContinue(&m_threadContext);
232	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
233}
234
235void GameController::loadState(int slot) {
236	GBAThreadInterrupt(&m_threadContext);
237	GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
238	GBAThreadContinue(&m_threadContext);
239	emit stateLoaded(&m_threadContext);
240	emit frameAvailable(m_drawContext);
241}
242
243void GameController::saveState(int slot) {
244	GBAThreadInterrupt(&m_threadContext);
245	GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
246	GBAThreadContinue(&m_threadContext);
247}
248
249void GameController::setVideoSync(bool set) {
250	m_videoSync = set;
251	if (!m_turbo) {
252		GBAThreadInterrupt(&m_threadContext);
253		m_threadContext.sync.videoFrameWait = set;
254		GBAThreadContinue(&m_threadContext);
255	}
256}
257
258void GameController::setAudioSync(bool set) {
259	m_audioSync = set;
260	if (!m_turbo) {
261		GBAThreadInterrupt(&m_threadContext);
262		m_threadContext.sync.audioWait = set;
263		GBAThreadContinue(&m_threadContext);
264	}
265}
266
267void GameController::setTurbo(bool set, bool forced) {
268	if (m_turboForced && !forced) {
269		return;
270	}
271	m_turbo = set;
272	if (set) {
273		m_turboForced = forced;
274	} else {
275		m_turboForced = false;
276	}
277	GBAThreadInterrupt(&m_threadContext);
278	m_threadContext.sync.audioWait = set ? false : m_audioSync;
279	m_threadContext.sync.videoFrameWait = set ? false : m_videoSync;
280	GBAThreadContinue(&m_threadContext);
281}
282
283void GameController::updateKeys() {
284	int activeKeys = m_activeKeys;
285#ifdef BUILD_SDL
286	activeKeys |= m_activeButtons;
287#endif
288	m_threadContext.activeKeys = activeKeys;
289}
290
291#ifdef BUILD_SDL
292void GameController::testSDLEvents() {
293	SDL_Joystick* joystick = m_sdlEvents.joystick;
294	SDL_JoystickUpdate();
295	int numButtons = SDL_JoystickNumButtons(joystick);
296	m_activeButtons = 0;
297	int i;
298	for (i = 0; i < numButtons; ++i) {
299		GBAKey key = GBAInputMapKey(&m_threadContext.inputMap, SDL_BINDING_BUTTON, i);
300		if (key == GBA_KEY_NONE) {
301			continue;
302		}
303		if (SDL_JoystickGetButton(joystick, i)) {
304			m_activeButtons |= 1 << key;
305		}
306	}
307	int numHats = SDL_JoystickNumHats(joystick);
308	for (i = 0; i < numHats; ++i) {
309		int hat = SDL_JoystickGetHat(joystick, i);
310		if (hat & SDL_HAT_UP) {
311			m_activeButtons |= 1 << GBA_KEY_UP;
312		}
313		if (hat & SDL_HAT_LEFT) {
314			m_activeButtons |= 1 << GBA_KEY_LEFT;
315		}
316		if (hat & SDL_HAT_DOWN) {
317			m_activeButtons |= 1 << GBA_KEY_DOWN;
318		}
319		if (hat & SDL_HAT_RIGHT) {
320			m_activeButtons |= 1 << GBA_KEY_RIGHT;
321		}
322	}
323	updateKeys();
324}
325#endif