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 m_backing = std::move(QImage());
28 m_oldBacking = std::move(QImage());
29 m_isDrawing = true;
30 m_context = controller;
31 emit drawingStarted();
32}
33
34void DisplayQt::stopDrawing() {
35 m_isDrawing = false;
36 m_context.reset();
37}
38
39void DisplayQt::lockAspectRatio(bool lock) {
40 Display::lockAspectRatio(lock);
41 update();
42}
43
44void DisplayQt::lockIntegerScaling(bool lock) {
45 Display::lockIntegerScaling(lock);
46 update();
47}
48
49void DisplayQt::interframeBlending(bool lock) {
50 Display::interframeBlending(lock);
51 update();
52}
53
54void DisplayQt::filter(bool filter) {
55 Display::filter(filter);
56 update();
57}
58
59void DisplayQt::framePosted() {
60 update();
61 const color_t* buffer = m_context->drawContext();
62 if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
63 return;
64 }
65 m_oldBacking = m_backing;
66#ifdef COLOR_16_BIT
67#ifdef COLOR_5_6_5
68 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB16);
69#else
70 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_RGB555);
71#endif
72#else
73 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), m_width, m_height, QImage::Format_ARGB32);
74 m_backing = m_backing.convertToFormat(QImage::Format_RGB32);
75#endif
76#ifndef COLOR_5_6_5
77 m_backing = m_backing.rgbSwapped();
78#endif
79}
80
81void DisplayQt::resizeContext() {
82 if (!m_context) {
83 return;
84 }
85 QSize size = m_context->screenDimensions();
86 if (m_width != size.width() || m_height != size.height()) {
87 m_width = size.width();
88 m_height = size.height();
89 m_oldBacking = std::move(QImage());
90 m_backing = std::move(QImage());
91 }
92}
93
94void DisplayQt::paintEvent(QPaintEvent*) {
95 QPainter painter(this);
96 painter.fillRect(QRect(QPoint(), size()), Qt::black);
97 if (isFiltered()) {
98 painter.setRenderHint(QPainter::SmoothPixmapTransform);
99 }
100 // TODO: Refactor this code out (since it's copied in like 3 places)
101 QSize s = size();
102 QSize ds = s;
103 if (isAspectRatioLocked()) {
104 if (s.width() * m_height > s.height() * m_width) {
105 ds.setWidth(s.height() * m_width / m_height);
106 } else if (s.width() * m_height < s.height() * m_width) {
107 ds.setHeight(s.width() * m_height / m_width);
108 }
109 }
110 if (isIntegerScalingLocked()) {
111 if (ds.width() >= m_width) {
112 ds.setWidth(ds.width() - ds.width() % m_width);
113 }
114 if (ds.height() >= m_height) {
115 ds.setHeight(ds.height() - ds.height() % m_height);
116 }
117 }
118 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
119 QRect full(origin, ds);
120
121 if (hasInterframeBlending()) {
122 painter.drawImage(full, m_oldBacking, QRect(0, 0, m_width, m_height));
123 painter.setOpacity(0.5);
124 }
125 painter.drawImage(full, m_backing, QRect(0, 0, m_width, m_height));
126 painter.setOpacity(1);
127 if (isShowOSD()) {
128 messagePainter()->paint(&painter);
129 }
130}