all repos — mgba @ 7073fa6a24ee44da81c42cd6bed3f902e3610af2

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