all repos — mgba @ 697c1cfa9d67413138349a28380f65be8c6b464d

mGBA Game Boy Advance Emulator

src/platform/qt/MapView.cpp (view raw)

  1/* Copyright (c) 2013-2017 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 "MapView.h"
  7
  8#include "CoreController.h"
  9#include "GBAApp.h"
 10
 11#include <QButtonGroup>
 12#include <QFontDatabase>
 13#include <QRadioButton>
 14#include <QTimer>
 15
 16using namespace QGBA;
 17
 18MapView::MapView(std::shared_ptr<CoreController> controller, QWidget* parent)
 19	: AssetView(controller, parent)
 20	, m_controller(controller)
 21{
 22	m_ui.setupUi(this);
 23	m_ui.tile->setController(controller);
 24
 25	switch (m_controller->platform()) {
 26#ifdef M_CORE_GBA
 27	case PLATFORM_GBA:
 28		m_ui.tile->setBoundary(2048, 0, 2);
 29		break;
 30#endif
 31#ifdef M_CORE_GB
 32	case PLATFORM_GB:
 33		m_ui.tile->setBoundary(1024, 0, 0);
 34		break;
 35#endif
 36	default:
 37		return;
 38	}
 39
 40	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
 41		updateTiles(true);
 42	});
 43
 44	CoreController::Interrupter interrupter(m_controller);
 45	const mCoreChannelInfo* videoLayers;
 46	size_t nVideo = m_controller->thread()->core->listVideoLayers(m_controller->thread()->core, &videoLayers);
 47	QButtonGroup* group = new QButtonGroup(this);
 48	for (size_t i = 0; i < nVideo; ++i) {
 49		if (strncmp(videoLayers[i].internalName, "bg", 2) != 0) {
 50			continue;
 51		}
 52		QRadioButton* button = new QRadioButton(tr(videoLayers[i].visibleName));
 53		if (!i) {
 54			button->setChecked(true);
 55		}
 56		m_ui.bgLayout->addWidget(button);
 57		connect(button, &QAbstractButton::pressed, button, [this, i]() {
 58			selectMap(i);
 59		});
 60		group->addButton(button);
 61	}
 62}
 63
 64void MapView::selectMap(int map) {
 65	if (map >= mMapCacheSetSize(&m_cacheSet->maps)) {
 66		return;
 67	}
 68	if (map == m_map) {
 69		return;
 70	}
 71	m_map = map;
 72	updateTiles(true);
 73}
 74
 75void MapView::updateTilesGBA(bool force) {
 76	QImage bg;
 77	{
 78		CoreController::Interrupter interrupter(m_controller);
 79		mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map);
 80		int tilesW = 1 << mMapCacheSystemInfoGetTilesWide(mapCache->sysConfig);
 81		int tilesH = 1 << mMapCacheSystemInfoGetTilesHigh(mapCache->sysConfig);
 82		bg = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32);
 83		uchar* bgBits = bg.bits();
 84		for (int j = 0; j < tilesH; ++j) {
 85			for (int i = 0; i < tilesW; ++i) {
 86				mMapCacheCleanTile(mapCache, &m_mapStatus[i + j * tilesW], i, j);
 87			}
 88			for (int i = 0; i < 8; ++i) {
 89				memcpy(static_cast<void*>(&bgBits[tilesW * 32 * (i + j * 8)]), mMapCacheGetRow(mapCache, i + j * 8), tilesW * 32);
 90			}
 91		}
 92	}
 93	bg = bg.convertToFormat(QImage::Format_RGB32).rgbSwapped();
 94	QPixmap map = QPixmap::fromImage(bg);
 95	if (m_ui.magnification->value() > 1) {
 96		map = map.scaled(map.size() * m_ui.magnification->value());
 97	}
 98	m_ui.map->setPixmap(map);
 99}
100
101#ifdef M_CORE_GB
102void MapView::updateTilesGB(bool force) {
103	updateTilesGBA(force);
104}
105#endif
106