all repos — mgba @ a7dc4e3285a6e617e2c16bec95dd922b551f4b98

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