all repos — mgba @ 0579ececb343b13c5be40dc4f1ce60fb5a8812c6

mGBA Game Boy Advance Emulator

src/platform/qt/Window.cpp (view raw)

  1#include "Window.h"
  2
  3#include <QFileDialog>
  4#include <QKeyEvent>
  5#include <QKeySequence>
  6#include <QMenuBar>
  7
  8#include "GameController.h"
  9#include "GDBWindow.h"
 10#include "GDBController.h"
 11#include "LoadSaveState.h"
 12#include "LogView.h"
 13
 14using namespace QGBA;
 15
 16Window::Window(QWidget* parent)
 17	: QMainWindow(parent)
 18#ifdef USE_GDB_STUB
 19	, m_gdbController(nullptr)
 20#endif
 21{
 22	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 23	setMinimumSize(240, 160);
 24
 25	m_controller = new GameController(this);
 26	m_logView = new LogView();
 27
 28	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
 29	format.setSwapInterval(1);
 30	m_display = new Display(format);
 31	setCentralWidget(m_display);
 32	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 33	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
 34	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 35	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 36	connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
 37	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 38	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 39	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 40	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 41	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 42
 43	setupMenu(menuBar());
 44}
 45
 46Window::~Window() {
 47	delete m_logView;
 48}
 49
 50GBAKey Window::mapKey(int qtKey) {
 51	switch (qtKey) {
 52	case Qt::Key_Z:
 53		return GBA_KEY_A;
 54		break;
 55	case Qt::Key_X:
 56		return GBA_KEY_B;
 57		break;
 58	case Qt::Key_A:
 59		return GBA_KEY_L;
 60		break;
 61	case Qt::Key_S:
 62		return GBA_KEY_R;
 63		break;
 64	case Qt::Key_Return:
 65		return GBA_KEY_START;
 66		break;
 67	case Qt::Key_Backspace:
 68		return GBA_KEY_SELECT;
 69		break;
 70	case Qt::Key_Up:
 71		return GBA_KEY_UP;
 72		break;
 73	case Qt::Key_Down:
 74		return GBA_KEY_DOWN;
 75		break;
 76	case Qt::Key_Left:
 77		return GBA_KEY_LEFT;
 78		break;
 79	case Qt::Key_Right:
 80		return GBA_KEY_RIGHT;
 81		break;
 82	default:
 83		return GBA_KEY_NONE;
 84	}
 85}
 86
 87void Window::selectROM() {
 88	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
 89	if (!filename.isEmpty()) {
 90		m_controller->loadGame(filename);
 91	}
 92}
 93
 94#ifdef USE_GDB_STUB
 95void Window::gdbOpen() {
 96	if (!m_gdbController) {
 97		m_gdbController = new GDBController(m_controller, this);
 98	}
 99	GDBWindow* window = new GDBWindow(m_gdbController);
100	window->show();
101}
102#endif
103
104void Window::keyPressEvent(QKeyEvent* event) {
105	if (event->isAutoRepeat()) {
106		QWidget::keyPressEvent(event);
107		return;
108	}
109	GBAKey key = mapKey(event->key());
110	if (key == GBA_KEY_NONE) {
111		QWidget::keyPressEvent(event);
112		return;
113	}
114	m_controller->keyPressed(key);
115	event->accept();
116}
117
118void Window::keyReleaseEvent(QKeyEvent* event) {
119	if (event->isAutoRepeat()) {
120		QWidget::keyReleaseEvent(event);
121		return;
122	}
123	GBAKey key = mapKey(event->key());
124	if (key == GBA_KEY_NONE) {
125		QWidget::keyPressEvent(event);
126		return;
127	}
128	m_controller->keyReleased(key);
129	event->accept();
130}
131
132void Window::closeEvent(QCloseEvent* event) {
133	emit shutdown();
134	QMainWindow::closeEvent(event);
135}
136
137void Window::gameStarted(GBAThread* context) {
138	emit startDrawing(m_controller->drawContext(), context);
139	foreach (QAction* action, m_gameActions) {
140		action->setDisabled(false);
141	}
142}
143
144void Window::gameStopped() {
145	foreach (QAction* action, m_gameActions) {
146		action->setDisabled(true);
147	}
148}
149
150void Window::openStateWindow(LoadSave ls) {
151	LoadSaveState* window = new LoadSaveState(m_controller);
152	window->setAttribute(Qt::WA_DeleteOnClose);
153	connect(this, SIGNAL(shutdown()), window, SLOT(hide()));
154	window->setMode(ls);
155	window->show();
156}
157
158void Window::setupMenu(QMenuBar* menubar) {
159	menubar->clear();
160	QMenu* fileMenu = menubar->addMenu(tr("&File"));
161	fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
162
163	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
164	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
165	reset->setShortcut(tr("Ctrl+R"));
166	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
167	m_gameActions.append(reset);
168	emulationMenu->addAction(reset);
169
170	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
171	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
172	m_gameActions.append(shutdown);
173	emulationMenu->addAction(shutdown);
174	emulationMenu->addSeparator();
175
176	QAction* loadState = new QAction(tr("&Load state"), emulationMenu);
177	loadState->setShortcut(tr("Ctrl+L"));
178	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
179	m_gameActions.append(loadState);
180	emulationMenu->addAction(loadState);
181
182	QAction* saveState = new QAction(tr("&Save state"), emulationMenu);
183	saveState->setShortcut(tr("Ctrl+S"));
184	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
185	m_gameActions.append(saveState);
186	emulationMenu->addAction(saveState);
187	emulationMenu->addSeparator();
188
189	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
190	pause->setChecked(false);
191	pause->setCheckable(true);
192	pause->setShortcut(tr("Ctrl+P"));
193	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
194	connect(m_controller, &GameController::gamePaused, [pause]() { pause->setChecked(true); });
195	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
196	m_gameActions.append(pause);
197	emulationMenu->addAction(pause);
198
199	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
200	frameAdvance->setShortcut(tr("Ctrl+N"));
201	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
202	m_gameActions.append(frameAdvance);
203	emulationMenu->addAction(frameAdvance);
204
205	QMenu* target = emulationMenu->addMenu("FPS target");
206	QAction* setTarget = new QAction(tr("15"), emulationMenu);
207	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
208	target->addAction(setTarget);
209	setTarget = new QAction(tr("30"), emulationMenu);
210	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
211	target->addAction(setTarget);
212	setTarget = new QAction(tr("45"), emulationMenu);
213	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
214	target->addAction(setTarget);
215	setTarget = new QAction(tr("60"), emulationMenu);
216	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
217	target->addAction(setTarget);
218	setTarget = new QAction(tr("90"), emulationMenu);
219	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
220	target->addAction(setTarget);
221	setTarget = new QAction(tr("120"), emulationMenu);
222	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
223	target->addAction(setTarget);
224	setTarget = new QAction(tr("240"), emulationMenu);
225	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
226	target->addAction(setTarget);
227
228	QMenu* videoMenu = menubar->addMenu(tr("&Video"));
229	QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
230	QAction* setSize = new QAction(tr("1x"), videoMenu);
231	connect(setSize, &QAction::triggered, [this]() {
232		showNormal();
233		resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
234	});
235	frameMenu->addAction(setSize);
236	setSize = new QAction(tr("2x"), videoMenu);
237	connect(setSize, &QAction::triggered, [this]() {
238		showNormal();
239		resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
240	});
241	frameMenu->addAction(setSize);
242	setSize = new QAction(tr("3x"), videoMenu);
243	connect(setSize, &QAction::triggered, [this]() {
244		showNormal();
245		resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
246	});
247	frameMenu->addAction(setSize);
248	setSize = new QAction(tr("4x"), videoMenu);
249	connect(setSize, &QAction::triggered, [this]() {
250		showNormal();
251		resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
252	});
253	frameMenu->addAction(setSize);
254	frameMenu->addAction(tr("Fullscreen"), this, SLOT(showFullScreen()), QKeySequence("Ctrl+F"));
255
256	QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
257	QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
258	QAction* setBuffer = new QAction(tr("512"), buffersMenu);
259	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
260	buffersMenu->addAction(setBuffer);
261	setBuffer = new QAction(tr("1024"), buffersMenu);
262	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
263	buffersMenu->addAction(setBuffer);
264	setBuffer = new QAction(tr("2048"), buffersMenu);
265	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
266	buffersMenu->addAction(setBuffer);
267
268	QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
269	QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
270	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
271	debuggingMenu->addAction(viewLogs);
272#ifdef USE_GDB_STUB
273	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
274	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
275	debuggingMenu->addAction(gdbWindow);
276#endif
277
278	foreach (QAction* action, m_gameActions) {
279		action->setDisabled(true);
280	}
281}