all repos — mgba @ aacab52a840f8c086a8fb22160c52f25fbe49032

mGBA Game Boy Advance Emulator

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* context) {
21	m_context = context;
22}
23
24void DisplayQt::lockAspectRatio(bool lock) {
25	m_lockAspectRatio = lock;
26	update();
27}
28
29void DisplayQt::filter(bool filter) {
30	m_filter = filter;
31	update();
32}
33
34void DisplayQt::framePosted(const uint32_t* buffer) {
35	update();
36	if (const_cast<const QImage&>(m_backing).bits() == reinterpret_cast<const uchar*>(buffer)) {
37		return;
38	}
39#ifdef COLOR_16_BIT
40#ifdef COLOR_5_6_5
41	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB16);
42#else
43	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB555);
44#endif
45#else
46	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB32);
47#endif
48}
49
50void DisplayQt::paintEvent(QPaintEvent*) {
51	QPainter painter(this);
52	painter.fillRect(QRect(QPoint(), size()), Qt::black);
53	if (m_filter) {
54		painter.setRenderHint(QPainter::SmoothPixmapTransform);
55	}
56	QSize s = size();
57	QSize ds = s;
58	if (s.width() * 2 > s.height() * 3) {
59		ds.setWidth(s.height() * 3 / 2);
60	} else if (s.width() * 2 < s.height() * 3) {
61		ds.setHeight(s.width() * 2 / 3);
62	}
63	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
64	QRect full(origin, ds);
65
66#ifdef COLOR_5_6_5
67	painter.drawImage(full, m_backing, QRect(0, 0, 240, 160));
68#else
69	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, 240, 160));
70#endif
71}