src/platform/qt/Window.cpp (view raw)
1#include "Window.h"
2
3#include <QFileDialog>
4#include <QKeyEvent>
5
6extern "C" {
7#include "gba.h"
8}
9
10using namespace QGBA;
11
12Window::Window(QWidget* parent) : QMainWindow(parent) {
13 setupUi(this);
14
15 m_controller = new GameController(this);
16 m_display = new Display(this);
17 setCentralWidget(m_display);
18 connect(m_controller, SIGNAL(frameAvailable(const QImage&)), m_display, SLOT(draw(const QImage&)));
19 connect(m_controller, SIGNAL(audioDeviceAvailable(GBAAudio*)), this, SLOT(setupAudio(GBAAudio*)));
20
21 connect(actionOpen, SIGNAL(triggered()), this, SLOT(selectROM()));
22}
23
24GBAKey Window::mapKey(int qtKey) {
25 switch (qtKey) {
26 case Qt::Key_Z:
27 return GBA_KEY_A;
28 break;
29 case Qt::Key_X:
30 return GBA_KEY_B;
31 break;
32 case Qt::Key_A:
33 return GBA_KEY_L;
34 break;
35 case Qt::Key_S:
36 return GBA_KEY_R;
37 break;
38 case Qt::Key_Return:
39 return GBA_KEY_START;
40 break;
41 case Qt::Key_Backspace:
42 return GBA_KEY_SELECT;
43 break;
44 case Qt::Key_Up:
45 return GBA_KEY_UP;
46 break;
47 case Qt::Key_Down:
48 return GBA_KEY_DOWN;
49 break;
50 case Qt::Key_Left:
51 return GBA_KEY_LEFT;
52 break;
53 case Qt::Key_Right:
54 return GBA_KEY_RIGHT;
55 break;
56 default:
57 return GBA_KEY_NONE;
58 }
59}
60
61void Window::selectROM() {
62 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
63 if (!filename.isEmpty()) {
64 m_controller->loadGame(filename);
65 }
66}
67
68void Window::keyPressEvent(QKeyEvent* event) {
69 if (event->isAutoRepeat()) {
70 QWidget::keyPressEvent(event);
71 return;
72 }
73 GBAKey key = mapKey(event->key());
74 if (key == GBA_KEY_NONE) {
75 QWidget::keyPressEvent(event);
76 return;
77 }
78 m_controller->keyPressed(key);
79 event->accept();
80}
81
82void Window::keyReleaseEvent(QKeyEvent* event) {
83 if (event->isAutoRepeat()) {
84 QWidget::keyReleaseEvent(event);
85 return;
86 }
87 GBAKey key = mapKey(event->key());
88 if (key == GBA_KEY_NONE) {
89 QWidget::keyPressEvent(event);
90 return;
91 }
92 m_controller->keyReleased(key);
93 event->accept();
94}
95
96void Window::setupAudio(GBAAudio* audio) {
97 AudioDevice::Thread* thread = new AudioDevice::Thread(this);
98 thread->setInput(audio);
99 thread->start(QThread::HighPriority);
100}