src/platform/qt/GameController.cpp (view raw)
1#include "GameController.h"
2
3#include "AudioProcessor.h"
4#include "InputController.h"
5
6#include <QThread>
7
8extern "C" {
9#include "gba.h"
10#include "gba-audio.h"
11#include "gba-serialize.h"
12#include "renderers/video-software.h"
13#include "util/vfs.h"
14}
15
16using namespace QGBA;
17
18GameController::GameController(QObject* parent)
19 : QObject(parent)
20 , m_drawContext(new uint32_t[256 * 256])
21 , m_threadContext()
22 , m_activeKeys(0)
23 , m_gameOpen(false)
24 , m_audioThread(new QThread(this))
25 , m_audioProcessor(new AudioProcessor)
26 , m_videoSync(VIDEO_SYNC)
27 , m_audioSync(AUDIO_SYNC)
28 , m_turbo(false)
29 , m_turboForced(false)
30 , m_inputController(nullptr)
31{
32 m_renderer = new GBAVideoSoftwareRenderer;
33 GBAVideoSoftwareRendererCreate(m_renderer);
34 m_renderer->outputBuffer = (color_t*) m_drawContext;
35 m_renderer->outputBufferStride = 256;
36 m_threadContext.state = THREAD_INITIALIZED;
37 m_threadContext.debugger = 0;
38 m_threadContext.frameskip = 0;
39 m_threadContext.bios = 0;
40 m_threadContext.renderer = &m_renderer->d;
41 m_threadContext.userData = this;
42 m_threadContext.rewindBufferCapacity = 0;
43 m_threadContext.logLevel = -1;
44
45 m_threadContext.startCallback = [] (GBAThread* context) {
46 GameController* controller = static_cast<GameController*>(context->userData);
47 controller->m_audioProcessor->setInput(context);
48 controller->gameStarted(context);
49 };
50
51 m_threadContext.cleanCallback = [] (GBAThread* context) {
52 GameController* controller = static_cast<GameController*>(context->userData);
53 controller->gameStopped(context);
54 };
55
56 m_threadContext.frameCallback = [] (GBAThread* context) {
57 GameController* controller = static_cast<GameController*>(context->userData);
58 controller->m_pauseMutex.lock();
59 if (controller->m_pauseAfterFrame) {
60 GBAThreadPauseFromThread(context);
61 controller->m_pauseAfterFrame = false;
62 controller->gamePaused(&controller->m_threadContext);
63 }
64 controller->m_pauseMutex.unlock();
65 controller->frameAvailable(controller->m_drawContext);
66 };
67
68 m_threadContext.logHandler = [] (GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
69 GameController* controller = static_cast<GameController*>(context->userData);
70 controller->postLog(level, QString().vsprintf(format, args));
71 };
72
73 m_audioThread->start(QThread::TimeCriticalPriority);
74 m_audioProcessor->moveToThread(m_audioThread);
75 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
76 connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
77 connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
78 connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
79
80#ifdef BUILD_SDL
81 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents()));
82#endif
83}
84
85GameController::~GameController() {
86 m_audioThread->quit();
87 m_audioThread->wait();
88 disconnect();
89 closeGame();
90 delete m_renderer;
91 delete[] m_drawContext;
92}
93
94ARMDebugger* GameController::debugger() {
95 return m_threadContext.debugger;
96}
97
98void GameController::setDebugger(ARMDebugger* debugger) {
99 bool wasPaused = isPaused();
100 setPaused(true);
101 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
102 GBADetachDebugger(m_threadContext.gba);
103 }
104 m_threadContext.debugger = debugger;
105 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
106 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
107 }
108 setPaused(wasPaused);
109}
110
111void GameController::loadGame(const QString& path, bool dirmode) {
112 closeGame();
113 if (!dirmode) {
114 QFile file(path);
115 if (!file.open(QIODevice::ReadOnly)) {
116 return;
117 }
118 file.close();
119 }
120
121 m_fname = path;
122 m_dirmode = dirmode;
123 openGame();
124}
125
126void GameController::openGame() {
127 m_gameOpen = true;
128
129 m_pauseAfterFrame = false;
130
131 if (m_turbo) {
132 m_threadContext.sync.videoFrameWait = false;
133 m_threadContext.sync.audioWait = false;
134 } else {
135 m_threadContext.sync.videoFrameWait = m_videoSync;
136 m_threadContext.sync.audioWait = m_audioSync;
137 }
138
139 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
140 if (m_dirmode) {
141 m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
142 m_threadContext.stateDir = m_threadContext.gameDir;
143 } else {
144 m_threadContext.rom = VFileOpen(m_threadContext.fname, O_RDONLY);
145#if ENABLE_LIBZIP
146 m_threadContext.gameDir = VDirOpenZip(m_threadContext.fname, 0);
147#endif
148 }
149
150 if (!m_bios.isNull()) {
151 m_threadContext.bios = VFileOpen(m_bios.toLocal8Bit().constData(), O_RDONLY);
152 }
153
154 if (!m_patch.isNull()) {
155 m_threadContext.patch = VFileOpen(m_patch.toLocal8Bit().constData(), O_RDONLY);
156 }
157
158 if (!GBAThreadStart(&m_threadContext)) {
159 m_gameOpen = false;
160 }
161}
162
163void GameController::loadBIOS(const QString& path) {
164 m_bios = path;
165 if (m_gameOpen) {
166 closeGame();
167 openGame();
168 }
169}
170
171void GameController::loadPatch(const QString& path) {
172 m_patch = path;
173 if (m_gameOpen) {
174 closeGame();
175 openGame();
176 }
177}
178
179void GameController::closeGame() {
180 if (!m_gameOpen) {
181 return;
182 }
183 if (GBAThreadIsPaused(&m_threadContext)) {
184 GBAThreadUnpause(&m_threadContext);
185 }
186 GBAThreadEnd(&m_threadContext);
187 GBAThreadJoin(&m_threadContext);
188 if (m_threadContext.fname) {
189 free(const_cast<char*>(m_threadContext.fname));
190 m_threadContext.fname = nullptr;
191 }
192
193 m_patch = QString();
194
195 m_gameOpen = false;
196 emit gameStopped(&m_threadContext);
197}
198
199bool GameController::isPaused() {
200 return GBAThreadIsPaused(&m_threadContext);
201}
202
203void GameController::setPaused(bool paused) {
204 if (paused == GBAThreadIsPaused(&m_threadContext)) {
205 return;
206 }
207 if (paused) {
208 GBAThreadPause(&m_threadContext);
209 emit gamePaused(&m_threadContext);
210 } else {
211 GBAThreadUnpause(&m_threadContext);
212 emit gameUnpaused(&m_threadContext);
213 }
214}
215
216void GameController::reset() {
217 GBAThreadReset(&m_threadContext);
218}
219
220void GameController::frameAdvance() {
221 m_pauseMutex.lock();
222 m_pauseAfterFrame = true;
223 setPaused(false);
224 m_pauseMutex.unlock();
225}
226
227void GameController::keyPressed(int key) {
228 int mappedKey = 1 << key;
229 m_activeKeys |= mappedKey;
230 updateKeys();
231}
232
233void GameController::keyReleased(int key) {
234 int mappedKey = 1 << key;
235 m_activeKeys &= ~mappedKey;
236 updateKeys();
237}
238
239void GameController::setAudioBufferSamples(int samples) {
240 if (m_gameOpen) {
241 GBAThreadInterrupt(&m_threadContext);
242 m_threadContext.audioBuffers = samples;
243 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
244 GBAThreadContinue(&m_threadContext);
245 } else {
246 m_threadContext.audioBuffers = samples;
247
248 }
249 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
250}
251
252void GameController::setFPSTarget(float fps) {
253 if (m_gameOpen) {
254 GBAThreadInterrupt(&m_threadContext);
255 m_threadContext.fpsTarget = fps;
256 GBAThreadContinue(&m_threadContext);
257 } else {
258 m_threadContext.fpsTarget = fps;
259 }
260 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
261}
262
263void GameController::loadState(int slot) {
264 GBAThreadInterrupt(&m_threadContext);
265 GBALoadState(m_threadContext.gba, m_threadContext.stateDir, slot);
266 GBAThreadContinue(&m_threadContext);
267 emit stateLoaded(&m_threadContext);
268 emit frameAvailable(m_drawContext);
269}
270
271void GameController::saveState(int slot) {
272 GBAThreadInterrupt(&m_threadContext);
273 GBASaveState(m_threadContext.gba, m_threadContext.stateDir, slot, true);
274 GBAThreadContinue(&m_threadContext);
275}
276
277void GameController::setVideoSync(bool set) {
278 m_videoSync = set;
279 if (!m_turbo && m_gameOpen) {
280 GBAThreadInterrupt(&m_threadContext);
281 m_threadContext.sync.videoFrameWait = set;
282 GBAThreadContinue(&m_threadContext);
283 }
284}
285
286void GameController::setAudioSync(bool set) {
287 m_audioSync = set;
288 if (!m_turbo && m_gameOpen) {
289 GBAThreadInterrupt(&m_threadContext);
290 m_threadContext.sync.audioWait = set;
291 GBAThreadContinue(&m_threadContext);
292 }
293}
294
295void GameController::setFrameskip(int skip) {
296 m_threadContext.frameskip = skip;
297}
298
299void GameController::setTurbo(bool set, bool forced) {
300 if (m_turboForced && !forced) {
301 return;
302 }
303 m_turbo = set;
304 if (set) {
305 m_turboForced = forced;
306 } else {
307 m_turboForced = false;
308 }
309 if (m_gameOpen) {
310 GBAThreadInterrupt(&m_threadContext);
311 m_threadContext.sync.audioWait = set ? false : m_audioSync;
312 m_threadContext.sync.videoFrameWait = set ? false : m_videoSync;
313 GBAThreadContinue(&m_threadContext);
314 }
315}
316
317void GameController::setAVStream(GBAAVStream* stream) {
318 if (m_gameOpen) {
319 GBAThreadInterrupt(&m_threadContext);
320 m_threadContext.stream = stream;
321 GBAThreadContinue(&m_threadContext);
322 } else {
323 m_threadContext.stream = stream;
324 }
325}
326
327void GameController::clearAVStream() {
328 if (m_gameOpen) {
329 GBAThreadInterrupt(&m_threadContext);
330 m_threadContext.stream = nullptr;
331 GBAThreadContinue(&m_threadContext);
332 } else {
333 m_threadContext.stream = nullptr;
334 }
335}
336
337void GameController::updateKeys() {
338 int activeKeys = m_activeKeys;
339#ifdef BUILD_SDL
340 activeKeys |= m_activeButtons;
341#endif
342 m_threadContext.activeKeys = activeKeys;
343}
344
345#ifdef BUILD_SDL
346void GameController::testSDLEvents() {
347 if (!m_inputController) {
348 return;
349 }
350
351 m_activeButtons = m_inputController->testSDLEvents();
352 updateKeys();
353}
354#endif