]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Windows compatibility:
[libs/gl.git] / source / extension.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <set>
9 #ifndef WIN32
10 #include <GL/glx.h>
11 #endif
12 #include <msp/strings/formatter.h>
13 #include <msp/strings/utils.h>
14 #include "arb_shader_objects.h"
15 #include "arb_vertex_buffer_object.h"
16 #include "arb_vertex_shader.h"
17 #include "ext_framebuffer_object.h"
18 #include "except.h"
19 #include "extension.h"
20 #include "gl.h"
21 #include "version_1_2.h"
22 #include "version_1_3.h"
23
24 using namespace std;
25
26 namespace Msp {
27 namespace GL {
28
29 bool is_supported(const string &ext)
30 {
31         static set<string> extensions;
32         static bool init_done=false;
33
34         if(!init_done)
35         {
36                 if(const char *gl_ext=reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
37                 {
38                         vector<string> exts=split(gl_ext);
39                         extensions.insert(exts.begin(), exts.end());
40                 }
41
42                 /* XXX Conceptually a bit weird place for this, but I couldn't really come up
43                    with anything better that would still be transparent. */
44                 if(extensions.count("GL_ARB_shader_objects"))
45                         init_arb_shader_objects();
46                 if(extensions.count("GL_ARB_vertex_shader"))
47                         init_arb_vertex_shader();
48                 if(extensions.count("GL_EXT_framebuffer_object"))
49                         init_ext_framebuffer_object();
50                 if(extensions.count("GL_ARB_vertex_buffer_object"))
51                         init_arb_vertex_buffer_object();
52
53                 init_done=true;
54         }
55
56         return extensions.count(ext);
57 }
58
59 const Version &get_gl_version()
60 {
61         static Version version;
62         static bool init_done=false;
63
64         if(!init_done)
65         {
66                 string gl_ver=reinterpret_cast<const char *>(glGetString(GL_VERSION));
67                 vector<string> parts=split(gl_ver.substr(0, gl_ver.find(' ')), '.');
68                 version.major=lexical_cast<unsigned>(parts[0]);
69                 version.minor=lexical_cast<unsigned>(parts[1]);
70
71                 unsigned combined=version.major*0x100+version.minor;
72                 if(combined>=0x102)
73                         init_version_1_2();
74                 if(combined>=0x103)
75                         init_version_1_3();
76         }
77
78         return version;
79 }
80
81 void require_extension(const string &ext)
82 {
83         if(!is_supported(ext))
84                 throw UnsupportedExtension(ext);
85 }
86
87 void require_version(unsigned a, unsigned b)
88 {
89         const Version &ver=get_gl_version();
90         if(ver.major<a || (ver.major==a && ver.minor<b))
91                 throw UnsupportedExtension(format("OpenGL %d.%d", a, b));
92 }
93
94 ExtFunc *get_proc_address(const string &name)
95 {
96 #ifndef WIN32
97         return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(name.c_str()));
98 #else
99         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
100 #endif
101 }
102
103 } // namespace GL
104 } // namespace Msp