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#include "gba/gba.h"
15}
16
17using namespace QGBA;
18
19TileView::TileView(GameController* controller, QWidget* parent)
20 : QWidget(parent)
21 , m_controller(controller)
22 , m_paletteId(0)
23{
24 m_ui.setupUi(this);
25 GBAVideoTileCacheInit(&m_tileCache);
26
27 m_ui.preview->setDimensions(QSize(8, 8));
28 m_updateTimer.setSingleShot(true);
29 m_updateTimer.setInterval(10);
30 connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTiles()));
31
32 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
33
34 m_ui.tileId->setFont(font);
35 m_ui.address->setFont(font);
36
37 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), &m_updateTimer, SLOT(start()));
38 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
39 connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
40 connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int)));
41 connect(m_ui.palette256, SIGNAL(toggled(bool)), this, SLOT(updateTiles()));
42}
43
44TileView::~TileView() {
45 if (m_controller->isLoaded() && m_controller->thread() && m_controller->thread()->core) {
46 GBA* gba = static_cast<GBA*>(m_controller->thread()->core->board);
47 gba->video.renderer->cache = nullptr;
48 }
49
50 GBAVideoTileCacheDeinit(&m_tileCache);
51}
52
53void TileView::selectIndex(int index) {
54 const uint16_t* data;
55 m_ui.tileId->setText(QString::number(index));
56 if (m_ui.palette256->isChecked()) {
57 m_ui.address->setText(tr("0x%0").arg(index * 64 | BASE_VRAM, 8, 16, QChar('0')));
58 if (index < 1024) {
59 data = GBAVideoTileCacheGetTile256(&m_tileCache, index, 0);
60 } else {
61 data = GBAVideoTileCacheGetTile256(&m_tileCache, index, 1);
62 }
63 } else {
64 m_ui.address->setText(tr("0x%0").arg(index * 32 | BASE_VRAM, 8, 16, QChar('0')));
65 if (index < 2048) {
66 data = GBAVideoTileCacheGetTile16(&m_tileCache, index, m_paletteId);
67 } else {
68 data = GBAVideoTileCacheGetTile16(&m_tileCache, index, m_paletteId + 16);
69 }
70 }
71 for (int i = 0; i < 64; ++i) {
72 m_ui.preview->setColor(i, data[i]);
73 }
74 m_ui.preview->update();
75}
76
77void TileView::updateTiles(bool force) {
78 if (!m_controller->thread() || !m_controller->thread()->core) {
79 return;
80 }
81
82 GBA* gba = static_cast<GBA*>(m_controller->thread()->core->board);
83 GBAVideoTileCacheAssociate(&m_tileCache, &gba->video);
84
85 if (m_ui.palette256->isChecked()) {
86 m_ui.tiles->setTileCount(1536);
87 for (int i = 0; i < 1024; ++i) {
88 const uint16_t* data = GBAVideoTileCacheGetTile256IfDirty(&m_tileCache, i, 0);
89 if (data) {
90 m_ui.tiles->setTile(i, data);
91 } else if (force) {
92 m_ui.tiles->setTile(i, GBAVideoTileCacheGetTile256(&m_tileCache, i, 0));
93 }
94 }
95 for (int i = 1024; i < 1536; ++i) {
96 const uint16_t* data = GBAVideoTileCacheGetTile256IfDirty(&m_tileCache, i, 1);
97 if (data) {
98 m_ui.tiles->setTile(i, data);
99 } else if (force) {
100 m_ui.tiles->setTile(i, GBAVideoTileCacheGetTile256(&m_tileCache, i, 1));
101 }
102 }
103 } else {
104 m_ui.tiles->setTileCount(3072);
105 for (int i = 0; i < 2048; ++i) {
106 const uint16_t* data = GBAVideoTileCacheGetTile16IfDirty(&m_tileCache, i, m_paletteId);
107 if (data) {
108 m_ui.tiles->setTile(i, data);
109 } else if (force) {
110 m_ui.tiles->setTile(i, GBAVideoTileCacheGetTile16(&m_tileCache, i, m_paletteId));
111 }
112 }
113 for (int i = 2048; i < 3072; ++i) {
114 const uint16_t* data = GBAVideoTileCacheGetTile16IfDirty(&m_tileCache, i, m_paletteId + 16);
115 if (data) {
116 m_ui.tiles->setTile(i, data);
117 } else if (force) {
118 m_ui.tiles->setTile(i, GBAVideoTileCacheGetTile16(&m_tileCache, i, m_paletteId + 16));
119 }
120 }
121 }
122}
123
124void TileView::updatePalette(int palette) {
125 m_paletteId = palette;
126 updateTiles();
127}
128
129void TileView::resizeEvent(QResizeEvent*) {
130 updateTiles(true);
131}
132
133void TileView::showEvent(QShowEvent*) {
134 m_updateTimer.start();
135}