all repos — mgba @ 055ba3df630c46d6c07355ef8bcfeb59fb36b5aa

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 <QPainter>
 9
10#include <mgba/core/core.h>
11#include <mgba/core/thread.h>
12
13using namespace QGBA;
14
15DisplayQt::DisplayQt(QWidget* parent)
16	: Display(parent)
17{
18}
19
20void DisplayQt::startDrawing(mCoreThread* context) {
21	context->core->desiredVideoDimensions(context->core, &m_width, &m_height);
22	m_backing = std::move(QImage());
23	m_isDrawing = true;
24}
25
26void DisplayQt::lockAspectRatio(bool lock) {
27	Display::lockAspectRatio(lock);
28	update();
29}
30
31void DisplayQt::lockIntegerScaling(bool lock) {
32	Display::lockIntegerScaling(lock);
33	update();
34}
35
36void DisplayQt::filter(bool filter) {
37	Display::filter(filter);
38	update();
39}
40
41void DisplayQt::framePosted(const uint32_t* buffer) {
42	update();
43	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
44		return;
45	}
46#ifdef COLOR_16_BIT
47#ifdef COLOR_5_6_5
48	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
49#else
50	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
51#endif
52#else
53	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
54	m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
55#endif
56}
57
58void DisplayQt::paintEvent(QPaintEvent*) {
59	QPainter painter(this);
60	painter.fillRect(QRect(QPoint(), size()), Qt::black);
61	if (isFiltered()) {
62		painter.setRenderHint(QPainter::SmoothPixmapTransform);
63	}
64	QSize s = size();
65	QSize ds = s;
66	if (isAspectRatioLocked()) {
67		if (s.width() * m_height > s.height() * m_width) {
68			ds.setWidth(s.height() * m_width / m_height);
69		} else if (s.width() * m_height < s.height() * m_width) {
70			ds.setHeight(s.width() * m_height / m_width);
71		}
72	}
73	if (isIntegerScalingLocked()) {
74		ds.setWidth(ds.width() - ds.width() % m_width);
75		ds.setHeight(ds.height() - ds.height() % m_height);
76	}
77	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
78	QRect full(origin, ds);
79
80#ifdef COLOR_5_6_5
81	painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
82#else
83	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, m_width, m_height));
84#endif
85	messagePainter()->paint(&painter);
86}