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#ifdef BUILD_SDL
25 SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
26 GBASDLInitEvents(&m_sdlEvents);
27 SDL_JoystickEventState(SDL_QUERY);
28#endif
29 m_renderer = new GBAVideoSoftwareRenderer;
30 GBAVideoSoftwareRendererCreate(m_renderer);
31 m_renderer->outputBuffer = (color_t*) m_drawContext;
32 m_renderer->outputBufferStride = 256;
33 m_threadContext = {
34 .state = THREAD_INITIALIZED,
35 .debugger = 0,
36 .frameskip = 0,
37 .bios = 0,
38 .renderer = &m_renderer->d,
39 .userData = this,
40 .rewindBufferCapacity = 0
41 };
42 m_threadContext.startCallback = [] (GBAThread* context) {
43 GameController* controller = static_cast<GameController*>(context->userData);
44 controller->m_audioProcessor->setInput(context);
45 controller->gameStarted(context);
46 };
47
48 m_threadContext.cleanCallback = [] (GBAThread* context) {
49 GameController* controller = static_cast<GameController*>(context->userData);
50 controller->gameStopped(context);
51 };
52
53 m_threadContext.frameCallback = [] (GBAThread* context) {
54 GameController* controller = static_cast<GameController*>(context->userData);
55 controller->m_pauseMutex.lock();
56 if (controller->m_pauseAfterFrame) {
57 GBAThreadPause(context);
58 controller->m_pauseAfterFrame = false;
59 }
60 controller->m_pauseMutex.unlock();
61 controller->frameAvailable(controller->m_drawContext);
62 };
63
64 m_audioThread->start();
65 m_audioProcessor->moveToThread(m_audioThread);
66 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
67
68#ifdef BUILD_SDL
69 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents()));
70#endif
71}
72
73GameController::~GameController() {
74 m_audioProcessor->pause();
75 m_audioThread->quit();
76 if (GBAThreadIsPaused(&m_threadContext)) {
77 GBAThreadUnpause(&m_threadContext);
78 }
79 GBAThreadEnd(&m_threadContext);
80 GBAThreadJoin(&m_threadContext);
81 delete m_renderer;
82}
83
84ARMDebugger* GameController::debugger() {
85 return m_threadContext.debugger;
86}
87
88void GameController::setDebugger(ARMDebugger* debugger) {
89 bool wasPaused = isPaused();
90 setPaused(true);
91 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
92 GBADetachDebugger(m_threadContext.gba);
93 }
94 m_threadContext.debugger = debugger;
95 if (m_threadContext.debugger && GBAThreadHasStarted(&m_threadContext)) {
96 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
97 }
98 setPaused(wasPaused);
99}
100
101void GameController::loadGame(const QString& path) {
102 closeGame();
103 m_threadContext.sync.videoFrameWait = 0;
104 m_threadContext.sync.audioWait = 1;
105 m_rom = new QFile(path);
106 if (!m_rom->open(QIODevice::ReadOnly)) {
107 delete m_rom;
108 m_rom = nullptr;
109 }
110
111 m_pauseAfterFrame = false;
112
113 m_threadContext.rom = VFileFromFD(m_rom->handle());
114 m_threadContext.fname = path.toLocal8Bit().constData();
115
116 GBAThreadStart(&m_threadContext);
117}
118
119void GameController::closeGame() {
120 if (!m_rom) {
121 return;
122 }
123 GBAThreadEnd(&m_threadContext);
124 GBAThreadJoin(&m_threadContext);
125 if (m_rom) {
126 m_rom->close();
127 delete m_rom;
128 m_rom = nullptr;
129 }
130 emit gameStopped(&m_threadContext);
131}
132
133bool GameController::isPaused() {
134 return GBAThreadIsPaused(&m_threadContext);
135}
136
137void GameController::setPaused(bool paused) {
138 if (paused == GBAThreadIsPaused(&m_threadContext)) {
139 return;
140 }
141 if (paused) {
142 GBAThreadPause(&m_threadContext);
143 m_audioProcessor->pause();
144 } else {
145 m_audioProcessor->start();
146 GBAThreadUnpause(&m_threadContext);
147 }
148}
149
150void GameController::frameAdvance() {
151 m_pauseMutex.lock();
152 m_pauseAfterFrame = true;
153 setPaused(false);
154 m_pauseMutex.unlock();
155}
156
157void GameController::keyPressed(int key) {
158 int mappedKey = 1 << key;
159 m_activeKeys |= mappedKey;
160 updateKeys();
161}
162
163void GameController::keyReleased(int key) {
164 int mappedKey = 1 << key;
165 m_activeKeys &= ~mappedKey;
166 updateKeys();
167}
168
169void GameController::setAudioBufferSamples(int samples) {
170 GBAThreadInterrupt(&m_threadContext);
171 m_threadContext.audioBuffers = samples;
172 GBAAudioResizeBuffer(&m_threadContext.gba->audio, samples);
173 GBAThreadContinue(&m_threadContext);
174 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
175}
176
177void GameController::updateKeys() {
178 int activeKeys = m_activeKeys;
179#ifdef BUILD_SDL
180 activeKeys |= m_activeButtons;
181#endif
182 m_threadContext.activeKeys = activeKeys;
183}
184
185#ifdef BUILD_SDL
186void GameController::testSDLEvents() {
187 SDL_Joystick* joystick = m_sdlEvents.joystick;
188 SDL_JoystickUpdate();
189 int numButtons = SDL_JoystickNumButtons(joystick);
190 m_activeButtons = 0;
191 int i;
192 for (i = 0; i < numButtons; ++i) {
193 GBAKey key = GBASDLMapButtonToKey(i);
194 if (key == GBA_KEY_NONE) {
195 continue;
196 }
197 if (SDL_JoystickGetButton(joystick, i)) {
198 m_activeButtons |= 1 << key;
199 }
200 }
201 int numHats = SDL_JoystickNumHats(joystick);
202 for (i = 0; i < numHats; ++i) {
203 int hat = SDL_JoystickGetHat(joystick, i);
204 if (hat & SDL_HAT_UP) {
205 m_activeButtons |= 1 << GBA_KEY_UP;
206 }
207 if (hat & SDL_HAT_LEFT) {
208 m_activeButtons |= 1 << GBA_KEY_LEFT;
209 }
210 if (hat & SDL_HAT_DOWN) {
211 m_activeButtons |= 1 << GBA_KEY_DOWN;
212 }
213 if (hat & SDL_HAT_RIGHT) {
214 m_activeButtons |= 1 << GBA_KEY_RIGHT;
215 }
216 }
217 updateKeys();
218}
219#endif