Menu

[r1793]: / trunk / LWJGL / src / native / common / extgl.c  Maximize  Restore  History

Download this file

281 lines (242 with data), 8.0 kB

  1
  2
  3
  4
  5
  6
  7
  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
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* ----------------------------------------------------------------------------
Copyright (c) 2001-2002, Lev Povalahev
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name of the author may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
------------------------------------------------------------------------------*/
/*
Lev Povalahev
levp@gmx.net
http://www.uni-karlsruhe.de/~uli2/
*/
#include <stdio.h>
#include <string.h>
#include "extgl.h"
#include "common_tools.h"
/* turn off the warning for the borland compiler*/
#ifdef __BORLANDC__
#pragma warn -8064
#pragma warn -8065
#endif /* __BORLANDC__ */
#ifdef _X11
#include <dlfcn.h>
#endif
#ifdef _MACOSX
aglChoosePixelFormatPROC aglChoosePixelFormat = NULL;
aglDestroyPixelFormatPROC aglDestroyPixelFormat = NULL;
aglNextPixelFormatPROC aglNextPixelFormat = NULL;
aglDescribePixelFormatPROC aglDescribePixelFormat = NULL;
aglDevicesOfPixelFormatPROC aglDevicesOfPixelFormat = NULL;
aglQueryRendererInfoPROC aglQueryRendererInfo = NULL;
aglDestroyRendererInfoPROC aglDestroyRendererInfo = NULL;
aglNextRendererInfoPROC aglNextRendererInfo = NULL;
aglDescribeRendererPROC aglDescribeRenderer = NULL;
aglCreateContextPROC aglCreateContext = NULL;
aglDestroyContextPROC aglDestroyContext = NULL;
aglCopyContextPROC aglCopyContext = NULL;
aglUpdateContextPROC aglUpdateContext = NULL;
aglSetCurrentContextPROC aglSetCurrentContext = NULL;
aglGetCurrentContextPROC aglGetCurrentContext = NULL;
aglSetDrawablePROC aglSetDrawable = NULL;
aglSetOffScreenPROC aglSetOffScreen = NULL;
aglSetFullScreenPROC aglSetFullScreen = NULL;
aglGetDrawablePROC aglGetDrawable = NULL;
aglSetVirtualScreenPROC aglSetVirtualScreen = NULL;
aglGetVirtualScreenPROC aglGetVirtualScreen = NULL;
aglGetVersionPROC aglGetVersion = NULL;
aglSwapBuffersPROC aglSwapBuffers = NULL;
aglEnablePROC aglEnable = NULL;
aglDisablePROC aglDisable = NULL;
aglIsEnabledPROC aglIsEnabled = NULL;
aglSetIntegerPROC aglSetInteger = NULL;
aglGetIntegerPROC aglGetInteger = NULL;
aglUseFontPROC aglUseFont = NULL;
aglGetErrorPROC aglGetError = NULL;
aglErrorStringPROC aglErrorString = NULL;
aglResetLibraryPROC aglResetLibrary = NULL;
aglSurfaceTexturePROC aglSurfaceTexture = NULL;
#endif
#ifndef _MACOSX
struct ExtensionTypes extgl_Extensions;
#endif
#ifdef _WIN32
HMODULE lib_gl_handle = NULL;
#endif
#ifdef _X11
void * lib_gl_handle = NULL;
typedef void * (APIENTRY * glXGetProcAddressARBPROC) (const GLubyte *procName);
static glXGetProcAddressARBPROC glXGetProcAddressARB;
#endif
#ifdef _MACOSX
#include <mach-o/dyld.h>
static const struct mach_header *opengl_lib_handle = NULL;
#endif
/* getProcAddress */
void *extgl_GetProcAddress(const char *name)
{
#ifdef _WIN32
void *t = wglGetProcAddress(name);
if (t == NULL)
{
t = GetProcAddress(lib_gl_handle, name);
if (t == NULL)
{
printfDebug("Could not locate symbol %s\n", name);
}
}
return t;
#endif
#ifdef _X11
void *t = (void*)glXGetProcAddressARB((const GLubyte*)name);
if (t == NULL)
{
t = dlsym(lib_gl_handle, name);
if (t == NULL)
{
printfDebug("Could not locate symbol %s\n", name);
}
}
return t;
#endif
#ifdef _MACOSX
#define BUFFER_SIZE 1024
char mach_name[BUFFER_SIZE] = "_";
strncat(mach_name, name, BUFFER_SIZE - 1);
if (NSIsSymbolNameDefinedInImage(opengl_lib_handle, mach_name)) {
NSSymbol sym = NSLookupSymbolInImage(opengl_lib_handle, mach_name, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
void *address = NSAddressOfSymbol(sym);
return address;
} else
return NULL;
#endif
}
void extgl_InitializeClass(JNIEnv *env, jclass clazz, int num_functions, JavaMethodAndExtFunction *functions) {
ext_InitializeClass(env, clazz, &extgl_GetProcAddress, num_functions, functions);
}
bool extgl_InitializeFunctions(int num_functions, ExtFunction *functions) {
return ext_InitializeFunctions(&extgl_GetProcAddress, num_functions, functions);
}
#ifdef _MACOSX
static const struct mach_header *loadImage(const char *lib_name) {
return NSAddImage(lib_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
}
#endif
bool extgl_QueryExtension(JNIEnv *env, const GLubyte*extensions, const char *name)
{
const GLubyte *start;
GLubyte *where, *terminator;
if (extensions == NULL) {
printfDebug("NULL extension string\n");
return false;
}
/* Extension names should not have spaces. */
where = (GLubyte *) strchr(name, ' ');
if (where || *name == '\0')
return false;
/* It takes a bit of care to be fool-proof about parsing the
OpenGL extensions string. Don't be fooled by sub-strings,
etc. */
start = extensions;
for (;;)
{
where = (GLubyte *) strstr((const char *) start, name);
if (!where)
break;
terminator = where + strlen(name);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0') {
return true;
}
start = terminator;
}
return false;
}
/*-----------------------------------------------------*/
/* AGL stuff END*/
/*-----------------------------------------------------*/
#ifdef _MACOSX
bool extgl_Open(JNIEnv *env) {
if (opengl_lib_handle != NULL)
return true;
opengl_lib_handle = loadImage("/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib");
if (opengl_lib_handle != NULL) {
return true;
} else {
throwException(env, "Could not load OpenGL library");
return false;
}
}
#endif
#ifdef _X11
bool extgl_Open(JNIEnv *env)
{
#define BUFFER_SIZE 2000
static char buffer[BUFFER_SIZE];
if (lib_gl_handle != NULL)
return true;
lib_gl_handle = dlopen("libGL.so.1", RTLD_LAZY | RTLD_GLOBAL);
if (lib_gl_handle == NULL) {
snprintf(buffer, BUFFER_SIZE, "Error loading libGL.so.1: %s", dlerror());
buffer[BUFFER_SIZE - 1] = '\0';
throwException(env, buffer);
return false;
}
glXGetProcAddressARB = (glXGetProcAddressARBPROC)dlsym(lib_gl_handle, "glXGetProcAddressARB");
if (glXGetProcAddressARB == NULL) {
extgl_Close();
throwException(env, "Could not get address of glXGetProcAddressARB");
return false;
}
return true;
}
#endif /* X11 */
#ifdef _WIN32
bool extgl_Open(JNIEnv *env)
{
if (lib_gl_handle != NULL)
return true;
// load the dynamic libraries for OpenGL
lib_gl_handle = LoadLibrary("opengl32.dll");
if (lib_gl_handle == NULL) {
throwException(env, "Could not load OpenGL library");
return false;
}
return true;
}
#endif /* WIN32 */
void extgl_Close(void)
{
#ifdef _X11
dlclose(lib_gl_handle);
lib_gl_handle = NULL;
#endif
#ifdef _WIN32
FreeLibrary(lib_gl_handle);
lib_gl_handle = NULL;
#endif
#ifdef _MACOSX
opengl_lib_handle = NULL;
#endif
}
/* turn on the warning for the borland compiler*/
#ifdef __BORLANDC__
#pragma warn .8064
#pragma warn .8065
#endif /* __BORLANDC__ */
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.