all repos — mgba @ 67c3f386a4106d5d3017aea882f64dc0ae94a375

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	m_backing = std::move(QImage());
 28	m_isDrawing = true;
 29	m_context = controller;
 30}
 31
 32void DisplayQt::stopDrawing() {
 33	m_isDrawing = false;
 34	m_context.reset();
 35}
 36
 37void DisplayQt::lockAspectRatio(bool lock) {
 38	Display::lockAspectRatio(lock);
 39	update();
 40}
 41
 42void DisplayQt::lockIntegerScaling(bool lock) {
 43	Display::lockIntegerScaling(lock);
 44	update();
 45}
 46
 47void DisplayQt::filter(bool filter) {
 48	Display::filter(filter);
 49	update();
 50}
 51
 52void DisplayQt::framePosted() {
 53	update();
 54	const color_t* buffer = m_context->drawContext();
 55	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
 56		return;
 57	}
 58#ifdef COLOR_16_BIT
 59#ifdef COLOR_5_6_5
 60	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
 61#else
 62	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
 63#endif
 64#else
 65	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
 66	m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
 67#endif
 68}
 69
 70void DisplayQt::resizeContext() {
 71	if (!m_context) {
 72		return;
 73	}
 74	QSize size = m_context->screenDimensions();
 75	if (m_width != size.width() || m_height != size.height()) {
 76		m_width = size.width();
 77		m_height = size.height();
 78		m_backing = std::move(QImage());
 79	}
 80}
 81
 82void DisplayQt::paintEvent(QPaintEvent*) {
 83	QPainter painter(this);
 84	painter.fillRect(QRect(QPoint(), size()), Qt::black);
 85	if (isFiltered()) {
 86		painter.setRenderHint(QPainter::SmoothPixmapTransform);
 87	}
 88	QSize s = size();
 89	QSize ds = s;
 90	if (isAspectRatioLocked()) {
 91		if (s.width() * m_height > s.height() * m_width) {
 92			ds.setWidth(s.height() * m_width / m_height);
 93		} else if (s.width() * m_height < s.height() * m_width) {
 94			ds.setHeight(s.width() * m_height / m_width);
 95		}
 96	}
 97	if (isIntegerScalingLocked()) {
 98		ds.setWidth(ds.width() - ds.width() % m_width);
 99		ds.setHeight(ds.height() - ds.height() % m_height);
100	}
101	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
102	QRect full(origin, ds);
103
104#ifdef COLOR_5_6_5
105	painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
106#else
107	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, m_width, m_height));
108#endif
109	messagePainter()->paint(&painter);
110}