glew_head.c 7.15 KB
Newer Older
mem's avatar
mem committed
1
#include <GL/glew.h>
2

ikits's avatar
ikits committed
3
4
#if defined(_WIN32)
#  include <GL/wglew.h>
5
#elif !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX))
ikits's avatar
ikits committed
6
#  include <GL/glxew.h>
ikits's avatar
ikits committed
7
#endif
mem's avatar
mem committed
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Define glewGetContext and related helper macros.
 */
#ifdef GLEW_MX
#  define glewGetContext() ctx
#  ifdef _WIN32
#    define GLEW_CONTEXT_ARG_DEF_INIT GLEWContext* ctx
#    define GLEW_CONTEXT_ARG_VAR_INIT ctx
#    define wglewGetContext() ctx
#    define WGLEW_CONTEXT_ARG_DEF_INIT WGLEWContext* ctx
#    define WGLEW_CONTEXT_ARG_DEF_LIST WGLEWContext* ctx
#  else /* _WIN32 */
#    define GLEW_CONTEXT_ARG_DEF_INIT void
#    define GLEW_CONTEXT_ARG_VAR_INIT
#    define glxewGetContext() ctx
#    define GLXEW_CONTEXT_ARG_DEF_INIT void
#    define GLXEW_CONTEXT_ARG_DEF_LIST GLXEWContext* ctx
#  endif /* _WIN32 */
#  define GLEW_CONTEXT_ARG_DEF_LIST GLEWContext* ctx
#else /* GLEW_MX */
#  define GLEW_CONTEXT_ARG_DEF_INIT void
#  define GLEW_CONTEXT_ARG_VAR_INIT
#  define GLEW_CONTEXT_ARG_DEF_LIST void
#  define WGLEW_CONTEXT_ARG_DEF_INIT void
#  define WGLEW_CONTEXT_ARG_DEF_LIST void
#  define GLXEW_CONTEXT_ARG_DEF_INIT void
#  define GLXEW_CONTEXT_ARG_DEF_LIST void
#endif /* GLEW_MX */
ikits's avatar
ikits committed
37

38
#if defined(GLEW_REGAL)
39
40
41
42
43

/* In GLEW_REGAL mode we call direcly into the linked
   libRegal.so glGetProcAddressREGAL for looking up
   the GL function pointers. */

44
45
46
47
48
49
50
51
52
#  undef glGetProcAddressREGAL
#  ifdef WIN32
extern void *  __stdcall glGetProcAddressREGAL(const GLchar *name);
static void * (__stdcall * regalGetProcAddress) (const GLchar *) = glGetProcAddressREGAL;
#    else
extern void * glGetProcAddressREGAL(const GLchar *name);
static void * (*regalGetProcAddress) (const GLchar *) = glGetProcAddressREGAL;
#  endif
#  define glGetProcAddressREGAL GLEW_GET_FUN(__glewGetProcAddressREGAL)
53

54
#elif defined(__sgi) || defined (__sun) || defined(__HAIKU__) || defined(GLEW_APPLE_GLX)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

void* dlGetProcAddress (const GLubyte* name)
{
  static void* h = NULL;
  static void* gpa;

  if (h == NULL)
  {
    if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
    gpa = dlsym(h, "glXGetProcAddress");
  }

  if (gpa != NULL)
    return ((void*(*)(const GLubyte*))gpa)(name);
  else
    return dlsym(h, (const char*)name);
}
#endif /* __sgi || __sun || GLEW_APPLE_GLX */

ikits's avatar
ikits committed
77
#if defined(__APPLE__)
ikits's avatar
ikits committed
78
79
#include <stdlib.h>
#include <string.h>
80
81
82
83
84
85
86
87
88
#include <AvailabilityMacros.h>

#ifdef MAC_OS_X_VERSION_10_3

#include <dlfcn.h>

void* NSGLGetProcAddress (const GLubyte *name)
{
  static void* image = NULL;
89
  void* addr;
90
91
92
93
  if (NULL == image) 
  {
    image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
  }
94
  if( !image ) return NULL;
95
  addr = dlsym(image, (const char*)name);
96
97
98
99
100
101
  if( addr ) return addr;
#ifdef GLEW_APPLE_GLX
  return dlGetProcAddress( name ); // try next for glx symbols
#else
  return NULL;
#endif
102
103
104
105
}
#else

#include <mach-o/dyld.h>
ikits's avatar
ikits committed
106

107
void* NSGLGetProcAddress (const GLubyte *name)
ikits's avatar
ikits committed
108
{
ikits's avatar
ikits committed
109
  static const struct mach_header* image = NULL;
ikits's avatar
ikits committed
110
  NSSymbol symbol;
111
  char* symbolName;
ikits's avatar
ikits committed
112
113
114
115
  if (NULL == image)
  {
    image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  }
ikits's avatar
ikits committed
116
  /* prepend a '_' for the Unix C symbol mangling convention */
117
118
  symbolName = malloc(strlen((const char*)name) + 2);
  strcpy(symbolName+1, (const char*)name);
ikits's avatar
ikits committed
119
120
  symbolName[0] = '_';
  symbol = NULL;
ikits's avatar
ikits committed
121
122
123
  /* if (NSIsSymbolNameDefined(symbolName))
	 symbol = NSLookupAndBindSymbol(symbolName); */
  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
ikits's avatar
ikits committed
124
  free(symbolName);
125
126
127
128
129
130
  if( symbol ) return NSAddressOfSymbol(symbol);
#ifdef GLEW_APPLE_GLX
  return dlGetProcAddress( name ); // try next for glx symbols
#else
  return NULL;
#endif
ikits's avatar
ikits committed
131
}
132
#endif /* MAC_OS_X_VERSION_10_3 */
ikits's avatar
ikits committed
133
#endif /* __APPLE__ */
134

135
136
137
/*
 * Define glewGetProcAddress.
 */
138
#if defined(GLEW_REGAL)
139
#  define glewGetProcAddress(name) regalGetProcAddress((const GLchar *) name)
140
#elif defined(_WIN32)
141
#  define glewGetProcAddress(name) wglGetProcAddress((LPCSTR)name)
142
143
#elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX)
#  define glewGetProcAddress(name) NSGLGetProcAddress(name)
144
#elif defined(__sgi) || defined(__sun) || defined(__HAIKU__)
145
146
147
#  define glewGetProcAddress(name) dlGetProcAddress(name)
#elif defined(__ANDROID__)
#  define glewGetProcAddress(name) NULL /* TODO */
148
149
#elif defined(__native_client__)
#  define glewGetProcAddress(name) NULL /* TODO */
150
151
#else /* __linux */
#  define glewGetProcAddress(name) (*glXGetProcAddressARB)(name)
152
153
#endif

ikits's avatar
ikits committed
154
155
156
157
158
/*
 * Define GLboolean const cast.
 */
#define CONST_CAST(x) (*(GLboolean*)&x)

159
160
161
162
/*
 * GLEW, just like OpenGL or GLU, does not rely on the standard C library.
 * These functions implement the functionality required in this file.
 */
ikits's avatar
ikits committed
163
static GLuint _glewStrLen (const GLubyte* s)
164
165
{
  GLuint i=0;
ikits's avatar
ikits committed
166
167
  if (s == NULL) return 0;
  while (s[i] != '\0') i++;
168
169
  return i;
}
mem's avatar
mem committed
170

ikits's avatar
ikits committed
171
static GLuint _glewStrCLen (const GLubyte* s, GLubyte c)
172
173
{
  GLuint i=0;
ikits's avatar
ikits committed
174
175
  if (s == NULL) return 0;
  while (s[i] != '\0' && s[i] != c) i++;
ikits's avatar
ikits committed
176
  return (s[i] == '\0' || s[i] == c) ? i : 0;
177
}
178

ikits's avatar
ikits committed
179
static GLboolean _glewStrSame (const GLubyte* a, const GLubyte* b, GLuint n)
180
181
{
  GLuint i=0;
ikits's avatar
ikits committed
182
183
184
  if(a == NULL || b == NULL)
    return (a == NULL && b == NULL && n == 0) ? GL_TRUE : GL_FALSE;
  while (i < n && a[i] != '\0' && b[i] != '\0' && a[i] == b[i]) i++;
185
186
  return i == n ? GL_TRUE : GL_FALSE;
}
187

ikits's avatar
ikits committed
188
static GLboolean _glewStrSame1 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
ikits's avatar
ikits committed
189
190
191
{
  while (*na > 0 && (**a == ' ' || **a == '\n' || **a == '\r' || **a == '\t'))
  {
ikits's avatar
ikits committed
192
193
    (*a)++;
    (*na)--;
ikits's avatar
ikits committed
194
195
196
197
  }
  if(*na >= nb)
  {
    GLuint i=0;
ikits's avatar
ikits committed
198
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
nigels's avatar
nigels committed
199
200
201
202
203
204
    if(i == nb)
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
ikits's avatar
ikits committed
205
206
207
208
  }
  return GL_FALSE;
}

ikits's avatar
ikits committed
209
static GLboolean _glewStrSame2 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
210
211
212
213
{
  if(*na >= nb)
  {
    GLuint i=0;
ikits's avatar
ikits committed
214
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
nigels's avatar
nigels committed
215
216
217
218
219
220
    if(i == nb)
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
221
222
223
224
  }
  return GL_FALSE;
}

ikits's avatar
ikits committed
225
static GLboolean _glewStrSame3 (GLubyte** a, GLuint* na, const GLubyte* b, GLuint nb)
226
{
ikits's avatar
ikits committed
227
  if(*na >= nb)
228
229
  {
    GLuint i=0;
ikits's avatar
ikits committed
230
231
    while (i < nb && (*a)+i != NULL && b+i != NULL && (*a)[i] == b[i]) i++;
    if (i == nb && (*na == nb || (*a)[i] == ' ' || (*a)[i] == '\n' || (*a)[i] == '\r' || (*a)[i] == '\t'))
ikits's avatar
ikits committed
232
233
234
235
236
    {
      *a = *a + nb;
      *na = *na - nb;
      return GL_TRUE;
    }
237
238
239
  }
  return GL_FALSE;
}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

/*
 * Search for name in the extensions string. Use of strstr()
 * is not sufficient because extension names can be prefixes of
 * other extension names. Could use strtok() but the constant
 * string returned by glGetString might be in read-only memory.
 */
static GLboolean _glewSearchExtension (const char* name, const GLubyte *start, const GLubyte *end)
{
  const GLubyte* p;
  GLuint len = _glewStrLen((const GLubyte*)name);
  p = start;
  while (p < end)
  {
    GLuint n = _glewStrCLen(p, ' ');
    if (len == n && _glewStrSame((const GLubyte*)name, p, n)) return GL_TRUE;
    p += n+1;
  }
  return GL_FALSE;
}