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