all repos — mgba @ b1f750e441af63b35141702ac00fd2f13313bd44

mGBA Game Boy Advance Emulator

src/platform/qt/TileView.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 "TileView.h"
  7
  8#include "GBAApp.h"
  9
 10#include <QFontDatabase>
 11
 12extern "C" {
 13#include "gba/gba.h"
 14}
 15
 16using namespace QGBA;
 17
 18TileView::TileView(GameController* controller, QWidget* parent)
 19	: QWidget(parent)
 20	, m_controller(controller)
 21	, m_paletteId(0)
 22{
 23	m_ui.setupUi(this);
 24	GBAVideoTileCacheInit(&m_tileCache);
 25
 26	m_ui.preview->setDimensions(QSize(8, 8));
 27	updateTiles();
 28
 29	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 30
 31	m_ui.tileId->setFont(font);
 32	m_ui.address->setFont(font);
 33
 34	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updateTiles()));
 35	connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
 36	connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
 37	connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int)));
 38	connect(m_ui.palette256, SIGNAL(toggled(bool)), this, SLOT(updateTiles()));
 39}
 40
 41TileView::~TileView() {
 42	GBAVideoTileCacheDeinit(&m_tileCache);
 43}
 44
 45void TileView::selectIndex(int index) {
 46	const uint16_t* data;
 47	m_ui.tileId->setText(QString::number(index));
 48	if (m_ui.palette256->isChecked()) {
 49		m_ui.address->setText(tr("0x%0").arg(index * 64 | BASE_VRAM, 8, 16, QChar('0')));
 50		if (index < 1024) {
 51			data = GBAVideoTileCacheGetTile256(&m_tileCache, index, 0);
 52		} else {
 53			data = GBAVideoTileCacheGetTile256(&m_tileCache, index, 1);
 54		}
 55	} else {
 56		m_ui.address->setText(tr("0x%0").arg(index * 32 | BASE_VRAM, 8, 16, QChar('0')));
 57		if (index < 2048) {
 58			data = GBAVideoTileCacheGetTile16(&m_tileCache, index, m_paletteId);
 59		} else {
 60			data = GBAVideoTileCacheGetTile16(&m_tileCache, index, m_paletteId + 16);
 61		}
 62	}
 63	for (int i = 0; i < 64; ++i) {
 64		m_ui.preview->setColor(i, data[i]);
 65	}
 66	m_ui.preview->update();
 67}
 68
 69void TileView::updateTiles() {
 70	if (!m_controller->thread() || !m_controller->thread()->core) {
 71		return;
 72	}
 73
 74	GBA* gba = static_cast<GBA*>(m_controller->thread()->core->board);
 75	GBAVideoTileCacheAssociate(&m_tileCache, &gba->video);
 76
 77	if (m_ui.palette256->isChecked()) {
 78		m_ui.tiles->setMinimumSize(256, 384);
 79		for (int i = 0; i < 1024; ++i) {
 80			const uint16_t* data = GBAVideoTileCacheGetTile256IfDirty(&m_tileCache, i, 0);
 81			if (data) {
 82				m_ui.tiles->setTile(i, data);
 83			}
 84		}
 85		for (int i = 1024; i < 1536; ++i) {
 86			const uint16_t* data = GBAVideoTileCacheGetTile256IfDirty(&m_tileCache, i, 1);
 87			if (data) {
 88				m_ui.tiles->setTile(i, data);
 89			}
 90		}
 91	} else {
 92		m_ui.tiles->setMinimumSize(256, 768);
 93		for (int i = 0; i < 2048; ++i) {
 94			const uint16_t* data = GBAVideoTileCacheGetTile16IfDirty(&m_tileCache, i, m_paletteId);
 95			if (data) {
 96				m_ui.tiles->setTile(i, data);
 97			}
 98		}
 99		for (int i = 2048; i < 3072; ++i) {
100			const uint16_t* data = GBAVideoTileCacheGetTile16IfDirty(&m_tileCache, i, m_paletteId + 16);
101			if (data) {
102				m_ui.tiles->setTile(i, data);
103			}
104		}
105	}
106}
107
108void TileView::updatePalette(int palette) {
109	m_paletteId = palette;
110	updateTiles();
111}