This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathGraphicsManager.cpp
More file actions
executable file
·448 lines (391 loc) · 11.4 KB
/
GraphicsManager.cpp
File metadata and controls
executable file
·448 lines (391 loc) · 11.4 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "GraphicsManager.h"
#include "SpriteBatch.h"
#include "ScaleSystem.h"
#include "../Logger.h"
#include "../defines.h"
#include "../Scenes/SceneManager.h"
#include "../Objects/BaseCamera.h"
#include "../Scenes/BaseScene.h"
#include "../Components/CameraComponent.h"
#ifdef DESKTOP
#include <wglext.h>
#endif
#ifdef MOBILE
#include <GLES/gl.h>
#endif
namespace star
{
GraphicsManager::GraphicsManager()
: Singleton<GraphicsManager>()
, mHorizontalViewportOffset(0)
, mVerticalViewportOffset(0)
, mViewProjectionMatrix()
, mViewInverseMatrix()
, mProjectionMatrix()
, mScreenResolution(0,0)
, mViewportResolution(0,0)
, mbHasWindowChanged(false)
, mIsInitialized(false)
#ifdef DESKTOP
, mWglSwapIntervalEXT(NULL)
, mWglGetSwapIntervalEXT(NULL)
#else
, mDisplay()
, mSurface()
, mContext()
#endif
{
LOG(star::LogLevel::Info,
_T("Graphics Manager : Constructor"), STARENGINE_LOG_TAG);
}
GraphicsManager::~GraphicsManager()
{
LOG(star::LogLevel::Info,
_T("Graphics Manager : Destructor"), STARENGINE_LOG_TAG);
}
void GraphicsManager::CalculateViewPort()
{
vec2 screenRes = GetWindowResolution();
vec2 workingRes = ScaleSystem::GetInstance()->GetWorkingResolution();
float32 width = screenRes.x / workingRes.x;
float32 height = screenRes.y / workingRes.y;
mHorizontalViewportOffset = 0;
mVerticalViewportOffset = 0;
float32 aspectRatio(0);
if(width > height)
{
height = screenRes.y;
aspectRatio = (workingRes.x / workingRes.y);
width = height * aspectRatio;
mHorizontalViewportOffset = static_cast<int32>((screenRes.x - width)/2);
}
else
{
width = screenRes.x;
aspectRatio = (workingRes.y / workingRes.x);
height = width * aspectRatio;
mVerticalViewportOffset = static_cast<int32>((screenRes.y - height)/2);
}
glViewport(mHorizontalViewportOffset, mVerticalViewportOffset, static_cast<int32>(width), static_cast<int32>(height));
mViewportResolution.x = width;
mViewportResolution.y = height;
ScaleSystem::GetInstance()->CalculateScale();
}
void GraphicsManager::SetVSync(bool vSync)
{
#ifdef DESKTOP
//Enables or disables VSync.
//0 = No Sync , 1+ = VSync
//Default value is 1.
if(!vSync)
{
mWglSwapIntervalEXT(0);
}
else
{
mWglSwapIntervalEXT(1);
}
#else
LOG(LogLevel::Warning,
_T("Setting VSync on mobile is not supported. Default VSync is enabled"),
STARENGINE_LOG_TAG);
#endif
}
bool GraphicsManager::GetVSync() const
{
#ifdef DESKTOP
return !(mWglGetSwapIntervalEXT() == 0);
#else
LOG(LogLevel::Warning,
_T("Toggeling VSync on mobile is not supported. Default VSync is enabled"),
STARENGINE_LOG_TAG);
return true;
#endif
}
#ifdef DESKTOP
void GraphicsManager::Initialize(int32 screenWidth, int32 screenHeight)
{
if(!mIsInitialized)
{
mScreenResolution.x = float32(screenWidth);
mScreenResolution.y = float32(screenHeight);
glewInit();
LOG(star::LogLevel::Info,
_T("Graphics Manager : Initializing OpenGL Functors"),
STARENGINE_LOG_TAG);
if(!InitializeOpenGLFunctors())
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : Graphics card doesn't support VSync option!!"),
STARENGINE_LOG_TAG);
}
SetVSync(true);
//Initializes base GL state.
//DEPTH_TEST is default disabled
InitializeOpenGLStates();
mIsInitialized = true;
}
}
#else
void GraphicsManager::Initialize(const android_app* pApplication)
{
if(!mIsInitialized)
{
LOG(star::LogLevel::Info,
_T("Graphics Manager : Initialize"), STARENGINE_LOG_TAG);
EGLint lFormat, lNumConfigs, lErrorResult;
EGLConfig lConfig;
// Defines display requirements. 16bits mode here.
const EGLint lAttributes[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_BLUE_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_RED_SIZE, 5,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (mDisplay == EGL_NO_DISPLAY)
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : No display found"), STARENGINE_LOG_TAG);
return;
}
if (!eglInitialize(mDisplay, NULL, NULL))
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : Could not initialize display"), STARENGINE_LOG_TAG);
return;
}
if(!eglChooseConfig(mDisplay, lAttributes, &lConfig, 1,&lNumConfigs) || (lNumConfigs <= 0))
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : No display config"), STARENGINE_LOG_TAG);
return;
}
if (!eglGetConfigAttrib(mDisplay, lConfig,EGL_NATIVE_VISUAL_ID, &lFormat))
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : No config attributes"), STARENGINE_LOG_TAG);
return;
}
ANativeWindow_setBuffersGeometry(pApplication->window, 0, 0,lFormat);
mSurface = eglCreateWindowSurface(mDisplay, lConfig, pApplication->window, NULL );
if (mSurface == EGL_NO_SURFACE)
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : Could not create surface"), STARENGINE_LOG_TAG);
return;
}
EGLint contextAttrs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
mContext = eglCreateContext(mDisplay, lConfig, EGL_NO_CONTEXT, contextAttrs);
if (mContext == EGL_NO_CONTEXT)
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : Could not create context"), STARENGINE_LOG_TAG);
return;
}
EGLint sX(0), sY(0);
if (!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext)
|| !eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &sX)
|| !eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &sY)
|| (sX <= 0) || (sY <= 0))
{
LOG(star::LogLevel::Error,
_T("Graphics Manager : Could not activate display"),
STARENGINE_LOG_TAG);
return;
}
//[COMMENT] This might be redundant!
mViewportResolution.x = sX;
mViewportResolution.y = sY;
mScreenResolution = mViewportResolution;
glViewport(0, 0, mViewportResolution.x, mViewportResolution.y);
InitializeOpenGLStates();
LOG(star::LogLevel::Info,
_T("Graphics Manager : Initialized"), STARENGINE_LOG_TAG);
mIsInitialized = true;
}
}
void GraphicsManager::Destroy()
{
LOG(star::LogLevel::Info,
_T("Graphics Manager : Destroy"), STARENGINE_LOG_TAG);
// Destroys OpenGL context.
if (mDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,EGL_NO_CONTEXT);
if (mContext != EGL_NO_CONTEXT)
{
eglDestroyContext(mDisplay, mContext);
mContext = EGL_NO_CONTEXT;
}
if (mSurface != EGL_NO_SURFACE)
{
eglDestroySurface(mDisplay, mSurface);
mSurface = EGL_NO_SURFACE;
}
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
LOG(star::LogLevel::Info,
_T("Graphics Manager : Destroyed"), STARENGINE_LOG_TAG);
mIsInitialized = false;
}
}
#endif
void GraphicsManager::InitializeOpenGLStates()
{
//glDisable(GL_DEPTH_TEST);
glClearColor(0.f, 0.f, 0.f, 1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
void GraphicsManager::StartDraw()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
void GraphicsManager::StopDraw()
{
#ifdef ANDROID
if (eglSwapBuffers(mDisplay, mSurface) != EGL_TRUE)
{
return;
}
#endif
}
void GraphicsManager::Update()
{
if(SceneManager::GetInstance()->GetActiveScene())
{
auto projectionObject(SceneManager::GetInstance()->GetActiveScene()->GetActiveCamera());
if(projectionObject)
{
const mat4& projection = projectionObject->GetComponent<CameraComponent>()
->GetProjection();
const mat4& view = projectionObject->GetComponent<CameraComponent>()
->GetView();
const mat4& viewInverse = projectionObject->GetComponent<CameraComponent>()
->GetViewInverse();
mProjectionMatrix = projection;
mViewMatrix = view;
mViewInverseMatrix = viewInverse;
mViewProjectionMatrix = projection * viewInverse;
}
}
}
int32 GraphicsManager::GetWindowWidth() const
{
return int32(mScreenResolution.x);
}
int32 GraphicsManager::GetWindowHeight() const
{
return int32(mScreenResolution.y);
}
int32 GraphicsManager::GetViewportWidth() const
{
return int32(mViewportResolution.x);
}
int32 GraphicsManager::GetViewportHeight() const
{
return int32(mViewportResolution.y);
}
int32 GraphicsManager::GetScreenWidth() const
{
return int32(ScaleSystem::GetInstance()->GetWorkingResolution().x);
}
int32 GraphicsManager::GetScreenHeight() const
{
return int32(ScaleSystem::GetInstance()->GetWorkingResolution().y);
}
const mat4& GraphicsManager::GetViewInverseProjectionMatrix() const
{
return mViewProjectionMatrix;
}
const mat4& GraphicsManager::GetProjectionMatrix() const
{
return mProjectionMatrix;
}
const mat4& GraphicsManager::GetViewMatrix() const
{
return mViewMatrix;
}
const mat4& GraphicsManager::GetViewInverseMatrix() const
{
return mViewInverseMatrix;
}
float32 GraphicsManager::GetWindowAspectRatio() const
{
return mScreenResolution.x / mScreenResolution.y;
}
const vec2 & GraphicsManager::GetWindowResolution() const
{
return mScreenResolution;
}
const vec2 & GraphicsManager::GetViewportResolution() const
{
return mViewportResolution;
}
const vec2 & GraphicsManager::GetScreenResolution() const
{
return ScaleSystem::GetInstance()->GetWorkingResolution();
}
float32 GraphicsManager::GetViewportAspectRatio() const
{
return mViewportResolution.x / mViewportResolution.y;
}
int32 GraphicsManager::GetHorizontalViewportOffset() const
{
return mHorizontalViewportOffset;
}
int32 GraphicsManager::GetVerticalViewportOffset() const
{
return mVerticalViewportOffset;
}
void GraphicsManager::SetWindowDimensions(int32 width, int32 height)
{
mScreenResolution.x = float32(width);
mScreenResolution.y = float32(height);
CalculateViewPort();
}
void GraphicsManager::SetHasWindowChanged(bool isTrue)
{
mbHasWindowChanged = isTrue;
if(isTrue)
{
CalculateViewPort();
}
}
bool GraphicsManager::GetHasWindowChanged() const
{
return mbHasWindowChanged;
}
#ifdef DESKTOP
bool GraphicsManager::WGLExtensionSupported(const schar* extension_name)
{
// this is the pointer to the function which returns the pointer to string with the list of all wgl extensions
PFNWGLGETEXTENSIONSSTRINGEXTPROC _wglGetExtensionsStringEXT = NULL;
// determine pointer to wglGetExtensionsStringEXT function
_wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC) wglGetProcAddress("wglGetExtensionsStringEXT");
if (strstr(_wglGetExtensionsStringEXT(), extension_name) == NULL)
{
// string was not found
return false;
}
// extension is supported
return true;
}
bool GraphicsManager::InitializeOpenGLFunctors()
{
if (WGLExtensionSupported("WGL_EXT_swap_control"))
{
// Extension is supported, init pointers.
mWglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
// this is another function from WGL_EXT_swap_control extension
mWglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) wglGetProcAddress("wglGetSwapIntervalEXT");
return true;
}
return false;
}
#endif
}