all repos — mgba @ 1a0e44c014ad34bea30d237d27015d82d02cda4b

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	setSystemDimensions(m_width, m_height);
28	m_backing = 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::filter(bool filter) {
49	Display::filter(filter);
50	update();
51}
52
53void DisplayQt::framePosted() {
54	update();
55	color_t* buffer = m_context->drawContext();
56	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
57		return;
58	}
59#ifdef COLOR_16_BIT
60#ifdef COLOR_5_6_5
61	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
62#else
63	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
64#endif
65#else
66	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
67	m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
68#endif
69}
70
71void DisplayQt::paintEvent(QPaintEvent*) {
72	QPainter painter(this);
73	painter.fillRect(QRect(QPoint(), size()), Qt::black);
74	if (isFiltered()) {
75		painter.setRenderHint(QPainter::SmoothPixmapTransform);
76	}
77	QSize s = size();
78	QSize ds = viewportSize();
79	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
80	QRect full(origin, ds);
81
82#ifdef COLOR_5_6_5
83	painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
84#else
85	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, m_width, m_height));
86#endif
87	messagePainter()->paint(&painter);
88}