src/platform/qt/Display.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 "Display.h"
7
8#include "DisplayGL.h"
9#include "DisplayQt.h"
10
11using namespace QGBA;
12
13#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
14Display::Driver Display::s_driver = Display::Driver::OPENGL;
15#else
16Display::Driver Display::s_driver = Display::Driver::QT;
17#endif
18
19Display* Display::create(QWidget* parent) {
20#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
21 QSurfaceFormat format;
22 format.setSwapInterval(1);
23 format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
24#endif
25
26 switch (s_driver) {
27#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
28 case Driver::OPENGL:
29 format.setVersion(3, 0);
30 return new DisplayGL(format, parent);
31#endif
32#ifdef BUILD_GL
33 case Driver::OPENGL1:
34 format.setVersion(1, 4);
35 return new DisplayGL(format, parent);
36#endif
37
38 case Driver::QT:
39 return new DisplayQt(parent);
40
41 default:
42#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
43 return new DisplayGL(format, parent);
44#else
45 return new DisplayQt(parent);
46#endif
47 }
48}
49
50Display::Display(QWidget* parent)
51 : QWidget(parent)
52{
53 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
54 connect(&m_mouseTimer, &QTimer::timeout, this, &Display::hideCursor);
55 m_mouseTimer.setSingleShot(true);
56 m_mouseTimer.setInterval(MOUSE_DISAPPEAR_TIMER);
57 setMouseTracking(true);
58}
59
60QSize Display::viewportSize() {
61 QSize s = size();
62 QSize ds = s;
63 if (isAspectRatioLocked()) {
64 if (s.width() * m_coreHeight > s.height() * m_coreWidth) {
65 ds.setWidth(s.height() * m_coreWidth / m_coreHeight);
66 } else if (s.width() * m_coreHeight < s.height() * m_coreWidth) {
67 ds.setHeight(s.width() * m_coreHeight / m_coreWidth);
68 }
69 }
70 if (isIntegerScalingLocked()) {
71 ds.setWidth(ds.width() - ds.width() % m_coreWidth);
72 ds.setHeight(ds.height() - ds.height() % m_coreHeight);
73 }
74 return ds;
75}
76
77void Display::resizeEvent(QResizeEvent*) {
78 m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
79}
80
81void Display::lockAspectRatio(bool lock) {
82 m_lockAspectRatio = lock;
83 m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
84}
85
86void Display::lockIntegerScaling(bool lock) {
87 m_lockIntegerScaling = lock;
88}
89
90void Display::interframeBlending(bool lock) {
91 m_interframeBlending = lock;
92}
93
94void Display::filter(bool filter) {
95 m_filter = filter;
96}
97
98void Display::showMessage(const QString& message) {
99 m_messagePainter.showMessage(message);
100 if (!isDrawing()) {
101 forceDraw();
102 }
103}
104
105void Display::mouseMoveEvent(QMouseEvent* event) {
106 emit showCursor();
107 m_mouseTimer.stop();
108 m_mouseTimer.start();
109 event->ignore();
110}
111
112void Display::setSystemDimensions(int width, int height) {
113 m_coreWidth = width;
114 m_coreHeight = height;
115}