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