/
glelp.cpp
151 lines (118 loc) · 3.57 KB
/
glelp.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
// GLELP 0.1 - OpenGL Extenstions Helper
// copyright (c) 2016 Anders Malm - Ekoli / Odious ^ S/N ^ Censor
#include "glelp.h"
// UGLY HACK, I just want to use as little windows specific things as possible
// WGL_ARB_pixel_format
PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB;
PFNWGLGETPIXELFORMATATTRIBFVARBPROC wglGetPixelFormatAttribfvARB;
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
// WGL_ARB_create_context
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
//WGL_ARB_extensions_string
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
namespace glelp
{
/*
* Checks if the asked for version is less or equal compared
* to the version provided by the driver
*/
bool checkVersion(char major, char minor)
{
// this might be happening too quickly..
if (glGetString == 0)
{
glGetString = (PFNGLGETSTRINGPROC)GetProcAddress(GetModuleHandle(TEXT("opengl32.dll")), "glGetString");
}
const GLubyte* version = glGetString(GL_VERSION);
int GLmajor = version[0] - '0';
int GLminor = version[2] - '0';
if(major > GLmajor)
return false;
if(minor > GLminor)
{
if( major < GLmajor)
return true;
else
return false;
}
return true;
}
bool loadHardcodedWindowsExtension()
{
if(!glelp::checkAvailable("WGL_ARB_pixel_format")) return false;
wglGetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
wglGetPixelFormatAttribfvARB = (PFNWGLGETPIXELFORMATATTRIBFVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribfvARB");
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
if(!glelp::checkAvailable("WGL_ARB_create_context")) return false;
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if(!checkAvailable("WGL_ARB_extensions_string")) return false;
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
return true;
}
bool loadExtensions()
{
// Check that we support the minimum version required
if(!checkVersion(GLELP_GL_MAJOR_VERSION, GLELP_GL_MINOR_VERSION))
return false;
// load hard coded windows extensions needed
if(!loadHardcodedWindowsExtension())
return false;
return initExtensions();
}
const char* wext;
/*
Quick hack for comparing two strings so we can live without std libraries
*/
bool compare(const char* str1, const char* str2)
{
unsigned int i = 0;
while(true)
{
char c1 = str1[i];
char c2 = str2[i];
if(c1 != c2) return false;
if((c1 == 0) && (c2 == 0)) return true;
i++;
}
}
/*
Check each extensiom
*/
bool findExtension(const char* extension, const char* extensions)
{
unsigned char ci = 0;
char current[256];
while(true)
{
while((*extensions) != ' ')
{
if((*extensions) == 0)
return false;
current[ci] = (*extensions);
extensions++;
ci++;
}
current[ci] = 0;
if(compare(current, extension))
return true;
extensions++;
ci = 0;
}
}
bool checkAvailable(const char* extension)
{
// Check windows extensions
if(wglGetExtensionsStringARB == 0)
{
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
HWND hWnd = GetActiveWindow();
HDC hDC = GetDC(hWnd);
wext = wglGetExtensionsStringARB(hDC);
}
if(findExtension(extension, (char*)glGetString(GL_EXTENSIONS)))
return true;
if (findExtension(extension, (char*)wext))
return true;
return false;
}
}