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
10using namespace QGBA;
11
12DisplayQt::DisplayQt(QWidget* parent)
13 : Display(parent)
14 , m_backing(nullptr)
15 , m_lockAspectRatio(false)
16 , m_filter(false)
17{
18}
19
20void DisplayQt::startDrawing(GBAThread*) {
21}
22
23void DisplayQt::lockAspectRatio(bool lock) {
24 m_lockAspectRatio = lock;
25 update();
26}
27
28void DisplayQt::filter(bool filter) {
29 m_filter = filter;
30 update();
31}
32
33void DisplayQt::framePosted(const uint32_t* buffer) {
34 update();
35 if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
36 return;
37 }
38#ifdef COLOR_16_BIT
39#ifdef COLOR_5_6_5
40 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB16);
41#else
42 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB555);
43#endif
44#else
45 m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB32);
46#endif
47}
48
49void DisplayQt::showMessage(const QString& message) {
50 m_messagePainter.showMessage(message);
51}
52
53void DisplayQt::paintEvent(QPaintEvent*) {
54 QPainter painter(this);
55 painter.fillRect(QRect(QPoint(), size()), Qt::black);
56 if (m_filter) {
57 painter.setRenderHint(QPainter::SmoothPixmapTransform);
58 }
59 QSize s = size();
60 QSize ds = s;
61 if (m_lockAspectRatio) {
62 if (s.width() * 2 > s.height() * 3) {
63 ds.setWidth(s.height() * 3 / 2);
64 } else if (s.width() * 2 < s.height() * 3) {
65 ds.setHeight(s.width() * 2 / 3);
66 }
67 }
68 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
69 QRect full(origin, ds);
70
71#ifdef COLOR_5_6_5
72 painter.drawImage(full, m_backing, QRect(0, 0, 240, 160));
73#else
74 painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, 240, 160));
75#endif
76 m_messagePainter.paint(&painter);
77}
78
79void DisplayQt::resizeEvent(QResizeEvent*) {
80 m_messagePainter.resize(size(), m_lockAspectRatio);
81}