all repos — mgba @ 05ec732a83eca1a1291b163017d10f009905212b

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#include <QStackedLayout>
  8
  9#include "GameController.h"
 10#include "GDBWindow.h"
 11#include "GDBController.h"
 12#include "LoadSaveState.h"
 13#include "LogView.h"
 14
 15extern "C" {
 16#include "platform/commandline.h"
 17}
 18
 19using namespace QGBA;
 20
 21Window::Window(QWidget* parent)
 22	: QMainWindow(parent)
 23	, m_logView(new LogView())
 24	, m_stateWindow(nullptr)
 25	, m_screenWidget(new QLabel())
 26	, m_logo(":/res/mgba-1024.png")
 27#ifdef USE_GDB_STUB
 28	, m_gdbController(nullptr)
 29#endif
 30{
 31	setWindowTitle(PROJECT_NAME);
 32	m_controller = new GameController(this);
 33
 34	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
 35	format.setSwapInterval(1);
 36	m_display = new Display(format);
 37
 38	m_screenWidget->setLayout(new QStackedLayout());
 39	m_screenWidget->layout()->setContentsMargins(0, 0, 0, 0);
 40	m_screenWidget->setAlignment(Qt::AlignCenter);
 41	m_screenWidget->setMinimumSize(m_display->minimumSize());
 42	m_screenWidget->setSizePolicy(m_display->sizePolicy());
 43	m_screenWidget->resize(m_display->minimumSize() * 2);
 44	QPalette palette = m_screenWidget->palette();
 45	palette.setColor(m_screenWidget->backgroundRole(), Qt::black);
 46	m_screenWidget->setPalette(palette);
 47	m_screenWidget->setAutoFillBackground(true);
 48	setCentralWidget(m_screenWidget);
 49
 50	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 51	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
 52	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 53	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
 54	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 55	connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
 56	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 57	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 58	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 59	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 60	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 61
 62	setupMenu(menuBar());
 63}
 64
 65Window::~Window() {
 66	delete m_logView;
 67}
 68
 69GBAKey Window::mapKey(int qtKey) {
 70	switch (qtKey) {
 71	case Qt::Key_Z:
 72		return GBA_KEY_A;
 73		break;
 74	case Qt::Key_X:
 75		return GBA_KEY_B;
 76		break;
 77	case Qt::Key_A:
 78		return GBA_KEY_L;
 79		break;
 80	case Qt::Key_S:
 81		return GBA_KEY_R;
 82		break;
 83	case Qt::Key_Return:
 84		return GBA_KEY_START;
 85		break;
 86	case Qt::Key_Backspace:
 87		return GBA_KEY_SELECT;
 88		break;
 89	case Qt::Key_Up:
 90		return GBA_KEY_UP;
 91		break;
 92	case Qt::Key_Down:
 93		return GBA_KEY_DOWN;
 94		break;
 95	case Qt::Key_Left:
 96		return GBA_KEY_LEFT;
 97		break;
 98	case Qt::Key_Right:
 99		return GBA_KEY_RIGHT;
100		break;
101	default:
102		return GBA_KEY_NONE;
103	}
104}
105
106void Window::optionsPassed(StartupOptions* opts) {
107	if (opts->logLevel) {
108		m_logView->setLevels(opts->logLevel);
109	}
110
111	if (opts->bios) {
112		m_controller->loadBIOS(opts->bios);
113	}
114
115	if (opts->fname) {
116		m_controller->loadGame(opts->fname, opts->dirmode);
117	}
118
119	// TODO:
120	// - patch
121	// - frameskip;
122	// - rewindBufferCapacity
123	// - rewindBufferInterval
124	// - DebuggerType debuggerType
125	// - debugAtStart
126}
127
128void Window::selectROM() {
129	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
130	if (!filename.isEmpty()) {
131		m_controller->loadGame(filename);
132	}
133}
134
135void Window::selectBIOS() {
136	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
137	if (!filename.isEmpty()) {
138		m_controller->loadBIOS(filename);
139	}
140}
141
142#ifdef USE_GDB_STUB
143void Window::gdbOpen() {
144	if (!m_gdbController) {
145		m_gdbController = new GDBController(m_controller, this);
146	}
147	GDBWindow* window = new GDBWindow(m_gdbController);
148	window->show();
149}
150#endif
151
152void Window::keyPressEvent(QKeyEvent* event) {
153	if (event->isAutoRepeat()) {
154		QWidget::keyPressEvent(event);
155		return;
156	}
157	if (event->key() == Qt::Key_Tab) {
158		m_controller->setTurbo(true, false);
159	}
160	GBAKey key = mapKey(event->key());
161	if (key == GBA_KEY_NONE) {
162		QWidget::keyPressEvent(event);
163		return;
164	}
165	m_controller->keyPressed(key);
166	event->accept();
167}
168
169void Window::keyReleaseEvent(QKeyEvent* event) {
170	if (event->isAutoRepeat()) {
171		QWidget::keyReleaseEvent(event);
172		return;
173	}
174	if (event->key() == Qt::Key_Tab) {
175		m_controller->setTurbo(false, false);
176	}
177	GBAKey key = mapKey(event->key());
178	if (key == GBA_KEY_NONE) {
179		QWidget::keyPressEvent(event);
180		return;
181	}
182	m_controller->keyReleased(key);
183	event->accept();
184}
185
186void Window::resizeEvent(QResizeEvent*) {
187	redoLogo();
188}
189
190void Window::closeEvent(QCloseEvent* event) {
191	emit shutdown();
192	QMainWindow::closeEvent(event);
193}
194
195void Window::toggleFullScreen() {
196	if (isFullScreen()) {
197		showNormal();
198	} else {
199		showFullScreen();
200	}
201}
202
203void Window::gameStarted(GBAThread* context) {
204	emit startDrawing(m_controller->drawContext(), context);
205	foreach (QAction* action, m_gameActions) {
206		action->setDisabled(false);
207	}
208	char title[13] = { '\0' };
209	GBAGetGameTitle(context->gba, title);
210	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
211	attachWidget(m_display);
212	m_screenWidget->setScaledContents(true);
213}
214
215void Window::gameStopped() {
216	foreach (QAction* action, m_gameActions) {
217		action->setDisabled(true);
218	}
219	setWindowTitle(tr(PROJECT_NAME));
220	detachWidget(m_display);
221	m_screenWidget->setScaledContents(false);
222	redoLogo();
223}
224
225void Window::redoLogo() {
226	if (m_controller->isLoaded()) {
227		return;
228	}
229	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
230	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
231	m_screenWidget->setPixmap(logo);
232}
233
234void Window::openStateWindow(LoadSave ls) {
235	if (m_stateWindow) {
236		return;
237	}
238	bool wasPaused = m_controller->isPaused();
239	m_stateWindow = new LoadSaveState(m_controller);
240	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
241	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
242	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
243		m_screenWidget->layout()->removeWidget(m_stateWindow);
244		m_stateWindow = nullptr;
245		setFocus();
246	});
247	if (!wasPaused) {
248		m_controller->setPaused(true);
249		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
250	}
251	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
252	m_stateWindow->setMode(ls);
253	attachWidget(m_stateWindow);
254}
255
256void Window::setupMenu(QMenuBar* menubar) {
257	menubar->clear();
258	QMenu* fileMenu = menubar->addMenu(tr("&File"));
259	fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
260	fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS()));
261
262	fileMenu->addSeparator();
263
264	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
265	loadState->setShortcut(tr("Ctrl+L"));
266	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
267	m_gameActions.append(loadState);
268	fileMenu->addAction(loadState);
269
270	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
271	saveState->setShortcut(tr("Ctrl+S"));
272	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
273	m_gameActions.append(saveState);
274	fileMenu->addAction(saveState);
275
276	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
277	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
278	int i;
279	for (i = 1; i < 10; ++i) {
280		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
281		quickLoad->setShortcut(tr("F%1").arg(i));
282		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
283		m_gameActions.append(quickLoad);
284		quickLoadMenu->addAction(quickLoad);
285
286		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
287		quickSave->setShortcut(tr("Shift+F%1").arg(i));
288		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
289		m_gameActions.append(quickSave);
290		quickSaveMenu->addAction(quickSave);
291	}
292
293#ifdef USE_PNG
294	fileMenu->addSeparator();
295	QAction* screenshot = new QAction(tr("Take &screenshot"), fileMenu);
296	screenshot->setShortcut(tr("F12"));
297	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
298	m_gameActions.append(screenshot);
299	fileMenu->addAction(screenshot);
300#endif
301
302#ifndef Q_OS_MAC
303	fileMenu->addSeparator();
304	fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
305#endif
306
307	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
308	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
309	reset->setShortcut(tr("Ctrl+R"));
310	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
311	m_gameActions.append(reset);
312	emulationMenu->addAction(reset);
313
314	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
315	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
316	m_gameActions.append(shutdown);
317	emulationMenu->addAction(shutdown);
318	emulationMenu->addSeparator();
319
320	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
321	pause->setChecked(false);
322	pause->setCheckable(true);
323	pause->setShortcut(tr("Ctrl+P"));
324	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
325	connect(m_controller, &GameController::gamePaused, [this, pause]() {
326		pause->setChecked(true);
327
328		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
329		QPixmap pixmap;
330		pixmap.convertFromImage(currentImage.rgbSwapped());
331		m_screenWidget->setPixmap(pixmap);
332	});
333	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
334	m_gameActions.append(pause);
335	emulationMenu->addAction(pause);
336
337	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
338	frameAdvance->setShortcut(tr("Ctrl+N"));
339	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
340	m_gameActions.append(frameAdvance);
341	emulationMenu->addAction(frameAdvance);
342
343	QMenu* target = emulationMenu->addMenu("FPS target");
344	QAction* setTarget = new QAction(tr("15"), emulationMenu);
345	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
346	target->addAction(setTarget);
347	setTarget = new QAction(tr("30"), emulationMenu);
348	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
349	target->addAction(setTarget);
350	setTarget = new QAction(tr("45"), emulationMenu);
351	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
352	target->addAction(setTarget);
353	setTarget = new QAction(tr("60"), emulationMenu);
354	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
355	target->addAction(setTarget);
356	setTarget = new QAction(tr("90"), emulationMenu);
357	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
358	target->addAction(setTarget);
359	setTarget = new QAction(tr("120"), emulationMenu);
360	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
361	target->addAction(setTarget);
362	setTarget = new QAction(tr("240"), emulationMenu);
363	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
364	target->addAction(setTarget);
365
366	emulationMenu->addSeparator();
367
368	QAction* turbo = new QAction(tr("T&urbo"), emulationMenu);
369	turbo->setCheckable(true);
370	turbo->setChecked(false);
371	turbo->setShortcut(tr("Shift+Tab"));
372	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
373	emulationMenu->addAction(turbo);
374
375	QAction* videoSync = new QAction(tr("Sync to &video"), emulationMenu);
376	videoSync->setCheckable(true);
377	videoSync->setChecked(GameController::VIDEO_SYNC);
378	connect(videoSync, SIGNAL(triggered(bool)), m_controller, SLOT(setVideoSync(bool)));
379	emulationMenu->addAction(videoSync);
380
381	QAction* audioSync = new QAction(tr("Sync to &audio"), emulationMenu);
382	audioSync->setCheckable(true);
383	audioSync->setChecked(GameController::AUDIO_SYNC);
384	connect(audioSync, SIGNAL(triggered(bool)), m_controller, SLOT(setAudioSync(bool)));
385	emulationMenu->addAction(audioSync);
386
387	QMenu* videoMenu = menubar->addMenu(tr("&Video"));
388	QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
389	QAction* setSize = new QAction(tr("1x"), videoMenu);
390	connect(setSize, &QAction::triggered, [this]() {
391		showNormal();
392		resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
393	});
394	frameMenu->addAction(setSize);
395	setSize = new QAction(tr("2x"), videoMenu);
396	connect(setSize, &QAction::triggered, [this]() {
397		showNormal();
398		resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
399	});
400	frameMenu->addAction(setSize);
401	setSize = new QAction(tr("3x"), videoMenu);
402	connect(setSize, &QAction::triggered, [this]() {
403		showNormal();
404		resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
405	});
406	frameMenu->addAction(setSize);
407	setSize = new QAction(tr("4x"), videoMenu);
408	connect(setSize, &QAction::triggered, [this]() {
409		showNormal();
410		resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
411	});
412	frameMenu->addAction(setSize);
413	frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
414
415	QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
416	QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
417	QAction* setBuffer = new QAction(tr("512"), buffersMenu);
418	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
419	buffersMenu->addAction(setBuffer);
420	setBuffer = new QAction(tr("1024"), buffersMenu);
421	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
422	buffersMenu->addAction(setBuffer);
423	setBuffer = new QAction(tr("2048"), buffersMenu);
424	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
425	buffersMenu->addAction(setBuffer);
426
427	QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
428	QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
429	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
430	debuggingMenu->addAction(viewLogs);
431#ifdef USE_GDB_STUB
432	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
433	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
434	debuggingMenu->addAction(gdbWindow);
435#endif
436
437	foreach (QAction* action, m_gameActions) {
438		action->setDisabled(true);
439	}
440}
441
442void Window::attachWidget(QWidget* widget) {
443	m_screenWidget->layout()->addWidget(widget);
444	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
445}
446
447void Window::detachWidget(QWidget* widget) {
448	m_screenWidget->layout()->removeWidget(widget);
449}