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