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 m_audioThread->wait();
91 if (GBAThreadIsPaused(&m_threadContext)) {
92 GBAThreadUnpause(&m_threadContext);
93 }
94 GBAThreadEnd(&m_threadContext);
95 GBAThreadJoin(&m_threadContext);
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 = 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_rom) {
141 m_rom->close();
142 delete m_rom;
143 m_rom = nullptr;
144 }
145 emit gameStopped(&m_threadContext);
146}
147
148bool GameController::isPaused() {
149 return GBAThreadIsPaused(&m_threadContext);
150}
151
152void GameController::setPaused(bool paused) {
153 if (paused == GBAThreadIsPaused(&m_threadContext)) {
154 return;
155 }
156 if (paused) {
157 GBAThreadPause(&m_threadContext);
158 emit gamePaused(&m_threadContext);
159 } else {
160 GBAThreadUnpause(&m_threadContext);
161 emit gameUnpaused(&m_threadContext);
162 }
163}
164
165void GameController::reset() {
166 GBAThreadReset(&m_threadContext);
167}
168
169void GameController::frameAdvance() {
170 m_pauseMutex.lock();
171 m_pauseAfterFrame = true;
172 setPaused(false);
173 m_pauseMutex.unlock();
174}
175
176void GameController::keyPressed(int key) {
177 int mappedKey = 1 << key;
178 m_activeKeys |= mappedKey;
179 updateKeys();
180}
181
182void GameController::keyReleased(int key) {
183 int mappedKey = 1 << key;
184 m_activeKeys &= ~mappedKey;
185 updateKeys();
186}
187
188void GameController::setAudioBufferSamples(int samples) {
189 GBAThreadInterrupt(&m_threadContext);
190 m_threadContext.audioBuffers = samples;
191 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
192 GBAThreadContinue(&m_threadContext);
193 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
194}
195
196void GameController::setFPSTarget(float fps) {
197 GBAThreadInterrupt(&m_threadContext);
198 m_threadContext.fpsTarget = fps;
199 GBAThreadContinue(&m_threadContext);
200 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
201}
202
203void GameController::updateKeys() {
204 int activeKeys = m_activeKeys;
205#ifdef BUILD_SDL
206 activeKeys |= m_activeButtons;
207#endif
208 m_threadContext.activeKeys = activeKeys;
209}
210
211#ifdef BUILD_SDL
212void GameController::testSDLEvents() {
213 SDL_Joystick* joystick = m_sdlEvents.joystick;
214 SDL_JoystickUpdate();
215 int numButtons = SDL_JoystickNumButtons(joystick);
216 m_activeButtons = 0;
217 int i;
218 for (i = 0; i < numButtons; ++i) {
219 GBAKey key = GBAInputMapKey(&m_threadContext.inputMap, SDL_BINDING_BUTTON, i);
220 if (key == GBA_KEY_NONE) {
221 continue;
222 }
223 if (SDL_JoystickGetButton(joystick, i)) {
224 m_activeButtons |= 1 << key;
225 }
226 }
227 int numHats = SDL_JoystickNumHats(joystick);
228 for (i = 0; i < numHats; ++i) {
229 int hat = SDL_JoystickGetHat(joystick, i);
230 if (hat & SDL_HAT_UP) {
231 m_activeButtons |= 1 << GBA_KEY_UP;
232 }
233 if (hat & SDL_HAT_LEFT) {
234 m_activeButtons |= 1 << GBA_KEY_LEFT;
235 }
236 if (hat & SDL_HAT_DOWN) {
237 m_activeButtons |= 1 << GBA_KEY_DOWN;
238 }
239 if (hat & SDL_HAT_RIGHT) {
240 m_activeButtons |= 1 << GBA_KEY_RIGHT;
241 }
242 }
243 updateKeys();
244}
245#endif