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