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