]> git.mxchange.org Git - simgear.git/blob - simgear/screen/extensions.cxx
Merge branch 'ehofman/framebuffer' into next
[simgear.git] / simgear / screen / extensions.cxx
1 /*
2  *
3  * Copyright (c) 2001 César Blecua Udías    All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * CESAR BLECUA UDIAS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  */
24
25 #include <string.h>
26
27 #include "extensions.hxx"
28 #include <simgear/debug/logstream.hxx>
29 #if !defined(WIN32)
30 #  include <dlfcn.h>
31 #endif
32
33 bool SGSearchExtensionsString(const char *extString, const char *extName) {
34     // Returns GL_TRUE if the *extName string appears in the *extString string,
35     // surrounded by white spaces, or GL_FALSE otherwise.
36
37     const char *p, *end;
38     int n, extNameLen;
39
40     if ((extString == NULL) || (extName == NULL))
41         return false;
42
43     extNameLen = strlen(extName);
44
45     p=extString;
46     end = p + strlen(p);
47
48     while (p < end) {
49         n = strcspn(p, " ");
50         if ((extNameLen == n) && (strncmp(extName, p, n) == 0))
51             return GL_TRUE;
52
53         p += (n + 1);
54     }
55
56     return GL_FALSE;
57 }
58
59 bool SGIsOpenGLExtensionSupported(const char *extName) {
60    // Returns GL_TRUE if the OpenGL Extension whose name is *extName
61    // is supported by the system, or GL_FALSE otherwise.
62    //
63    // The *extName string must follow the OpenGL extensions naming scheme
64    // (ie: "GL_type_extension", like GL_EXT_convolution)
65
66     return SGSearchExtensionsString((const char *)glGetString(GL_EXTENSIONS),extName);
67 }
68
69 #ifdef __APPLE__
70
71 #include <CoreFoundation/CoreFoundation.h>
72
73 void* macosxGetGLProcAddress(const char *func) {
74
75   /* We may want to cache the bundleRef at some point */
76   static CFBundleRef bundle = 0;
77
78   if (!bundle) {
79
80     CFURLRef bundleURL = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
81                                                         CFSTR("/System/Library/Frameworks/OpenGL.framework"), kCFURLPOSIXPathStyle, true);
82
83     bundle = CFBundleCreate (kCFAllocatorDefault, bundleURL);
84     CFRelease (bundleURL);
85   }
86
87   if (!bundle)
88     return 0;
89
90   CFStringRef functionName = CFStringCreateWithCString
91     (kCFAllocatorDefault, func, kCFStringEncodingASCII);
92   
93   void *function;
94   
95   function = CFBundleGetFunctionPointerForName (bundle, functionName);
96
97   CFRelease (functionName);
98
99   return function;
100 }
101
102 #elif !defined( WIN32 )
103
104 void *SGGetGLProcAddress(const char *func) {
105     static void *libHandle = NULL;
106     static void *(*glXGetProcAddressPtr)(const GLubyte*) = 0;
107     void *fptr = NULL;
108
109     /*
110      * Clear the error buffer
111      */
112     dlerror();
113
114     /*
115      * Since libGL must be linked to the binary we run on, this is the
116      * right handle. That 'current binary' handle also avoids conflicts which
117      * arise from linking with a different libGL at link time an than later
118      * use the standard libGL at runtime ...
119      */
120     if (libHandle == NULL) {
121         libHandle = dlopen(NULL, RTLD_LAZY);
122
123         if (!libHandle) {
124             const char *error = dlerror();
125             if (error) {
126                 SG_LOG(SG_GENERAL, SG_INFO, error);
127                 return 0;
128             }
129         }
130
131         void* symbol = dlsym(libHandle, "glXGetProcAddress");
132         if (!symbol)
133             symbol = dlsym(libHandle, "glXGetProcAddressARB");
134         glXGetProcAddressPtr = (void *(*)(const GLubyte*)) symbol;
135     }
136
137     // First try the glx api function for that
138     if (glXGetProcAddressPtr) {
139         fptr = glXGetProcAddressPtr((const GLubyte*)func);
140
141     } else if (libHandle != NULL) {
142         fptr = dlsym(libHandle, func);
143
144         const char *error = dlerror();
145         if (error)
146             SG_LOG(SG_GENERAL, SG_INFO, error);
147     }
148
149     return fptr;
150 }
151
152 #endif
153