-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRenderWindow.cpp
executable file
·140 lines (116 loc) · 3.7 KB
/
RenderWindow.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
#include "RenderWindow.h"
#include <math.h>
#include <QTimer>
#include <QMouseEvent>
#include <QOpenGLShaderProgram>
#include <QCoreApplication>
#include <QApplication>
#include <QScreen>
bool RenderWindow::m_transparent = false;
RenderWindow::RenderWindow(QWidget *parent): OpenGLMouseAdapterWidget(parent),
initialized(false), engineInitialized(false), engine(nullptr)
{
m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
// --transparent causes the clear color to be transparent. Therefore, on systems that
// support it, the widget will become transparent apart from the logo.
/* if (m_transparent) {
QSurfaceFormat fmt = format();
fmt.setAlphaBufferSize(8);
setFormat(fmt);
}
*/
}
RenderWindow::~RenderWindow()
{
cleanup();
}
Core::WeakPointer<Core::Engine> RenderWindow::getEngine() {
return engine;
}
QSize RenderWindow::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize RenderWindow::sizeHint() const
{
return QSize(1200, 800);
}
void RenderWindow::cleanup()
{
}
void RenderWindow::start() {
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(mainLoop()));
timer->start(1);
}
void RenderWindow::mainLoop() {
this->update();
}
void RenderWindow::initializeGL()
{
// In this example the widget's corresponding top-level window can change
// several times during the widget's lifetime. Whenever this happens, the
// QOpenGLWidget's associated context is destroyed and a new one is created.
// Therefore we have to be prepared to clean up the resources on the
// aboutToBeDestroyed() signal, instead of the destructor. The emission of
// the signal will be followed by an invocation of initializeGL() where we
// can recreate all resources.
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &RenderWindow::cleanup);
this->initializeOpenGLFunctions();
// Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
// implementations this is optional and support may not be present
// at all. Nonetheless the below code works in all cases and makes
// sure there is a VAO when one is needed.
m_vao.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
this->init();
this->start();
}
void RenderWindow::paintGL()
{
QMutexLocker ml(&this->updateMutex);
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
static bool _inited = false;
if (!_inited) {
this->engine->setDefaultRenderTargetToCurrent();
_inited = true;
}
this->engineUpdate();
this->engineRender();
}
void RenderWindow::resizeGL(int w, int h) {
float dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
Core::UInt32 scaledW = static_cast<unsigned int>(static_cast<float>(w) * dpr);
Core::UInt32 scaledH = static_cast<unsigned int>(static_cast<float>(h) * dpr);
engine->setRenderSize(scaledW, scaledH, true);
}
void RenderWindow::init() {
if (!engineInitialized) {
engine = Core::Engine::instance();
engineInitialized = true;
}
resolveOnInits();
}
void RenderWindow::engineUpdate() {
engine->update();
}
void RenderWindow::engineRender() {
engine->render();
}
void RenderWindow::onInit(LifeCycleEventCallback func) {
if (engineInitialized) {
resolveOnInit(func);
}
else {
onInits.push_back(func);
}
}
void RenderWindow::resolveOnInits() {
for(std::vector<LifeCycleEventCallback>::iterator itr = onInits.begin(); itr != onInits.end(); ++itr) {
LifeCycleEventCallback func = *itr;
resolveOnInit(func);
}
}
void RenderWindow::resolveOnInit(LifeCycleEventCallback callback) {
callback(this);
}