]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Fix compilation on 64-bit platforms
[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_program.h"
17 #include "arb_vertex_shader.h"
18 #include "ext_framebuffer_object.h"
19 #include "except.h"
20 #include "extension.h"
21 #include "gl.h"
22 #include "version_1_2.h"
23 #include "version_1_3.h"
24
25 using namespace std;
26
27 namespace Msp {
28 namespace GL {
29
30 bool is_supported(const string &ext)
31 {
32         static set<string> extensions;
33         static bool init_done=false;
34
35         if(!init_done)
36         {
37                 if(const char *gl_ext=reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
38                 {
39                         vector<string> exts=split(gl_ext);
40                         extensions.insert(exts.begin(), exts.end());
41                 }
42
43                 /* XXX Conceptually a bit weird place for this, but I couldn't really come up
44                    with anything better that would still be transparent. */
45                 if(extensions.count("GL_ARB_shader_objects"))
46                         init_arb_shader_objects();
47                 if(extensions.count("GL_ARB_vertex_shader"))
48                         init_arb_vertex_shader();
49                 if(extensions.count("GL_ARB_vertex_program"))
50                         init_arb_vertex_program();
51                 if(extensions.count("GL_EXT_framebuffer_object"))
52                         init_ext_framebuffer_object();
53                 if(extensions.count("GL_ARB_vertex_buffer_object"))
54                         init_arb_vertex_buffer_object();
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                 unsigned combined=version.major*0x100+version.minor;
75                 if(combined>=0x102)
76                         init_version_1_2();
77                 if(combined>=0x103)
78                         init_version_1_3();
79
80                 init_done = true;
81         }
82
83         return version;
84 }
85
86 void require_extension(const string &ext)
87 {
88         if(!is_supported(ext))
89                 throw UnsupportedExtension(ext);
90 }
91
92 void require_version(unsigned a, unsigned b)
93 {
94         const Version &ver=get_gl_version();
95         if(ver.major<a || (ver.major==a && ver.minor<b))
96                 throw UnsupportedExtension(format("OpenGL %d.%d", a, b));
97 }
98
99 ExtFunc *get_proc_address(const string &name)
100 {
101 #ifndef WIN32
102         return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(name.c_str()));
103 #else
104         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
105 #endif
106 }
107
108 } // namespace GL
109 } // namespace Msp