all repos — mgba @ 24094ecdf04cdeda17711141a27bfa0bc30de467

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	GBAKey key = mapKey(event->key());
158	if (key == GBA_KEY_NONE) {
159		QWidget::keyPressEvent(event);
160		return;
161	}
162	m_controller->keyPressed(key);
163	event->accept();
164}
165
166void Window::keyReleaseEvent(QKeyEvent* event) {
167	if (event->isAutoRepeat()) {
168		QWidget::keyReleaseEvent(event);
169		return;
170	}
171	GBAKey key = mapKey(event->key());
172	if (key == GBA_KEY_NONE) {
173		QWidget::keyPressEvent(event);
174		return;
175	}
176	m_controller->keyReleased(key);
177	event->accept();
178}
179
180void Window::resizeEvent(QResizeEvent*) {
181	redoLogo();
182}
183
184void Window::closeEvent(QCloseEvent* event) {
185	emit shutdown();
186	QMainWindow::closeEvent(event);
187}
188
189void Window::toggleFullScreen() {
190	if (isFullScreen()) {
191		showNormal();
192	} else {
193		showFullScreen();
194	}
195}
196
197void Window::gameStarted(GBAThread* context) {
198	emit startDrawing(m_controller->drawContext(), context);
199	foreach (QAction* action, m_gameActions) {
200		action->setDisabled(false);
201	}
202	char title[13] = { '\0' };
203	GBAGetGameTitle(context->gba, title);
204	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
205	attachWidget(m_display);
206	m_screenWidget->setScaledContents(true);
207}
208
209void Window::gameStopped() {
210	foreach (QAction* action, m_gameActions) {
211		action->setDisabled(true);
212	}
213	setWindowTitle(tr(PROJECT_NAME));
214	detachWidget(m_display);
215	m_screenWidget->setScaledContents(false);
216	redoLogo();
217}
218
219void Window::redoLogo() {
220	if (m_controller->isLoaded()) {
221		return;
222	}
223	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
224	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
225	m_screenWidget->setPixmap(logo);
226}
227
228void Window::openStateWindow(LoadSave ls) {
229	if (m_stateWindow) {
230		return;
231	}
232	bool wasPaused = m_controller->isPaused();
233	m_stateWindow = new LoadSaveState(m_controller);
234	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
235	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
236	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
237		m_screenWidget->layout()->removeWidget(m_stateWindow);
238		m_stateWindow = nullptr;
239		setFocus();
240	});
241	if (!wasPaused) {
242		m_controller->setPaused(true);
243		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
244	}
245	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
246	m_stateWindow->setMode(ls);
247	attachWidget(m_stateWindow);
248}
249
250void Window::setupMenu(QMenuBar* menubar) {
251	menubar->clear();
252	QMenu* fileMenu = menubar->addMenu(tr("&File"));
253	fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
254	fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS()));
255
256	fileMenu->addSeparator();
257
258	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
259	loadState->setShortcut(tr("Ctrl+L"));
260	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
261	m_gameActions.append(loadState);
262	fileMenu->addAction(loadState);
263
264	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
265	saveState->setShortcut(tr("Ctrl+S"));
266	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
267	m_gameActions.append(saveState);
268	fileMenu->addAction(saveState);
269
270	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
271	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
272	int i;
273	for (i = 1; i < 10; ++i) {
274		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
275		quickLoad->setShortcut(tr("F%1").arg(i));
276		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
277		m_gameActions.append(quickLoad);
278		quickLoadMenu->addAction(quickLoad);
279
280		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
281		quickSave->setShortcut(tr("Shift+F%1").arg(i));
282		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
283		m_gameActions.append(quickSave);
284		quickSaveMenu->addAction(quickSave);
285	}
286
287#ifdef USE_PNG
288	fileMenu->addSeparator();
289	QAction* screenshot = new QAction(tr("Take &screenshot"), fileMenu);
290	screenshot->setShortcut(tr("F12"));
291	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
292	m_gameActions.append(screenshot);
293	fileMenu->addAction(screenshot);
294#endif
295
296#ifndef Q_OS_MAC
297	fileMenu->addSeparator();
298	fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
299#endif
300
301	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
302	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
303	reset->setShortcut(tr("Ctrl+R"));
304	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
305	m_gameActions.append(reset);
306	emulationMenu->addAction(reset);
307
308	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
309	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
310	m_gameActions.append(shutdown);
311	emulationMenu->addAction(shutdown);
312	emulationMenu->addSeparator();
313
314	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
315	pause->setChecked(false);
316	pause->setCheckable(true);
317	pause->setShortcut(tr("Ctrl+P"));
318	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
319	connect(m_controller, &GameController::gamePaused, [this, pause]() {
320		pause->setChecked(true);
321
322		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
323		QPixmap pixmap;
324		pixmap.convertFromImage(currentImage.rgbSwapped());
325		m_screenWidget->setPixmap(pixmap);
326	});
327	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
328	m_gameActions.append(pause);
329	emulationMenu->addAction(pause);
330
331	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
332	frameAdvance->setShortcut(tr("Ctrl+N"));
333	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
334	m_gameActions.append(frameAdvance);
335	emulationMenu->addAction(frameAdvance);
336
337	QMenu* target = emulationMenu->addMenu("FPS target");
338	QAction* setTarget = new QAction(tr("15"), emulationMenu);
339	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
340	target->addAction(setTarget);
341	setTarget = new QAction(tr("30"), emulationMenu);
342	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
343	target->addAction(setTarget);
344	setTarget = new QAction(tr("45"), emulationMenu);
345	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
346	target->addAction(setTarget);
347	setTarget = new QAction(tr("60"), emulationMenu);
348	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
349	target->addAction(setTarget);
350	setTarget = new QAction(tr("90"), emulationMenu);
351	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
352	target->addAction(setTarget);
353	setTarget = new QAction(tr("120"), emulationMenu);
354	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
355	target->addAction(setTarget);
356	setTarget = new QAction(tr("240"), emulationMenu);
357	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
358	target->addAction(setTarget);
359
360	emulationMenu->addSeparator();
361
362	QAction* videoSync = new QAction(tr("Sync to &video"), emulationMenu);
363	videoSync->setCheckable(true);
364	videoSync->setChecked(GameController::VIDEO_SYNC);
365	connect(videoSync, SIGNAL(triggered(bool)), m_controller, SLOT(setVideoSync(bool)));
366	emulationMenu->addAction(videoSync);
367
368	QAction* audioSync = new QAction(tr("Sync to &audio"), emulationMenu);
369	audioSync->setCheckable(true);
370	audioSync->setChecked(GameController::AUDIO_SYNC);
371	connect(audioSync, SIGNAL(triggered(bool)), m_controller, SLOT(setAudioSync(bool)));
372	emulationMenu->addAction(audioSync);
373
374	QMenu* videoMenu = menubar->addMenu(tr("&Video"));
375	QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
376	QAction* setSize = new QAction(tr("1x"), videoMenu);
377	connect(setSize, &QAction::triggered, [this]() {
378		showNormal();
379		resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
380	});
381	frameMenu->addAction(setSize);
382	setSize = new QAction(tr("2x"), videoMenu);
383	connect(setSize, &QAction::triggered, [this]() {
384		showNormal();
385		resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
386	});
387	frameMenu->addAction(setSize);
388	setSize = new QAction(tr("3x"), videoMenu);
389	connect(setSize, &QAction::triggered, [this]() {
390		showNormal();
391		resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
392	});
393	frameMenu->addAction(setSize);
394	setSize = new QAction(tr("4x"), videoMenu);
395	connect(setSize, &QAction::triggered, [this]() {
396		showNormal();
397		resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
398	});
399	frameMenu->addAction(setSize);
400	frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
401
402	QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
403	QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
404	QAction* setBuffer = new QAction(tr("512"), buffersMenu);
405	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
406	buffersMenu->addAction(setBuffer);
407	setBuffer = new QAction(tr("1024"), buffersMenu);
408	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
409	buffersMenu->addAction(setBuffer);
410	setBuffer = new QAction(tr("2048"), buffersMenu);
411	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
412	buffersMenu->addAction(setBuffer);
413
414	QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
415	QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
416	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
417	debuggingMenu->addAction(viewLogs);
418#ifdef USE_GDB_STUB
419	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
420	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
421	debuggingMenu->addAction(gdbWindow);
422#endif
423
424	foreach (QAction* action, m_gameActions) {
425		action->setDisabled(true);
426	}
427}
428
429void Window::attachWidget(QWidget* widget) {
430	m_screenWidget->layout()->addWidget(widget);
431	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
432}
433
434void Window::detachWidget(QWidget* widget) {
435	m_screenWidget->layout()->removeWidget(widget);
436}