]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Complete rewrite of extension handling
[libs/gl.git] / source / extension.cpp
1 #include <set>
2 #ifndef WIN32
3 #define GLX_GLXEXT_PROTOTYPES
4 #include <GL/glx.h>
5 #endif
6 #include <msp/strings/format.h>
7 #include <msp/strings/utils.h>
8 #include "error.h"
9 #include "extension.h"
10 #include "gl.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Extension::Extension(const char *n, InitFunc f):
18         name(n),
19         init_func(f),
20         init_done(false),
21         support(UNSUPPORTED)
22 { }
23
24 Extension::operator bool() const
25 {
26         if(!init_done)
27         {
28                 support = init_func();
29                 init_done = true;
30         }
31
32         return support>UNSUPPORTED;
33 }
34
35
36 Require::Require(const Extension &ext)
37 {
38         if(!ext)
39                 throw unsupported_extension(ext.get_name());
40 }
41
42
43 bool is_supported(const string &ext)
44 {
45         static set<string> extensions;
46         static bool init_done = false;
47
48         if(!init_done)
49         {
50                 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
51                 {
52                         vector<string> exts = split(gl_ext);
53                         extensions.insert(exts.begin(), exts.end());
54                 }
55
56                 init_done = true;
57         }
58
59         return extensions.count(ext);
60 }
61
62 const Version &get_gl_version()
63 {
64         static Version version;
65         static bool init_done = false;
66
67         if(!init_done)
68         {
69                 string gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
70                 vector<string> parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.');
71                 version.major = lexical_cast<unsigned>(parts[0]);
72                 version.minor = lexical_cast<unsigned>(parts[1]);
73
74                 init_done = true;
75         }
76
77         return version;
78 }
79
80 bool is_version_at_least(unsigned a, unsigned b)
81 {
82         const Version &ver = get_gl_version();
83         return (ver.major>a || (ver.major==a && ver.minor>=b));
84 }
85
86 ExtFunc *get_proc_address(const string &name)
87 {
88 #ifndef WIN32
89         return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
90 #else
91         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
92 #endif
93 }
94
95 } // namespace GL
96 } // namespace Msp