all repos — mgba @ aed170b6700314d0913038ea17f869b013a5d301

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