all repos — mgba @ b73f6314ca08eca05c5d64e8b6184f93a2cad261

mGBA Game Boy Advance Emulator

src/platform/qt/LoadSaveState.cpp (view raw)

 1#include "LoadSaveState.h"
 2
 3#include "GameController.h"
 4#include "VFileDevice.h"
 5
 6extern "C" {
 7#include "gba-serialize.h"
 8#include "gba-video.h"
 9}
10
11using namespace QGBA;
12
13LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent)
14	: QWidget(parent)
15	, m_controller(controller)
16{
17	m_ui.setupUi(this);
18
19	QImage currentImage(reinterpret_cast<const uchar*>(controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
20	m_currentImage.convertFromImage(currentImage.rgbSwapped());
21
22	m_slots[0] = m_ui.state1;
23	m_slots[1] = m_ui.state2;
24	m_slots[2] = m_ui.state3;
25	m_slots[3] = m_ui.state4;
26	m_slots[4] = m_ui.state5;
27	m_slots[5] = m_ui.state6;
28	m_slots[6] = m_ui.state7;
29	m_slots[7] = m_ui.state8;
30	m_slots[8] = m_ui.state9;
31
32	int i;
33	for (i = 0; i < NUM_SLOTS; ++i) {
34		loadState(i);
35		connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i); });
36	}
37}
38
39void LoadSaveState::setMode(LoadSave mode) {
40	m_mode = mode;
41	QString text = mode == LoadSave::LOAD ? tr("Load State") : tr("SaveState");
42	setWindowTitle(text);
43	m_ui.lsLabel->setText(text);
44}
45
46void LoadSaveState::loadState(int slot) {
47	GBAThread* thread = m_controller->thread();
48	VFile* vf = GBAGetState(thread->gba, thread->stateDir, slot, false);
49	if (!vf) {
50		return;
51	}
52	VFileDevice vdev(vf);
53	QImage stateImage;
54	stateImage.load(&vdev, "PNG");
55	if (!stateImage.isNull()) {
56		QPixmap statePixmap;
57		statePixmap.convertFromImage(stateImage);
58		m_slots[slot]->setIcon(statePixmap);
59		m_slots[slot]->setText(QString());
60	} else {
61		m_slots[slot]->setText(tr("Slot %1").arg(slot + 1));
62	}
63	m_slots[slot]->setShortcut(QString::number(slot + 1));
64}
65
66void LoadSaveState::triggerState(int slot) {
67	GBAThread* thread = m_controller->thread();
68	GBAThreadInterrupt(thread);
69	if (m_mode == LoadSave::SAVE) {
70		GBASaveState(thread->gba, thread->stateDir, slot, true);
71	} else {
72		GBALoadState(thread->gba, thread->stateDir, slot);
73	}
74	GBAThreadContinue(thread);
75	close();
76}