all repos — mgba @ 7d980287cb40cc25fef7c48f0f77a22ae0100088

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_lockAspectRatio(false)
15	, m_filter(false)
16{
17	connect(&m_drawTimer, SIGNAL(timeout()), this, SLOT(update()));
18	m_drawTimer.setInterval(12); // Give update time roughly 4.6ms of clearance
19}
20
21void DisplayQt::startDrawing(const uint32_t* buffer, GBAThread* context) {
22	m_context = context;
23#ifdef COLOR_16_BIT
24#ifdef COLOR_5_6_5
25	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB16);
26#else
27	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB555);
28#endif
29#else
30	m_backing = QImage(reinterpret_cast<const uchar*>(buffer), 256, 256, QImage::Format_RGB32);
31#endif
32	m_drawTimer.start();
33}
34
35void DisplayQt::stopDrawing() {
36	m_drawTimer.stop();
37}
38
39void DisplayQt::pauseDrawing() {
40	m_drawTimer.stop();
41}
42
43void DisplayQt::unpauseDrawing() {
44	m_drawTimer.start();
45}
46
47void DisplayQt::forceDraw() {
48	update();
49}
50
51void DisplayQt::lockAspectRatio(bool lock) {
52	m_lockAspectRatio = lock;
53	update();
54}
55
56void DisplayQt::filter(bool filter) {
57	m_filter = filter;
58	update();
59}
60
61void DisplayQt::paintEvent(QPaintEvent*) {
62	QPainter painter(this);
63	painter.fillRect(QRect(QPoint(), size()), Qt::black);
64	if (m_filter) {
65		painter.setRenderHint(QPainter::SmoothPixmapTransform);
66	}
67	QSize s = size();
68	QSize ds = s;
69	if (s.width() * 2 > s.height() * 3) {
70		ds.setWidth(s.height() * 3 / 2);
71	} else if (s.width() * 2 < s.height() * 3) {
72		ds.setHeight(s.width() * 2 / 3);
73	}
74	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
75	QRect full(origin, ds);
76
77#ifdef COLOR_5_6_5
78	painter.drawImage(full, m_backing, QRect(0, 0, 240, 160));
79#else
80	painter.drawImage(full, m_backing.rgbSwapped(), QRect(0, 0, 240, 160));
81#endif
82}