src/platform/qt/AssetView.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 "AssetView.h"
7
8#include "CoreController.h"
9
10#include <QTimer>
11
12using namespace QGBA;
13
14AssetView::AssetView(std::shared_ptr<CoreController> controller, QWidget* parent)
15 : QWidget(parent)
16 , m_cacheSet(controller->graphicCaches())
17 , m_controller(controller)
18{
19 m_updateTimer.setSingleShot(true);
20 m_updateTimer.setInterval(1);
21 connect(&m_updateTimer, &QTimer::timeout, this, static_cast<void(AssetView::*)()>(&AssetView::updateTiles));
22
23 connect(controller.get(), &CoreController::frameAvailable, &m_updateTimer,
24 static_cast<void(QTimer::*)()>(&QTimer::start));
25 connect(controller.get(), &CoreController::stopping, this, &AssetView::close);
26 connect(controller.get(), &CoreController::stopping, &m_updateTimer, &QTimer::stop);
27}
28
29void AssetView::updateTiles() {
30 updateTiles(false);
31}
32
33void AssetView::updateTiles(bool force) {
34 switch (m_controller->platform()) {
35#ifdef M_CORE_GBA
36 case PLATFORM_GBA:
37 updateTilesGBA(force);
38 break;
39#endif
40#ifdef M_CORE_GB
41 case PLATFORM_GB:
42 updateTilesGB(force);
43 break;
44#endif
45 default:
46 return;
47 }
48}
49
50void AssetView::resizeEvent(QResizeEvent*) {
51 updateTiles(true);
52}
53
54void AssetView::showEvent(QShowEvent*) {
55 updateTiles(true);
56}
57
58void AssetView::compositeTile(const void* tBuffer, void* buffer, size_t stride, size_t x, size_t y, int depth) {
59 const uint8_t* tile = static_cast<const uint8_t*>(tBuffer);
60 uint8_t* pixels = static_cast<uint8_t*>(buffer);
61 size_t base = stride * y + x;
62 switch (depth) {
63 case 2:
64 for (size_t i = 0; i < 8; ++i) {
65 uint8_t tileDataLower = tile[i * 2];
66 uint8_t tileDataUpper = tile[i * 2 + 1];
67 uint8_t pixel;
68 pixel = ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
69 pixels[base + i * stride] = pixel;
70 pixel = ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
71 pixels[base + i * stride + 1] = pixel;
72 pixel = ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
73 pixels[base + i * stride + 2] = pixel;
74 pixel = ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
75 pixels[base + i * stride + 3] = pixel;
76 pixel = ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
77 pixels[base + i * stride + 4] = pixel;
78 pixel = ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
79 pixels[base + i * stride + 5] = pixel;
80 pixel = (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
81 pixels[base + i * stride + 6] = pixel;
82 pixel = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
83 pixels[base + i * stride + 7] = pixel;
84 }
85 break;
86 case 4:
87 for (size_t j = 0; j < 8; ++j) {
88 for (size_t i = 0; i < 4; ++i) {
89 pixels[base + j * stride + i * 2] = tile[j * 4 + i] & 0xF;
90 pixels[base + j * stride + i * 2 + 1] = tile[j * 4 + i] >> 4;
91 }
92 }
93 break;
94 case 8:
95 for (size_t i = 0; i < 8; ++i) {
96 memcpy(&pixels[base + i * stride], &tile[i * 8], 8);
97 }
98 break;
99 }
100}