all repos — mgba @ 21e7d763206959ac325f1d5d6e55bb61433c700e

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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 "DisplayQt.h"
  7
  8#include "CoreController.h"
  9
 10#include <QPainter>
 11
 12#include <mgba/core/core.h>
 13#include <mgba/core/thread.h>
 14#include <mgba-util/math.h>
 15
 16using namespace QGBA;
 17
 18DisplayQt::DisplayQt(QWidget* parent)
 19	: Display(parent)
 20{
 21}
 22
 23void DisplayQt::startDrawing(std::shared_ptr<CoreController> controller) {
 24	QSize size = controller->screenDimensions();
 25	m_width = size.width();
 26	m_height = size.height();
 27	setSystemDimensions(m_width, m_height);
 28	m_backing = std::move(QImage());
 29	m_isDrawing = true;
 30	m_context = controller;
 31}
 32
 33void DisplayQt::stopDrawing() {
 34	m_isDrawing = false;
 35	m_context.reset();
 36}
 37
 38void DisplayQt::lockAspectRatio(bool lock) {
 39	Display::lockAspectRatio(lock);
 40	update();
 41}
 42
 43void DisplayQt::lockIntegerScaling(bool lock) {
 44	Display::lockIntegerScaling(lock);
 45	update();
 46}
 47
 48void DisplayQt::filter(bool filter) {
 49	Display::filter(filter);
 50	update();
 51}
 52
 53void DisplayQt::framePosted() {
 54	update();
 55	const color_t* buffer = m_context->drawContext();
 56	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
 57		return;
 58	}
 59#ifdef COLOR_16_BIT
 60#ifdef COLOR_5_6_5
 61	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
 62#else
 63	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
 64#endif
 65#else
 66	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
 67	m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
 68#endif
 69}
 70
 71void DisplayQt::resizeContext() {
 72	if (!m_context) {
 73		return;
 74	}
 75	QSize size = m_context->screenDimensions();
 76	if (m_width != size.width() || m_height != size.height()) {
 77		m_width = size.width();
 78		m_height = size.height();
 79		m_backing = std::move(QImage());
 80	}
 81}
 82
 83void DisplayQt::paintEvent(QPaintEvent*) {
 84	QPainter painter(this);
 85	painter.fillRect(QRect(QPoint(), size()), Qt::black);
 86	if (isFiltered()) {
 87		painter.setRenderHint(QPainter::SmoothPixmapTransform);
 88	}
 89	QSize s = size();
 90	QSize ds = viewportSize();
 91	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
 92	QRect full(origin, ds);
 93
 94#ifdef COLOR_5_6_5
 95	painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
 96#else
 97	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, m_width, m_height));
 98#endif
 99	messagePainter()->paint(&painter);
100}