all repos — mgba @ f6f3cb5d3d8b91dd603772ea0eebb2513562a0cf

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