all repos — mgba @ 8e4a3439c05395f0c274db4d9089f75867cd71f4

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->fname) {
108		m_controller->loadGame(opts->fname, opts->dirmode);
109	}
110
111	if (opts->logLevel) {
112		m_logView->setLevels(opts->logLevel);
113	}
114
115	// TODO:
116	// - bios
117	// - patch
118	// - frameskip;
119	// - rewindBufferCapacity
120	// - rewindBufferInterval
121	// - DebuggerType debuggerType
122	// - debugAtStart
123}
124
125void Window::selectROM() {
126	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
127	if (!filename.isEmpty()) {
128		m_controller->loadGame(filename);
129	}
130}
131
132#ifdef USE_GDB_STUB
133void Window::gdbOpen() {
134	if (!m_gdbController) {
135		m_gdbController = new GDBController(m_controller, this);
136	}
137	GDBWindow* window = new GDBWindow(m_gdbController);
138	window->show();
139}
140#endif
141
142void Window::keyPressEvent(QKeyEvent* event) {
143	if (event->isAutoRepeat()) {
144		QWidget::keyPressEvent(event);
145		return;
146	}
147	GBAKey key = mapKey(event->key());
148	if (key == GBA_KEY_NONE) {
149		QWidget::keyPressEvent(event);
150		return;
151	}
152	m_controller->keyPressed(key);
153	event->accept();
154}
155
156void Window::keyReleaseEvent(QKeyEvent* event) {
157	if (event->isAutoRepeat()) {
158		QWidget::keyReleaseEvent(event);
159		return;
160	}
161	GBAKey key = mapKey(event->key());
162	if (key == GBA_KEY_NONE) {
163		QWidget::keyPressEvent(event);
164		return;
165	}
166	m_controller->keyReleased(key);
167	event->accept();
168}
169
170void Window::resizeEvent(QResizeEvent*) {
171	redoLogo();
172}
173
174void Window::closeEvent(QCloseEvent* event) {
175	emit shutdown();
176	QMainWindow::closeEvent(event);
177}
178
179void Window::toggleFullScreen() {
180	if (isFullScreen()) {
181		showNormal();
182		setCursor(Qt::ArrowCursor);
183	} else {
184		showFullScreen();
185		setCursor(Qt::BlankCursor);
186	}
187}
188
189void Window::gameStarted(GBAThread* context) {
190	emit startDrawing(m_controller->drawContext(), context);
191	foreach (QAction* action, m_gameActions) {
192		action->setDisabled(false);
193	}
194	char title[13] = { '\0' };
195	GBAGetGameTitle(context->gba, title);
196	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
197	attachWidget(m_display);
198	m_screenWidget->setScaledContents(true);
199}
200
201void Window::gameStopped() {
202	foreach (QAction* action, m_gameActions) {
203		action->setDisabled(true);
204	}
205	setWindowTitle(tr(PROJECT_NAME));
206	detachWidget(m_display);
207	m_screenWidget->setScaledContents(false);
208	redoLogo();
209}
210
211void Window::redoLogo() {
212	if (m_controller->isLoaded()) {
213		return;
214	}
215	m_screenWidget->setPixmap(m_logo.scaled(m_screenWidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
216}
217
218void Window::openStateWindow(LoadSave ls) {
219	if (m_stateWindow) {
220		return;
221	}
222	bool wasPaused = m_controller->isPaused();
223	m_stateWindow = new LoadSaveState(m_controller);
224	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(hide()));
225	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
226		m_screenWidget->layout()->removeWidget(m_stateWindow);
227		m_stateWindow = nullptr;
228		setFocus();
229	});
230	if (!wasPaused) {
231		m_controller->setPaused(true);
232		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
233	}
234	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
235	m_stateWindow->setMode(ls);
236	attachWidget(m_stateWindow);
237}
238
239void Window::setupMenu(QMenuBar* menubar) {
240	menubar->clear();
241	QMenu* fileMenu = menubar->addMenu(tr("&File"));
242	fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
243#ifndef Q_OS_MAC
244	fileMenu->addSeparator();
245	fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
246#endif
247
248	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
249	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
250	reset->setShortcut(tr("Ctrl+R"));
251	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
252	m_gameActions.append(reset);
253	emulationMenu->addAction(reset);
254
255	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
256	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
257	m_gameActions.append(shutdown);
258	emulationMenu->addAction(shutdown);
259	emulationMenu->addSeparator();
260
261	QAction* loadState = new QAction(tr("&Load state"), emulationMenu);
262	loadState->setShortcut(tr("Ctrl+L"));
263	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
264	m_gameActions.append(loadState);
265	emulationMenu->addAction(loadState);
266
267	QAction* saveState = new QAction(tr("&Save state"), emulationMenu);
268	saveState->setShortcut(tr("Ctrl+S"));
269	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
270	m_gameActions.append(saveState);
271	emulationMenu->addAction(saveState);
272
273	QMenu* quickLoadMenu = emulationMenu->addMenu(tr("Quick load"));
274	QMenu* quickSaveMenu = emulationMenu->addMenu(tr("Quick save"));
275	int i;
276	for (i = 1; i < 10; ++i) {
277		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
278		quickLoad->setShortcut(tr("F%1").arg(i));
279		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
280		m_gameActions.append(quickLoad);
281		quickLoadMenu->addAction(quickLoad);
282
283		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
284		quickSave->setShortcut(tr("Shift+F%1").arg(i));
285		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
286		m_gameActions.append(quickSave);
287		quickSaveMenu->addAction(quickSave);
288	}
289
290	emulationMenu->addSeparator();
291
292	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
293	pause->setChecked(false);
294	pause->setCheckable(true);
295	pause->setShortcut(tr("Ctrl+P"));
296	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
297	connect(m_controller, &GameController::gamePaused, [this, pause]() {
298		pause->setChecked(true);
299
300		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
301		QPixmap pixmap;
302		pixmap.convertFromImage(currentImage.rgbSwapped());
303		m_screenWidget->setPixmap(pixmap);
304	});
305	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
306	m_gameActions.append(pause);
307	emulationMenu->addAction(pause);
308
309	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
310	frameAdvance->setShortcut(tr("Ctrl+N"));
311	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
312	m_gameActions.append(frameAdvance);
313	emulationMenu->addAction(frameAdvance);
314
315	QMenu* target = emulationMenu->addMenu("FPS target");
316	QAction* setTarget = new QAction(tr("15"), emulationMenu);
317	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
318	target->addAction(setTarget);
319	setTarget = new QAction(tr("30"), emulationMenu);
320	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
321	target->addAction(setTarget);
322	setTarget = new QAction(tr("45"), emulationMenu);
323	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
324	target->addAction(setTarget);
325	setTarget = new QAction(tr("60"), emulationMenu);
326	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
327	target->addAction(setTarget);
328	setTarget = new QAction(tr("90"), emulationMenu);
329	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
330	target->addAction(setTarget);
331	setTarget = new QAction(tr("120"), emulationMenu);
332	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
333	target->addAction(setTarget);
334	setTarget = new QAction(tr("240"), emulationMenu);
335	connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
336	target->addAction(setTarget);
337
338	QMenu* videoMenu = menubar->addMenu(tr("&Video"));
339	QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
340	QAction* setSize = new QAction(tr("1x"), videoMenu);
341	connect(setSize, &QAction::triggered, [this]() {
342		showNormal();
343		resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
344	});
345	frameMenu->addAction(setSize);
346	setSize = new QAction(tr("2x"), videoMenu);
347	connect(setSize, &QAction::triggered, [this]() {
348		showNormal();
349		resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
350	});
351	frameMenu->addAction(setSize);
352	setSize = new QAction(tr("3x"), videoMenu);
353	connect(setSize, &QAction::triggered, [this]() {
354		showNormal();
355		resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
356	});
357	frameMenu->addAction(setSize);
358	setSize = new QAction(tr("4x"), videoMenu);
359	connect(setSize, &QAction::triggered, [this]() {
360		showNormal();
361		resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
362	});
363	frameMenu->addAction(setSize);
364	frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
365
366	QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
367	QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
368	QAction* setBuffer = new QAction(tr("512"), buffersMenu);
369	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
370	buffersMenu->addAction(setBuffer);
371	setBuffer = new QAction(tr("1024"), buffersMenu);
372	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
373	buffersMenu->addAction(setBuffer);
374	setBuffer = new QAction(tr("2048"), buffersMenu);
375	connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
376	buffersMenu->addAction(setBuffer);
377
378	QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
379	QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
380	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
381	debuggingMenu->addAction(viewLogs);
382#ifdef USE_GDB_STUB
383	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
384	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
385	debuggingMenu->addAction(gdbWindow);
386#endif
387
388	foreach (QAction* action, m_gameActions) {
389		action->setDisabled(true);
390	}
391}
392
393void Window::attachWidget(QWidget* widget) {
394	m_screenWidget->layout()->addWidget(widget);
395	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
396}
397
398void Window::detachWidget(QWidget* widget) {
399	m_screenWidget->layout()->removeWidget(widget);
400}