all repos — mgba @ 7bc605fcc0e04ae6f8ce92e21c9f3654c1930635

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "ObjView.h"
  7
  8#include "GBAApp.h"
  9
 10#include <QFontDatabase>
 11#include <QTimer>
 12
 13#include "LogController.h"
 14#include "VFileDevice.h"
 15
 16#ifdef M_CORE_GBA
 17#include <mgba/internal/gba/gba.h>
 18#endif
 19#ifdef M_CORE_GB
 20#include <mgba/internal/gb/gb.h>
 21#include <mgba/internal/gb/io.h>
 22#endif
 23#include <mgba-util/png-io.h>
 24
 25using namespace QGBA;
 26
 27ObjView::ObjView(GameController* controller, QWidget* parent)
 28	: AssetView(controller, parent)
 29	, m_controller(controller)
 30{
 31	m_ui.setupUi(this);
 32	m_ui.tile->setController(controller);
 33
 34	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 35
 36	m_ui.x->setFont(font);
 37	m_ui.y->setFont(font);
 38	m_ui.w->setFont(font);
 39	m_ui.h->setFont(font);
 40	m_ui.address->setFont(font);
 41	m_ui.priority->setFont(font);
 42	m_ui.palette->setFont(font);
 43	m_ui.transform->setFont(font);
 44	m_ui.mode->setFont(font);
 45
 46	connect(m_ui.tiles, &TilePainter::indexPressed, this, &ObjView::translateIndex);
 47	connect(m_ui.objId, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ObjView::selectObj);
 48	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
 49		updateTiles(true);
 50	});
 51#ifdef USE_PNG
 52	connect(m_ui.exportButton, &QAbstractButton::clicked, this, &ObjView::exportObj);
 53#else
 54	m_ui.exportButton->setVisible(false);
 55#endif
 56}
 57
 58void ObjView::selectObj(int obj) {
 59	m_objId = obj;
 60	updateTiles(true);
 61}
 62
 63void ObjView::translateIndex(int index) {
 64	unsigned x = index % m_objInfo.width;
 65	unsigned y = index / m_objInfo.width;
 66	m_ui.tile->selectIndex(x + y * m_objInfo.stride + m_tileOffset);
 67}
 68
 69#ifdef M_CORE_GBA
 70void ObjView::updateTilesGBA(bool force) {
 71	m_ui.objId->setMaximum(127);
 72	const GBA* gba = static_cast<const GBA*>(m_controller->thread()->core->board);
 73	const GBAObj* obj = &gba->video.oam.obj[m_objId];
 74
 75	unsigned shape = GBAObjAttributesAGetShape(obj->a);
 76	unsigned size = GBAObjAttributesBGetSize(obj->b);
 77	unsigned width = GBAVideoObjSizes[shape * 4 + size][0];
 78	unsigned height = GBAVideoObjSizes[shape * 4 + size][1];
 79	unsigned tile = GBAObjAttributesCGetTile(obj->c);
 80	m_ui.tiles->setTileCount(width * height / 64);
 81	m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
 82	m_ui.tiles->resize(QSize(width, height) * m_ui.magnification->value());
 83	unsigned palette = GBAObjAttributesCGetPalette(obj->c);
 84	unsigned tileBase = tile;
 85	unsigned paletteSet;
 86	unsigned bits;
 87	if (GBAObjAttributesAIs256Color(obj->a)) {
 88		m_ui.palette->setText("256-color");
 89		paletteSet = 1;
 90		m_ui.tile->setPalette(0);
 91		m_ui.tile->setPaletteSet(1, 1024, 1536);
 92		palette = 1;
 93		tile = tile / 2 + 1024;
 94		bits = 8;
 95	} else {
 96		m_ui.palette->setText(QString::number(palette));
 97		paletteSet = 0;
 98		m_ui.tile->setPalette(palette);
 99		m_ui.tile->setPaletteSet(0, 2048, 3072);
100		palette += 16;
101		tile += 2048;
102		bits = 4;
103	}
104	ObjInfo newInfo{
105		tile,
106		width / 8,
107		height / 8,
108		width / 8,
109		palette,
110		paletteSet,
111		bits
112	};
113	if (newInfo != m_objInfo) {
114		force = true;
115	}
116	GBARegisterDISPCNT dispcnt = gba->memory.io[0]; // FIXME: Register name can't be imported due to namespacing issues
117	if (!GBARegisterDISPCNTIsObjCharacterMapping(dispcnt)) {
118		newInfo.stride = 0x20 >> (GBAObjAttributesAGet256Color(obj->a));
119	};
120	m_objInfo = newInfo;
121	m_tileOffset = tile;
122	mTileCacheSetPalette(m_tileCache.get(), paletteSet);
123
124	int i = 0;
125	for (int y = 0; y < height / 8; ++y) {
126		for (int x = 0; x < width / 8; ++x, ++i, ++tile, ++tileBase) {
127			const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * tileBase], tile, palette);
128			if (data) {
129				m_ui.tiles->setTile(i, data);
130			} else if (force) {
131				m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), tile, palette));
132			}
133		}
134		tile += newInfo.stride - width / 8;
135		tileBase += newInfo.stride - width / 8;
136	}
137
138	m_ui.x->setText(QString::number(GBAObjAttributesBGetX(obj->b)));
139	m_ui.y->setText(QString::number(GBAObjAttributesAGetY(obj->a)));
140	m_ui.w->setText(QString::number(width));
141	m_ui.h->setText(QString::number(height));
142
143	m_ui.address->setText(tr("0x%0").arg(BASE_OAM + m_objId * sizeof(*obj), 8, 16, QChar('0')));
144	m_ui.priority->setText(QString::number(GBAObjAttributesCGetPriority(obj->c)));
145	m_ui.flippedH->setChecked(GBAObjAttributesBIsHFlip(obj->b));
146	m_ui.flippedV->setChecked(GBAObjAttributesBIsVFlip(obj->b));
147	m_ui.enabled->setChecked(!GBAObjAttributesAIsDisable(obj->a) || GBAObjAttributesAIsTransformed(obj->a));
148	m_ui.doubleSize->setChecked(GBAObjAttributesAIsDoubleSize(obj->a) && GBAObjAttributesAIsTransformed(obj->a));
149	m_ui.mosaic->setChecked(GBAObjAttributesAIsMosaic(obj->a));
150
151	if (GBAObjAttributesAIsTransformed(obj->a)) {
152		m_ui.transform->setText(QString::number(GBAObjAttributesBGetMatIndex(obj->b)));
153	} else {
154		m_ui.transform->setText(tr("Off"));
155	}
156
157	switch (GBAObjAttributesAGetMode(obj->a)) {
158	case OBJ_MODE_NORMAL:
159		m_ui.mode->setText(tr("Normal"));
160		break;
161	case OBJ_MODE_SEMITRANSPARENT:
162		m_ui.mode->setText(tr("Trans"));
163		break;
164	case OBJ_MODE_OBJWIN:
165		m_ui.mode->setText(tr("OBJWIN"));
166		break;
167	default:
168		m_ui.mode->setText(tr("Invalid"));
169		break;
170	}
171}
172#endif
173
174#ifdef M_CORE_GB
175void ObjView::updateTilesGB(bool force) {
176	m_ui.objId->setMaximum(39);
177	const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
178	const GBObj* obj = &gb->video.oam.obj[m_objId];
179
180	unsigned width = 8;
181	unsigned height = 8;
182	GBRegisterLCDC lcdc = gb->memory.io[REG_LCDC];
183	if (GBRegisterLCDCIsObjSize(lcdc)) {
184		height = 16;
185	}
186	unsigned tile = obj->tile;
187	m_ui.tiles->setTileCount(width * height / 64);
188	m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
189	m_ui.tiles->resize(QSize(width, height) * m_ui.magnification->value());
190	unsigned palette = 0;
191	if (gb->model >= GB_MODEL_CGB) {
192		if (GBObjAttributesIsBank(obj->attr)) {
193			tile += 512;
194		}
195		palette = GBObjAttributesGetCGBPalette(obj->attr);
196	} else {
197		palette = GBObjAttributesGetPalette(obj->attr);
198	}
199	m_ui.palette->setText(QString::number(palette));
200	palette += 8;
201
202	ObjInfo newInfo{
203		tile,
204		1,
205		height / 8,
206		1,
207		palette,
208		0,
209		2
210	};
211	if (newInfo != m_objInfo) {
212		force = true;
213	}
214	m_objInfo = newInfo;
215	m_tileOffset = tile;
216
217	int i = 0;
218	mTileCacheSetPalette(m_tileCache.get(), 0);
219	m_ui.tile->setPalette(palette);
220	m_ui.tile->setPaletteSet(0, 512, 1024);
221	for (int y = 0; y < height / 8; ++y, ++i) {
222		unsigned t = tile + i;
223		const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[16 * t], t, palette);
224		if (data) {
225			m_ui.tiles->setTile(i, data);
226		} else if (force) {
227			m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), t, palette));
228		}
229	}
230
231	m_ui.x->setText(QString::number(obj->x));
232	m_ui.y->setText(QString::number(obj->y));
233	m_ui.w->setText(QString::number(width));
234	m_ui.h->setText(QString::number(height));
235
236	m_ui.address->setText(tr("0x%0").arg(GB_BASE_OAM + m_objId * sizeof(*obj), 4, 16, QChar('0')));
237	m_ui.priority->setText(QString::number(GBObjAttributesGetPriority(obj->attr)));
238	m_ui.flippedH->setChecked(GBObjAttributesIsXFlip(obj->attr));
239	m_ui.flippedV->setChecked(GBObjAttributesIsYFlip(obj->attr));
240	m_ui.enabled->setChecked(obj->y != 0 && obj->y < 160);
241	m_ui.doubleSize->setChecked(false);
242	m_ui.mosaic->setChecked(false);
243	m_ui.transform->setText(tr("N/A"));
244	m_ui.mode->setText(tr("N/A"));
245}
246#endif
247
248#ifdef USE_PNG
249void ObjView::exportObj() {
250	GameController::Interrupter interrupter(m_controller);
251	QString filename = GBAApp::app()->getSaveFileName(this, tr("Export sprite"),
252	                                                  tr("Portable Network Graphics (*.png)"));
253	VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
254	if (!vf) {
255		LOG(QT, ERROR) << tr("Failed to open output PNG file: %1").arg(filename);
256		return;
257	}
258
259	mTileCacheSetPalette(m_tileCache.get(), m_objInfo.paletteSet);
260	png_structp png = PNGWriteOpen(vf);
261	png_infop info = PNGWriteHeader8(png, m_objInfo.width * 8, m_objInfo.height * 8);
262
263	const uint16_t* rawPalette = mTileCacheGetPalette(m_tileCache.get(), m_objInfo.paletteId);
264	unsigned colors = 1 << m_objInfo.bits;
265	uint32_t palette[256];
266	for (unsigned c = 0; c < colors && c < 256; ++c) {
267		uint16_t color = rawPalette[c];
268		palette[c] = M_R8(rawPalette[c]);
269		palette[c] |= M_G8(rawPalette[c]) << 8;
270		palette[c] |= M_B8(rawPalette[c]) << 16;
271		if (c) {
272			palette[c] |= 0xFF000000;
273		}
274	}
275	PNGWritePalette(png, info, palette, colors);
276
277	uint8_t* buffer = new uint8_t[m_objInfo.width * m_objInfo.height * 8 * 8];
278	unsigned t = m_objInfo.tile;
279	for (int y = 0; y < m_objInfo.height; ++y) {
280		for (int x = 0; x < m_objInfo.width; ++x, ++t) {
281			compositeTile(t, static_cast<void*>(buffer), m_objInfo.width * 8, x * 8, y * 8, m_objInfo.bits);
282		}
283		t += m_objInfo.stride - m_objInfo.width;
284	}
285	PNGWritePixels8(png, m_objInfo.width * 8, m_objInfo.height * 8, m_objInfo.width * 8, static_cast<void*>(buffer));
286	PNGWriteClose(png, info);
287	delete[] buffer;
288}
289#endif
290
291bool ObjView::ObjInfo::operator!=(const ObjInfo& other) {
292	return other.tile != tile ||
293		other.width != width ||
294		other.height != height ||
295		other.stride != stride ||
296		other.paletteId != paletteId ||
297		other.paletteSet != paletteSet;
298}