all repos — mgba @ daaa2fa5236df5e8976e7e0ba7ac39e0d9233422

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