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 #include "LogController.h"
11 
12 using namespace QGBA;
13 
14 #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
15 Display::Driver Display::s_driver = Display::Driver::OPENGL;
16 #else
17 Display::Driver Display::s_driver = Display::Driver::QT;
18 #endif
19 
create(QWidget * parent)20 Display* Display::create(QWidget* parent) {
21 #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
22 	QSurfaceFormat format;
23 	format.setSwapInterval(1);
24 	format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
25 #endif
26 
27 	switch (s_driver) {
28 #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
29 	case Driver::OPENGL:
30 #if defined(BUILD_GLES2) || defined(USE_EPOXY)
31 		if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES) {
32 			format.setVersion(2, 0);
33 		} else {
34 			format.setVersion(3, 2);
35 		}
36 		format.setProfile(QSurfaceFormat::CoreProfile);
37 		if (!DisplayGL::supportsFormat(format)) {
38 #ifdef BUILD_GL
39 			LOG(QT, WARN) << ("Failed to create an OpenGL Core context, trying old-style...");
40 			format.setVersion(1, 4);
41 			format.setOption(QSurfaceFormat::DeprecatedFunctions);
42 			if (!DisplayGL::supportsFormat(format)) {
43 				return nullptr;
44 			}
45 #else
46 			return nullptr;
47 #endif
48 		}
49 		return new DisplayGL(format, parent);
50 #endif
51 #endif
52 #ifdef BUILD_GL
53 	case Driver::OPENGL1:
54 		format.setVersion(1, 4);
55 		if (!DisplayGL::supportsFormat(format)) {
56 			return nullptr;
57 		}
58 		return new DisplayGL(format, parent);
59 #endif
60 
61 	case Driver::QT:
62 		return new DisplayQt(parent);
63 
64 	default:
65 #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
66 		return new DisplayGL(format, parent);
67 #else
68 		return new DisplayQt(parent);
69 #endif
70 	}
71 }
72 
Display(QWidget * parent)73 Display::Display(QWidget* parent)
74 	: QWidget(parent)
75 {
76 	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
77 	connect(&m_mouseTimer, &QTimer::timeout, this, &Display::hideCursor);
78 	m_mouseTimer.setSingleShot(true);
79 	m_mouseTimer.setInterval(MOUSE_DISAPPEAR_TIMER);
80 	setMouseTracking(true);
81 }
82 
resizeEvent(QResizeEvent *)83 void Display::resizeEvent(QResizeEvent*) {
84 	m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
85 }
86 
lockAspectRatio(bool lock)87 void Display::lockAspectRatio(bool lock) {
88 	m_lockAspectRatio = lock;
89 	m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
90 }
91 
lockIntegerScaling(bool lock)92 void Display::lockIntegerScaling(bool lock) {
93 	m_lockIntegerScaling = lock;
94 }
95 
interframeBlending(bool lock)96 void Display::interframeBlending(bool lock) {
97 	m_interframeBlending = lock;
98 }
99 
showOSDMessages(bool enable)100 void Display::showOSDMessages(bool enable) {
101 	m_showOSD = enable;
102 }
103 
filter(bool filter)104 void Display::filter(bool filter) {
105 	m_filter = filter;
106 }
107 
showMessage(const QString & message)108 void Display::showMessage(const QString& message) {
109 	m_messagePainter.showMessage(message);
110 	if (!isDrawing()) {
111 		forceDraw();
112 	}
113 }
114 
mouseMoveEvent(QMouseEvent *)115 void Display::mouseMoveEvent(QMouseEvent*) {
116 	emit showCursor();
117 	m_mouseTimer.stop();
118 	m_mouseTimer.start();
119 }
120