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_rom(nullptr)
23 , m_audioThread(new QThread(this))
24 , m_audioProcessor(new AudioProcessor)
25{
26 m_renderer = new GBAVideoSoftwareRenderer;
27 GBAVideoSoftwareRendererCreate(m_renderer);
28 m_renderer->outputBuffer = (color_t*) m_drawContext;
29 m_renderer->outputBufferStride = 256;
30 m_threadContext.state = THREAD_INITIALIZED;
31 m_threadContext.debugger = 0;
32 m_threadContext.frameskip = 0;
33 m_threadContext.bios = 0;
34 m_threadContext.renderer = &m_renderer->d;
35 m_threadContext.userData = this;
36 m_threadContext.rewindBufferCapacity = 0;
37 m_threadContext.logLevel = -1;
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(QThread::TimeCriticalPriority);
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 m_audioThread->wait();
91 if (GBAThreadIsPaused(&m_threadContext)) {
92 GBAThreadUnpause(&m_threadContext);
93 }
94 disconnect();
95 closeGame();
96 delete m_renderer;
97}
98
99ARMDebugger* GameController::debugger() {
100 return m_threadContext.debugger;
101}
102
103void GameController::setDebugger(ARMDebugger* debugger) {
104 bool wasPaused = isPaused();
105 setPaused(true);
106 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
107 GBADetachDebugger(m_threadContext.gba);
108 }
109 m_threadContext.debugger = debugger;
110 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
111 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
112 }
113 setPaused(wasPaused);
114}
115
116void GameController::loadGame(const QString& path) {
117 closeGame();
118 m_threadContext.sync.videoFrameWait = 0;
119 m_threadContext.sync.audioWait = 1;
120 m_rom = new QFile(path);
121 if (!m_rom->open(QIODevice::ReadOnly)) {
122 delete m_rom;
123 m_rom = nullptr;
124 }
125
126 m_pauseAfterFrame = false;
127
128 m_threadContext.rom = VFileFromFD(m_rom->handle());
129 m_threadContext.fname = strdup(path.toLocal8Bit().constData());
130
131 GBAThreadStart(&m_threadContext);
132}
133
134void GameController::closeGame() {
135 if (!m_rom) {
136 return;
137 }
138 GBAThreadEnd(&m_threadContext);
139 GBAThreadJoin(&m_threadContext);
140 if (m_threadContext.fname) {
141 free(const_cast<char*>(m_threadContext.fname));
142 m_threadContext.fname = nullptr;
143 }
144 if (m_rom) {
145 m_rom->close();
146 delete m_rom;
147 m_rom = nullptr;
148 }
149 emit gameStopped(&m_threadContext);
150}
151
152bool GameController::isPaused() {
153 return GBAThreadIsPaused(&m_threadContext);
154}
155
156void GameController::setPaused(bool paused) {
157 if (paused == GBAThreadIsPaused(&m_threadContext)) {
158 return;
159 }
160 if (paused) {
161 GBAThreadPause(&m_threadContext);
162 emit gamePaused(&m_threadContext);
163 } else {
164 GBAThreadUnpause(&m_threadContext);
165 emit gameUnpaused(&m_threadContext);
166 }
167}
168
169void GameController::reset() {
170 GBAThreadReset(&m_threadContext);
171}
172
173void GameController::frameAdvance() {
174 m_pauseMutex.lock();
175 m_pauseAfterFrame = true;
176 setPaused(false);
177 m_pauseMutex.unlock();
178}
179
180void GameController::keyPressed(int key) {
181 int mappedKey = 1 << key;
182 m_activeKeys |= mappedKey;
183 updateKeys();
184}
185
186void GameController::keyReleased(int key) {
187 int mappedKey = 1 << key;
188 m_activeKeys &= ~mappedKey;
189 updateKeys();
190}
191
192void GameController::setAudioBufferSamples(int samples) {
193 GBAThreadInterrupt(&m_threadContext);
194 m_threadContext.audioBuffers = samples;
195 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
196 GBAThreadContinue(&m_threadContext);
197 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
198}
199
200void GameController::setFPSTarget(float fps) {
201 GBAThreadInterrupt(&m_threadContext);
202 m_threadContext.fpsTarget = fps;
203 GBAThreadContinue(&m_threadContext);
204 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
205}
206
207void GameController::loadState(int slot) {
208 GBAThreadInterrupt(&m_threadContext);
209 GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
210 ConditionWake(&m_threadContext.sync.videoFrameAvailableCond); // Hack: wake up the drawing thread
211 GBAThreadContinue(&m_threadContext);
212 emit stateLoaded(&m_threadContext);
213 emit frameAvailable(m_drawContext);
214}
215
216void GameController::saveState(int slot) {
217 GBAThreadInterrupt(&m_threadContext);
218 GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
219 GBAThreadContinue(&m_threadContext);
220}
221
222void GameController::updateKeys() {
223 int activeKeys = m_activeKeys;
224#ifdef BUILD_SDL
225 activeKeys |= m_activeButtons;
226#endif
227 m_threadContext.activeKeys = activeKeys;
228}
229
230#ifdef BUILD_SDL
231void GameController::testSDLEvents() {
232 SDL_Joystick* joystick = m_sdlEvents.joystick;
233 SDL_JoystickUpdate();
234 int numButtons = SDL_JoystickNumButtons(joystick);
235 m_activeButtons = 0;
236 int i;
237 for (i = 0; i < numButtons; ++i) {
238 GBAKey key = GBAInputMapKey(&m_threadContext.inputMap, SDL_BINDING_BUTTON, i);
239 if (key == GBA_KEY_NONE) {
240 continue;
241 }
242 if (SDL_JoystickGetButton(joystick, i)) {
243 m_activeButtons |= 1 << key;
244 }
245 }
246 int numHats = SDL_JoystickNumHats(joystick);
247 for (i = 0; i < numHats; ++i) {
248 int hat = SDL_JoystickGetHat(joystick, i);
249 if (hat & SDL_HAT_UP) {
250 m_activeButtons |= 1 << GBA_KEY_UP;
251 }
252 if (hat & SDL_HAT_LEFT) {
253 m_activeButtons |= 1 << GBA_KEY_LEFT;
254 }
255 if (hat & SDL_HAT_DOWN) {
256 m_activeButtons |= 1 << GBA_KEY_DOWN;
257 }
258 if (hat & SDL_HAT_RIGHT) {
259 m_activeButtons |= 1 << GBA_KEY_RIGHT;
260 }
261 }
262 updateKeys();
263}
264#endif