all repos — mgba @ fa884d071ecaa3e05ff20b45a67bf9500dd3d6b6

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