all repos — mgba @ fc905657adeba406a2c280bbb94b09533a89e3f8

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
10extern "C" {
11#include "gba/video.h"
12}
13
14using namespace QGBA;
15
16DisplayQt::DisplayQt(QWidget* parent)
17	: Display(parent)
18	, m_isDrawing(false)
19	, m_backing(nullptr)
20{
21}
22
23void DisplayQt::startDrawing(GBAThread*) {
24	m_isDrawing = true;
25}
26
27void DisplayQt::lockAspectRatio(bool lock) {
28	Display::lockAspectRatio(lock);
29	update();
30}
31
32void DisplayQt::filter(bool filter) {
33	Display::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), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, QImage::Format_RGB16);
45#else
46	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, QImage::Format_RGB555);
47#endif
48#else
49	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, QImage::Format_RGB32);
50#endif
51}
52
53void DisplayQt::paintEvent(QPaintEvent*) {
54	QPainter painter(this);
55	painter.fillRect(QRect(QPoint(), size()), Qt::black);
56	if (isFiltered()) {
57		painter.setRenderHint(QPainter::SmoothPixmapTransform);
58	}
59	QSize s = size();
60	QSize ds = s;
61	if (isAspectRatioLocked()) {
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, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS));
73#else
74	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS));
75#endif
76	messagePainter()->paint(&painter);
77}