-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.d
160 lines (132 loc) · 3.84 KB
/
engine.d
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
/**
* Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
* Authors: $(Gabriel Gheorghe)
* License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
* Source: $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/graphics/engine.d)
* Documentation:
* Coverage:
**/
module liberty.graphics.engine;
import bindbc.opengl;
import liberty.math.vector;
import liberty.core.engine;
import liberty.logger;
import liberty.core.platform;
import liberty.graphics.constants;
import liberty.graphics.backend.impl;
/// Graphics engine base class.
final abstract class GfxEngine {
///
static GfxBackend backend;
/// Initialize graphics engine by creating the backend.
static void initialize() {
backend = GfxBackend.createBackend();
}
/// Resize the frame buffer viewport.
/// If backend is not created then it will generate a warning message.
static void resizeFrameBufferViewport(int width, int height) {
try {
if (backend !is null) {
glViewport(0, 0, width, height);
} else
Logger.warning(
"You are trying to resize frame buffer viewport without a backend",
typeof(this).stringof
);
} catch (Exception e) {}
}
///
debug static void runtimeCheckErr() {
immutable GLint er = glGetError();
if (er != GL_NO_ERROR) {
string getErrorString = getErrorString(er);
flushErrors;
Logger.error(getErrorString, typeof(this).stringof);
}
}
///
debug static bool runtimeCheckWarn() {
immutable GLint er = glGetError();
if (er != GL_NO_ERROR) {
string getErrorString = getErrorString(er);
flushErrors;
Logger.warning(getErrorString, typeof(this).stringof);
return false;
}
return true;
}
///
static const(char)[] getString(uint name) {
import std.string : fromStringz;
const(char)* sZ = glGetString(name);
debug runtimeCheckErr;
return (sZ is null) ? "(unknown)" : sZ.fromStringz;
}
///
static const(char)[] getString(uint name, uint index) {
import std.string : fromStringz;
const(char)* sZ = glGetStringi(name, index);
debug runtimeCheckErr;
return (sZ is null) ? "(unknown)" : sZ.fromStringz;
}
///
static const(char)[] getVersionString() {
return getString(GL_VERSION);
}
///
static const(char)[] getVendorString() {
return getString(GL_VENDOR);
}
///
static GfxVendor getVendor() {
import std.algorithm.searching : canFind;
const(char)[] s = getVendorString();
if (canFind(s, "AMD"))
return GfxVendor.AMD;
else if (canFind(s, "NVIDIA"))
return GfxVendor.NVIDIA;
else if (canFind(s, "Intel"))
return GfxVendor.INTEL;
return GfxVendor.UNKNOWN;
}
///
static const(char)[] getRendererString() {
return getString(GL_RENDERER);
}
///
static const(char)[] getShadingVersionString() {
return getString(GL_SHADING_LANGUAGE_VERSION);
}
///
static int getInt(uint pname) {
GLint param;
glGetIntegerv(pname, ¶m);
debug runtimeCheckErr;
return param;
}
///
static float getFloat(uint pname) {
GLfloat res;
glGetFloatv(pname, &res);
debug runtimeCheckErr;
return res;
}
private static string getErrorString(int er) {
switch (er) {
case GL_NO_ERROR: return "GL_NO_ERROR";
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
default: return "Unknown OpenGL error";
}
}
private static void flushErrors() {
int timeout;
while (++timeout <= 5) {
immutable r = glGetError();
if (r == GL_NO_ERROR)
break;
}
}
}