src/platform/qt/GameController.cpp (view raw)
1#include "GameController.h"
2
3extern "C" {
4#include "gba.h"
5#include "renderers/video-software.h"
6}
7
8using namespace QGBA;
9
10GameController::GameController(QObject* parent)
11 : QObject(parent)
12 , m_drawContext(256, 256, QImage::Format_RGB32)
13 , m_audioContext(0)
14{
15 m_renderer = new GBAVideoSoftwareRenderer;
16 GBAVideoSoftwareRendererCreate(m_renderer);
17 m_renderer->outputBuffer = (color_t*) m_drawContext.bits();
18 m_renderer->outputBufferStride = m_drawContext.bytesPerLine() / 4;
19 m_threadContext.useDebugger = 0;
20 m_threadContext.frameskip = 0;
21 m_threadContext.renderer = &m_renderer->d;
22 m_threadContext.frameskip = 0;
23 m_threadContext.sync.videoFrameWait = 0;
24 m_threadContext.sync.audioWait = 1;
25 m_threadContext.startCallback = [] (GBAThread* context) {
26 GameController* controller = static_cast<GameController*>(context->userData);
27 controller->audioDeviceAvailable(&context->gba->audio);
28 };
29 m_threadContext.cleanCallback = 0;
30 m_threadContext.frameCallback = [] (GBAThread* context) {
31 GameController* controller = static_cast<GameController*>(context->userData);
32 controller->frameAvailable(controller->m_drawContext);
33 };
34 m_threadContext.userData = this;
35 m_threadContext.rewindBufferCapacity = 0;
36}
37
38GameController::~GameController() {
39 GBAThreadEnd(&m_threadContext);
40 GBAThreadJoin(&m_threadContext);
41 delete m_renderer;
42}
43
44bool GameController::loadGame(const QString& path) {
45 m_rom = new QFile(path);
46 if (!m_rom->open(QIODevice::ReadOnly)) {
47 delete m_rom;
48 m_rom = 0;
49 return false;
50 }
51 m_threadContext.fd = m_rom->handle();
52 m_threadContext.fname = path.toLocal8Bit().constData();
53 GBAThreadStart(&m_threadContext);
54 return true;
55}