all repos — mgba @ 2e031a8c49fee06fadae7c33c83c8042cdfa2f35

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 "renderers/video-software.h"
 11#include "util/vfs.h"
 12}
 13
 14using namespace QGBA;
 15
 16GameController::GameController(QObject* parent)
 17	: QObject(parent)
 18	, m_drawContext(new uint32_t[256 * 256])
 19	, m_activeKeys(0)
 20	, m_rom(nullptr)
 21	, m_audioThread(new QThread(this))
 22	, m_audioProcessor(new AudioProcessor)
 23{
 24	m_renderer = new GBAVideoSoftwareRenderer;
 25	GBAVideoSoftwareRendererCreate(m_renderer);
 26	m_renderer->outputBuffer = (color_t*) m_drawContext;
 27	m_renderer->outputBufferStride = 256;
 28	m_threadContext = {
 29		.state = THREAD_INITIALIZED,
 30		.debugger = 0,
 31		.frameskip = 0,
 32		.bios = 0,
 33		.renderer = &m_renderer->d,
 34		.userData = this,
 35		.rewindBufferCapacity = 0,
 36		.logLevel = -1,
 37	};
 38
 39	GBAInputMapInit(&m_threadContext.inputMap);
 40
 41#ifdef BUILD_SDL
 42	SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
 43	m_sdlEvents.bindings = &m_threadContext.inputMap;
 44	GBASDLInitEvents(&m_sdlEvents);
 45	SDL_JoystickEventState(SDL_QUERY);
 46#endif
 47
 48	m_threadContext.startCallback = [] (GBAThread* context) {
 49		GameController* controller = static_cast<GameController*>(context->userData);
 50		controller->m_audioProcessor->setInput(context);
 51		controller->gameStarted(context);
 52	};
 53
 54	m_threadContext.cleanCallback = [] (GBAThread* context) {
 55		GameController* controller = static_cast<GameController*>(context->userData);
 56		controller->gameStopped(context);
 57	};
 58
 59	m_threadContext.frameCallback = [] (GBAThread* context) {
 60		GameController* controller = static_cast<GameController*>(context->userData);
 61		controller->m_pauseMutex.lock();
 62		if (controller->m_pauseAfterFrame) {
 63			GBAThreadPauseFromThread(context);
 64			controller->m_pauseAfterFrame = false;
 65			controller->gamePaused(&controller->m_threadContext);
 66		}
 67		controller->m_pauseMutex.unlock();
 68		controller->frameAvailable(controller->m_drawContext);
 69	};
 70
 71	m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
 72		GameController* controller = static_cast<GameController*>(context->userData);
 73		controller->postLog(level, QString().vsprintf(format, args));
 74	};
 75
 76	m_audioThread->start();
 77	m_audioProcessor->moveToThread(m_audioThread);
 78	connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
 79	connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
 80	connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
 81	connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
 82
 83#ifdef BUILD_SDL
 84	connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents()));
 85#endif
 86}
 87
 88GameController::~GameController() {
 89	m_audioThread->quit();
 90	if (GBAThreadIsPaused(&m_threadContext)) {
 91		GBAThreadUnpause(&m_threadContext);
 92	}
 93	GBAThreadEnd(&m_threadContext);
 94	GBAThreadJoin(&m_threadContext);
 95	delete m_renderer;
 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) {
116	closeGame();
117	m_threadContext.sync.videoFrameWait = 0;
118	m_threadContext.sync.audioWait = 1;
119	m_rom = new QFile(path);
120	if (!m_rom->open(QIODevice::ReadOnly)) {
121		delete m_rom;
122		m_rom = nullptr;
123	}
124
125	m_pauseAfterFrame = false;
126
127	m_threadContext.rom = VFileFromFD(m_rom->handle());
128	m_threadContext.fname = path.toLocal8Bit().constData();
129
130	GBAThreadStart(&m_threadContext);
131}
132
133void GameController::closeGame() {
134	if (!m_rom) {
135		return;
136	}
137	GBAThreadEnd(&m_threadContext);
138	GBAThreadJoin(&m_threadContext);
139	if (m_rom) {
140		m_rom->close();
141		delete m_rom;
142		m_rom = nullptr;
143	}
144	emit gameStopped(&m_threadContext);
145}
146
147bool GameController::isPaused() {
148	return GBAThreadIsPaused(&m_threadContext);
149}
150
151void GameController::setPaused(bool paused) {
152	if (paused == GBAThreadIsPaused(&m_threadContext)) {
153		return;
154	}
155	if (paused) {
156		GBAThreadPause(&m_threadContext);
157		emit gamePaused(&m_threadContext);
158	} else {
159		GBAThreadUnpause(&m_threadContext);
160		emit gameUnpaused(&m_threadContext);
161	}
162}
163
164void GameController::reset() {
165	GBAThreadReset(&m_threadContext);
166}
167
168void GameController::frameAdvance() {
169	m_pauseMutex.lock();
170	m_pauseAfterFrame = true;
171	setPaused(false);
172	m_pauseMutex.unlock();
173}
174
175void GameController::keyPressed(int key) {
176	int mappedKey = 1 << key;
177	m_activeKeys |= mappedKey;
178	updateKeys();
179}
180
181void GameController::keyReleased(int key) {
182	int mappedKey = 1 << key;
183	m_activeKeys &= ~mappedKey;
184	updateKeys();
185}
186
187void GameController::setAudioBufferSamples(int samples) {
188	GBAThreadInterrupt(&m_threadContext);
189	m_threadContext.audioBuffers = samples;
190	GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
191	GBAThreadContinue(&m_threadContext);
192	QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
193}
194
195void GameController::setFPSTarget(float fps) {
196	GBAThreadInterrupt(&m_threadContext);
197	m_threadContext.fpsTarget = fps;
198	GBAThreadContinue(&m_threadContext);
199	QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
200}
201
202void GameController::updateKeys() {
203	int activeKeys = m_activeKeys;
204#ifdef BUILD_SDL
205	activeKeys |= m_activeButtons;
206#endif
207	m_threadContext.activeKeys = activeKeys;
208}
209
210#ifdef BUILD_SDL
211void GameController::testSDLEvents() {
212	SDL_Joystick* joystick = m_sdlEvents.joystick;
213	SDL_JoystickUpdate();
214	int numButtons = SDL_JoystickNumButtons(joystick);
215	m_activeButtons = 0;
216	int i;
217	for (i = 0; i < numButtons; ++i) {
218		GBAKey key = GBAInputMapKey(&m_threadContext.inputMap, SDL_BINDING_BUTTON, i);
219		if (key == GBA_KEY_NONE) {
220			continue;
221		}
222		if (SDL_JoystickGetButton(joystick, i)) {
223			m_activeButtons |= 1 << key;
224		}
225	}
226	int numHats = SDL_JoystickNumHats(joystick);
227	for (i = 0; i < numHats; ++i) {
228		int hat = SDL_JoystickGetHat(joystick, i);
229		if (hat & SDL_HAT_UP) {
230			m_activeButtons |= 1 << GBA_KEY_UP;
231		}
232		if (hat & SDL_HAT_LEFT) {
233			m_activeButtons |= 1 << GBA_KEY_LEFT;
234		}
235		if (hat & SDL_HAT_DOWN) {
236			m_activeButtons |= 1 << GBA_KEY_DOWN;
237		}
238		if (hat & SDL_HAT_RIGHT) {
239			m_activeButtons |= 1 << GBA_KEY_RIGHT;
240		}
241	}
242	updateKeys();
243}
244#endif