all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

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#include <QTimer>
 12
 13#ifdef M_CORE_GB
 14#include <mgba/internal/gb/gb.h>
 15#endif
 16
 17using namespace QGBA;
 18
 19TileView::TileView(GameController* controller, QWidget* parent)
 20	: AssetView(controller, parent)
 21	, m_controller(controller)
 22	, m_tileStatus{}
 23	, m_paletteId(0)
 24{
 25	m_ui.setupUi(this);
 26	m_ui.tile->setController(controller);
 27
 28	connect(m_ui.tiles, &TilePainter::indexPressed, m_ui.tile, &AssetTile::selectIndex);
 29	connect(m_ui.paletteId, &QAbstractSlider::valueChanged, this, &TileView::updatePalette);
 30
 31	int max = 1024;
 32	int boundary = 1024;
 33	switch (m_controller->platform()) {
 34#ifdef M_CORE_GBA
 35	case PLATFORM_GBA:
 36		max = 3072;
 37		boundary = 2048;
 38		break;
 39#endif
 40#ifdef M_CORE_GB
 41	case PLATFORM_GB:
 42		max = 1024;
 43		boundary = 512;
 44		m_ui.palette256->setEnabled(false);
 45		break;
 46#endif
 47	default:
 48		return;
 49	}
 50	m_ui.tile->setPaletteSet(0, boundary, max);
 51
 52	connect(m_ui.palette256, &QAbstractButton::toggled, [this](bool selected) {
 53		if (selected) {
 54			m_ui.paletteId->setValue(0);
 55		}
 56		int max = 1024;
 57		int boundary = 1024;
 58		switch (m_controller->platform()) {
 59#ifdef M_CORE_GBA
 60		case PLATFORM_GBA:
 61			max = 3072 >> selected;
 62			boundary = 2048 >> selected;
 63			break;
 64#endif
 65#ifdef M_CORE_GB
 66		case PLATFORM_GB:
 67			return;
 68#endif
 69		default:
 70			break;
 71		}
 72		m_ui.tile->setPaletteSet(selected, boundary, max);
 73		updateTiles(true);
 74	});
 75	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
 76		updateTiles(true);
 77	});
 78}
 79
 80#ifdef M_CORE_GBA
 81void TileView::updateTilesGBA(bool force) {
 82	if (m_ui.palette256->isChecked()) {
 83		m_ui.tiles->setTileCount(1536);
 84		mTileCacheSetPalette(m_tileCache.get(), 1);
 85		for (int i = 0; i < 1024; ++i) {
 86			const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, 0);
 87			if (data) {
 88				m_ui.tiles->setTile(i, data);
 89			} else if (force) {
 90				m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, 0));
 91			}
 92		}
 93		for (int i = 1024; i < 1536; ++i) {
 94			const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, 1);
 95			if (data) {
 96				m_ui.tiles->setTile(i, data);
 97			} else if (force) {
 98				m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, 1));
 99			}
100		}
101	} else {
102		m_ui.tiles->setTileCount(3072);
103		mTileCacheSetPalette(m_tileCache.get(), 0);
104		for (int i = 0; i < 2048; ++i) {
105			const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, m_paletteId);
106			if (data) {
107				m_ui.tiles->setTile(i, data);
108			} else if (force) {
109				m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId));
110			}
111		}
112		for (int i = 2048; i < 3072; ++i) {
113			const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, m_paletteId + 16);
114			if (data) {
115				m_ui.tiles->setTile(i, data);
116			} else if (force) {
117				m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId + 16));
118			}
119		}
120	}
121}
122#endif
123
124#ifdef M_CORE_GB
125void TileView::updateTilesGB(bool force) {
126	const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
127	int count = gb->model >= GB_MODEL_CGB ? 1024 : 512;
128	m_ui.tiles->setTileCount(count);
129	mTileCacheSetPalette(m_tileCache.get(), 0);
130	for (int i = 0; i < count; ++i) {
131		const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[16 * i], i, m_paletteId);
132		if (data) {
133			m_ui.tiles->setTile(i, data);
134		} else if (force) {
135			m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId));
136		}
137	}
138}
139#endif
140
141void TileView::updatePalette(int palette) {
142	m_paletteId = palette;
143	m_ui.tile->setPalette(palette);
144	updateTiles(true);
145}