/
WindowQt.cpp
182 lines (140 loc) · 2.76 KB
/
WindowQt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "WindowQt.h"
#pragma warning(push)
#pragma warning(disable: 4127 4800 4244)
#include <QApplication>
#include <QDebug>
#include <QResizeEvent>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#pragma warning(pop)
QSurfaceFormat defaultFormat()
{
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CoreProfile);
#ifndef NDEBUG
format.setOption(QSurfaceFormat::DebugContext);
#endif
return format;
}
WindowQt::WindowQt()
: WindowQt(defaultFormat())
{
}
WindowQt::WindowQt(const QSurfaceFormat & format)
: m_context(new QOpenGLContext)
, m_updatePending(false)
, m_initialized(false)
{
QSurfaceFormat f(format);
f.setRenderableType(QSurfaceFormat::OpenGL);
setSurfaceType(OpenGLSurface);
create();
m_context->setFormat(format);
if (!m_context->create())
{
qDebug() << "Could not create OpenGL context.";
QApplication::quit();
}
}
WindowQt::~WindowQt()
{
if (m_initialized)
{
makeCurrent();
deinitializeGL();
doneCurrent();
}
}
QOpenGLContext * WindowQt::context()
{
return m_context.data();
}
void WindowQt::makeCurrent()
{
m_context->makeCurrent(this);
}
void WindowQt::doneCurrent()
{
m_context->doneCurrent();
}
void WindowQt::resizeEvent(QResizeEvent * event)
{
resize(event);
paint();
}
void WindowQt::exposeEvent(QExposeEvent * )
{
paint();
}
void WindowQt::initialize()
{
makeCurrent();
initializeGL();
doneCurrent();
m_initialized = true;
}
void WindowQt::resize(QResizeEvent * event)
{
if (!m_initialized)
initialize();
makeCurrent();
QResizeEvent deviceSpecificResizeEvent(event->size() * devicePixelRatio(), event->oldSize() * devicePixelRatio());
resizeGL(&deviceSpecificResizeEvent);
doneCurrent();
}
void WindowQt::paint()
{
if (!m_initialized)
initialize();
if (!isExposed())
return;
m_updatePending = false;
makeCurrent();
paintGL();
m_context->swapBuffers(this);
doneCurrent();
}
void WindowQt::updateGL()
{
if (!m_updatePending)
{
m_updatePending = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
}
bool WindowQt::event(QEvent * event)
{
switch (event->type())
{
case QEvent::UpdateRequest:
paint();
return true;
case QEvent::Enter:
enterEvent(event);
return true;
case QEvent::Leave:
leaveEvent(event);
return true;
default:
return QWindow::event(event);
}
}
void WindowQt::initializeGL()
{
}
void WindowQt::deinitializeGL()
{
}
void WindowQt::resizeGL(QResizeEvent *)
{
}
void WindowQt::paintGL()
{
updateGL();
}
void WindowQt::enterEvent(QEvent *)
{
}
void WindowQt::leaveEvent(QEvent *)
{
}