all repos — mgba @ f6f3cb5d3d8b91dd603772ea0eebb2513562a0cf

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