all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

mGBA Game Boy Advance Emulator

src/platform/qt/AssetTile.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 "AssetTile.h"
  7
  8#include "GBAApp.h"
  9
 10#include <QFontDatabase>
 11
 12#include <mgba/core/interface.h>
 13#ifdef M_CORE_GBA
 14#include <mgba/internal/gba/memory.h>
 15#endif
 16#ifdef M_CORE_GB
 17#include <mgba/internal/gb/memory.h>
 18#endif
 19
 20using namespace QGBA;
 21
 22AssetTile::AssetTile(QWidget* parent)
 23	: QGroupBox(parent)
 24	, m_tileCache(nullptr)
 25	, m_paletteId(0)
 26	, m_paletteSet(0)
 27	, m_index(0)
 28{
 29	m_ui.setupUi(this);
 30
 31	m_ui.preview->setDimensions(QSize(8, 8));
 32	m_ui.color->setDimensions(QSize(1, 1));
 33	m_ui.color->setSize(50);
 34
 35	connect(m_ui.preview, &Swatch::indexPressed, this, &AssetTile::selectColor);
 36
 37	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 38
 39	m_ui.tileId->setFont(font);
 40	m_ui.address->setFont(font);
 41	m_ui.r->setFont(font);
 42	m_ui.g->setFont(font);
 43	m_ui.b->setFont(font);
 44}
 45
 46void AssetTile::setController(GameController* controller) {
 47	m_tileCache = controller->tileCache();
 48	switch (controller->platform()) {
 49#ifdef M_CORE_GBA
 50	case PLATFORM_GBA:
 51		m_addressWidth = 8;
 52		m_addressBase = BASE_VRAM;
 53		m_boundaryBase = BASE_VRAM | 0x10000;
 54		break;
 55#endif
 56#ifdef M_CORE_GB
 57	case PLATFORM_GB:
 58		m_addressWidth = 4;
 59		m_addressBase = GB_BASE_VRAM;
 60		m_boundaryBase = GB_BASE_VRAM;
 61		break;
 62#endif
 63	default:
 64		m_addressWidth = 0;
 65		m_addressBase = 0;
 66		m_boundaryBase = 0;
 67		break;
 68	}
 69}
 70
 71void AssetTile::setPalette(int palette) {
 72	m_paletteId = palette;
 73	selectIndex(m_index);
 74}
 75
 76void AssetTile::setPaletteSet(int palette, int boundary, int max) {
 77	m_index = m_index * (1 + m_paletteSet) / (1 + palette);
 78	if (m_index >= max) {
 79		m_index = max - 1;
 80	}
 81	m_boundary = boundary;
 82	m_paletteSet = palette;
 83	selectIndex(m_index);
 84}
 85
 86void AssetTile::selectIndex(int index) {
 87	m_index = index;
 88	const uint16_t* data;
 89
 90	mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
 91	unsigned bpp = 8 << m_tileCache->bpp;
 92	int dispIndex = index;
 93	int paletteId = m_paletteId;
 94	int base = m_addressBase;
 95	if (index >= m_boundary) {
 96		base = m_boundaryBase;
 97		// XXX: Do this better
 98#ifdef M_CORE_GBA
 99		if (m_boundaryBase == (BASE_VRAM | 0x10000)) {
100			paletteId += m_tileCache->count / 2;
101		}
102#endif
103		dispIndex -= m_boundary;
104	}
105	data = mTileCacheGetTile(m_tileCache.get(), index, paletteId);
106	m_ui.tileId->setText(QString::number(dispIndex * (1 + m_paletteSet)));
107	m_ui.address->setText(tr("%0%1%2")
108		.arg(m_addressWidth == 4 ? index >= m_boundary : 0)
109		.arg(m_addressWidth == 4 ? ":" : "x")
110		.arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
111	for (int i = 0; i < 64; ++i) {
112		m_ui.preview->setColor(i, data[i]);
113	}
114	m_ui.preview->update();
115}
116
117void AssetTile::selectColor(int index) {
118	const uint16_t* data;
119	mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
120	unsigned bpp = 8 << m_tileCache->bpp;
121	int paletteId = m_paletteId;
122	// XXX: Do this better
123#ifdef M_CORE_GBA
124	if (m_index >= m_boundary && m_boundaryBase == (BASE_VRAM | 0x10000)) {
125		paletteId += m_tileCache->count / 2;
126	}
127#endif
128	data = mTileCacheGetTile(m_tileCache.get(), m_index, m_paletteId);
129	uint16_t color = data[index];
130	m_ui.color->setColor(0, color);
131	m_ui.color->update();
132
133	uint32_t r = M_R5(color);
134	uint32_t g = M_G5(color);
135	uint32_t b = M_B5(color);
136	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
137	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
138	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
139}
140