all repos — mgba @ b9acc05292b424376e8474f7b0e9672e4d3aaa93

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_oldBacking = 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::interframeBlending(bool lock) {
 49	Display::interframeBlending(lock);
 50	update();
 51}
 52
 53void DisplayQt::filter(bool filter) {
 54	Display::filter(filter);
 55	update();
 56}
 57
 58void DisplayQt::framePosted() {
 59	update();
 60	const color_t* buffer = m_context->drawContext();
 61	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
 62		return;
 63	}
 64	m_oldBacking = m_backing;
 65#ifdef COLOR_16_BIT
 66#ifdef COLOR_5_6_5
 67	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
 68#else
 69	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
 70#endif
 71#else
 72	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
 73	m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
 74#endif
 75#ifndef COLOR_5_6_5
 76	m_backing = m_backing.rgbSwapped();
 77#endif
 78}
 79
 80void DisplayQt::resizeContext() {
 81	if (!m_context) {
 82		return;
 83	}
 84	QSize size = m_context->screenDimensions();
 85	if (m_width != size.width() || m_height != size.height()) {
 86		m_width = size.width();
 87		m_height = size.height();
 88		m_oldBacking = std::move(QImage());
 89		m_backing = std::move(QImage());
 90	}
 91}
 92
 93void DisplayQt::paintEvent(QPaintEvent*) {
 94	QPainter painter(this);
 95	painter.fillRect(QRect(QPoint(), size()), Qt::black);
 96	if (isFiltered()) {
 97		painter.setRenderHint(QPainter::SmoothPixmapTransform);
 98	}
 99	QSize s = size();
100	QSize ds = s;
101	if (isAspectRatioLocked()) {
102		if (s.width() * m_height > s.height() * m_width) {
103			ds.setWidth(s.height() * m_width / m_height);
104		} else if (s.width() * m_height < s.height() * m_width) {
105			ds.setHeight(s.width() * m_height / m_width);
106		}
107	}
108	if (isIntegerScalingLocked()) {
109		if (ds.width() >= m_width) {
110			ds.setWidth(ds.width() - ds.width() % m_width);
111		}
112		if (ds.height() >= m_height) {
113			ds.setHeight(ds.height() - ds.height() % m_height);
114		}
115	}
116	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
117	QRect full(origin, ds);
118
119	if (hasInterframeBlending()) {
120		painter.drawImage(full, m_oldBacking, QRect(0, 0, m_width, m_height));
121		painter.setOpacity(0.5);
122	}
123	painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
124	painter.setOpacity(1);
125	if (isShowOSD()) {
126		messagePainter()->paint(&painter);
127	}
128}