all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

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 "gba/gba.h"
 14#ifdef M_CORE_GB
 15#include "gb/gb.h"
 16#include "gb/io.h"
 17#endif
 18
 19using namespace QGBA;
 20
 21ObjView::ObjView(GameController* controller, QWidget* parent)
 22	: AssetView(controller, parent)
 23	, m_controller(controller)
 24	, m_tileStatus{}
 25	, m_objId(0)
 26	, m_objInfo{}
 27{
 28	m_ui.setupUi(this);
 29	m_ui.tile->setController(controller);
 30
 31	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 32
 33	m_ui.x->setFont(font);
 34	m_ui.y->setFont(font);
 35	m_ui.w->setFont(font);
 36	m_ui.h->setFont(font);
 37	m_ui.address->setFont(font);
 38	m_ui.priority->setFont(font);
 39	m_ui.palette->setFont(font);
 40	m_ui.transform->setFont(font);
 41	m_ui.mode->setFont(font);
 42
 43	connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(translateIndex(int)));
 44	connect(m_ui.objId, SIGNAL(valueChanged(int)), this, SLOT(selectObj(int)));
 45	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
 46		updateTiles(true);
 47	});
 48}
 49
 50void ObjView::selectObj(int obj) {
 51	m_objId = obj;
 52	updateTiles(true);
 53}
 54
 55void ObjView::translateIndex(int index) {
 56	unsigned x = index % m_objInfo.width;
 57	unsigned y = index / m_objInfo.width;
 58	m_ui.tile->selectIndex(x + y * m_objInfo.stride + m_tileOffset);
 59}
 60
 61#ifdef M_CORE_GBA
 62void ObjView::updateTilesGBA(bool force) {
 63	const GBA* gba = static_cast<const GBA*>(m_controller->thread()->core->board);
 64	const GBAObj* obj = &gba->video.oam.obj[m_objId];
 65
 66	unsigned shape = GBAObjAttributesAGetShape(obj->a);
 67	unsigned size = GBAObjAttributesBGetSize(obj->b);
 68	unsigned width = GBAVideoObjSizes[shape * 4 + size][0];
 69	unsigned height = GBAVideoObjSizes[shape * 4 + size][1];
 70	unsigned tile = GBAObjAttributesCGetTile(obj->c);
 71	ObjInfo newInfo{
 72		tile,
 73		width / 8,
 74		height / 8,
 75		width / 8
 76	};
 77	m_ui.tiles->setTileCount(width * height / 64);
 78	m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
 79	unsigned palette = GBAObjAttributesCGetPalette(obj->c);
 80	GBARegisterDISPCNT dispcnt = gba->memory.io[0]; // FIXME: Register name can't be imported due to namespacing issues
 81	if (!GBARegisterDISPCNTIsObjCharacterMapping(dispcnt)) {
 82		newInfo.stride = 0x20 >> (GBAObjAttributesAGet256Color(obj->a));
 83	};
 84	if (newInfo != m_objInfo) {
 85		force = true;
 86	}
 87	m_objInfo = newInfo;
 88	int i = 0;
 89	if (GBAObjAttributesAIs256Color(obj->a)) {
 90		m_ui.palette->setText("256-color");
 91		mTileCacheSetPalette(m_tileCache.get(), 1);
 92		m_ui.tile->setPalette(0);
 93		m_ui.tile->setPaletteSet(1, 1024, 1536);
 94		tile /= 2;
 95		unsigned t = tile + i;
 96		for (int y = 0; y < height / 8; ++y) {
 97			for (int x = 0; x < width / 8; ++x, ++i, ++t) {
 98				const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * t], t + 1024, 1);
 99				if (data) {
100					m_ui.tiles->setTile(i, data);
101				} else if (force) {
102					m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), t + 1024, 1));
103				}
104			}
105			t += newInfo.stride - width / 8;
106		}
107		tile += 1024;
108	} else {
109		m_ui.palette->setText(QString::number(palette));
110		mTileCacheSetPalette(m_tileCache.get(), 0);
111		m_ui.tile->setPalette(palette);
112		m_ui.tile->setPaletteSet(0, 2048, 3072);
113		unsigned t = tile + i;
114		for (int y = 0; y < height / 8; ++y) {
115			for (int x = 0; x < width / 8; ++x, ++i, ++t) {
116				const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * t], t + 2048, palette + 16);
117				if (data) {
118					m_ui.tiles->setTile(i, data);
119				} else if (force) {
120					m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), t + 2048, palette + 16));
121				}
122			}
123			t += newInfo.stride - width / 8;
124		}
125		tile += 2048;
126	}
127	m_tileOffset = tile;
128
129	m_ui.x->setText(QString::number(GBAObjAttributesBGetX(obj->b)));
130	m_ui.y->setText(QString::number(GBAObjAttributesAGetY(obj->a)));
131	m_ui.w->setText(QString::number(width));
132	m_ui.h->setText(QString::number(height));
133
134	m_ui.address->setText(tr("0x%0").arg(BASE_OAM + m_objId * sizeof(*obj), 8, 16, QChar('0')));
135	m_ui.priority->setText(QString::number(GBAObjAttributesCGetPriority(obj->c)));
136	m_ui.flippedH->setChecked(GBAObjAttributesBIsHFlip(obj->b));
137	m_ui.flippedV->setChecked(GBAObjAttributesBIsVFlip(obj->b));
138	m_ui.enabled->setChecked(!GBAObjAttributesAIsDisable(obj->a) || GBAObjAttributesAIsTransformed(obj->a));
139	m_ui.doubleSize->setChecked(GBAObjAttributesAIsDoubleSize(obj->a) && GBAObjAttributesAIsTransformed(obj->a));
140	m_ui.mosaic->setChecked(GBAObjAttributesAIsMosaic(obj->a));
141
142	if (GBAObjAttributesAIsTransformed(obj->a)) {
143		m_ui.transform->setText(QString::number(GBAObjAttributesBGetMatIndex(obj->b)));
144	} else {
145		m_ui.transform->setText(tr("Off"));
146	}
147
148	switch (GBAObjAttributesAGetMode(obj->a)) {
149	case OBJ_MODE_NORMAL:
150		m_ui.mode->setText(tr("Normal"));
151		break;
152	case OBJ_MODE_SEMITRANSPARENT:
153		m_ui.mode->setText(tr("Trans"));
154		break;
155	case OBJ_MODE_OBJWIN:
156		m_ui.mode->setText(tr("OBJWIN"));
157		break;
158	default:
159		m_ui.mode->setText(tr("Invalid"));
160		break;
161	}
162}
163#endif
164
165#ifdef M_CORE_GB
166void ObjView::updateTilesGB(bool force) {
167	const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
168	const GBObj* obj = &gb->video.oam.obj[m_objId];
169
170	unsigned width = 8;
171	unsigned height = 8;
172	GBRegisterLCDC lcdc = gb->memory.io[REG_LCDC];
173	if (GBRegisterLCDCIsObjSize(lcdc)) {
174		height = 16;
175	}
176	unsigned tile = obj->tile;
177	ObjInfo newInfo{
178		tile,
179		1,
180		height / 8,
181		1
182	};
183	if (newInfo != m_objInfo) {
184		force = true;
185	}
186	m_objInfo = newInfo;
187	m_ui.tiles->setTileCount(width * height / 64);
188	m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
189	int palette = 0;
190	if (gb->model >= GB_MODEL_CGB) {
191		if (GBObjAttributesIsBank(obj->attr)) {
192			tile += 512;
193		}
194		palette = GBObjAttributesGetCGBPalette(obj->attr);
195	} else {
196		palette = GBObjAttributesGetPalette(obj->attr);
197	}
198	int i = 0;
199	m_ui.palette->setText(QString::number(palette));
200	mTileCacheSetPalette(m_tileCache.get(), 0);
201	m_ui.tile->setPalette(palette + 8);
202	m_ui.tile->setPaletteSet(0, 512, 1024);
203	for (int y = 0; y < height / 8; ++y, ++i) {
204		unsigned t = tile + i;
205		const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[16 * t], t, palette + 8);
206		if (data) {
207			m_ui.tiles->setTile(i, data);
208		} else if (force) {
209			m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), t, palette + 8));
210		}
211	}
212	m_tileOffset = tile;
213
214	m_ui.x->setText(QString::number(obj->x));
215	m_ui.y->setText(QString::number(obj->y));
216	m_ui.w->setText(QString::number(width));
217	m_ui.h->setText(QString::number(height));
218
219	m_ui.address->setText(tr("0x%0").arg(GB_BASE_OAM + m_objId * sizeof(*obj), 4, 16, QChar('0')));
220	m_ui.priority->setText(QString::number(GBObjAttributesGetPriority(obj->attr)));
221	m_ui.flippedH->setChecked(GBObjAttributesIsXFlip(obj->attr));
222	m_ui.flippedV->setChecked(GBObjAttributesIsYFlip(obj->attr));
223	m_ui.enabled->setChecked(obj->y != 0 && obj->y < 160);
224	m_ui.doubleSize->setChecked(false);
225	m_ui.mosaic->setChecked(false);
226	m_ui.transform->setText(tr("N/A"));
227	m_ui.mode->setText(tr("N/A"));
228}
229#endif
230
231
232bool ObjView::ObjInfo::operator!=(const ObjInfo& other) {
233	return other.tile != tile ||
234		other.width != width ||
235		other.height != height ||
236		other.stride != stride;
237}