all repos — mgba @ 55679df8fc49fca3d5f8584d16b90e9bb734a922

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