all repos — mgba @ f49494cd1d76255d8969057469c69fd82ee0a409

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