all repos — mgba @ 255242a66515aaef88e5b6c07cc866cdd32214c7

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*) {
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::paintEvent(QPaintEvent*) {
50	QPainter painter(this);
51	painter.fillRect(QRect(QPoint(), size()), Qt::black);
52	if (m_filter) {
53		painter.setRenderHint(QPainter::SmoothPixmapTransform);
54	}
55	QSize s = size();
56	QSize ds = s;
57	if (s.width() * 2 > s.height() * 3) {
58		ds.setWidth(s.height() * 3 / 2);
59	} else if (s.width() * 2 < s.height() * 3) {
60		ds.setHeight(s.width() * 2 / 3);
61	}
62	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
63	QRect full(origin, ds);
64
65#ifdef COLOR_5_6_5
66	painter.drawImage(full, m_backing, QRect(0, 0, 240, 160));
67#else
68	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, 240, 160));
69#endif
70}