1
|
|
|
import ctypes as ct |
2
|
|
|
from ctypes.util import find_library |
3
|
|
|
|
4
|
|
|
import platform |
5
|
|
|
osName = platform.system() |
6
|
|
|
|
7
|
|
|
def define_function(libName, name, returnType, params): |
8
|
|
|
'''Helper function to help in binding functions''' |
9
|
|
|
if osName == "Windows": |
10
|
|
|
function = ct.WINFUNCTYPE(returnType, *params) |
11
|
|
|
lib = ct.WinDLL(libName) |
12
|
|
|
elif osName == "Darwin" or osName == "Linux": |
13
|
|
|
function = ct.FUNCTYPE(returnType, *params) |
14
|
|
|
lib = ct.CDLL(find_library(libName)) |
15
|
|
|
|
16
|
|
|
address = getattr(lib, name) |
17
|
|
|
new_func = ct.cast(address, function) |
18
|
|
|
|
19
|
|
|
return new_func |
20
|
|
|
|
21
|
|
|
if osName == "Windows": |
22
|
|
|
glGetProcAddress = define_function('opengl32', 'wglGetProcAddress', |
23
|
|
|
ct.POINTER(ct.c_int), (ct.c_char_p,)) |
24
|
|
|
|
25
|
|
|
elif osName in ('Linux', 'Darwin', 'Windows'): |
26
|
|
|
from sdl2 import SDL_GL_GetProcAddress |
27
|
|
|
glGetProcAddress = SDL_GL_GetProcAddress |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class _BindGL(object): |
31
|
|
|
def __init__(self): |
32
|
|
|
self.osName = platform.system() |
33
|
|
|
|
34
|
|
|
if self.osName == 'Linux': |
35
|
|
|
libFound = find_library('GL') |
36
|
|
|
self.lib = ct.CDLL(libFound) |
37
|
|
|
self.funcType = ct.CFUNCTYPE |
38
|
|
|
elif self.osName == 'Windows': |
39
|
|
|
libFound = find_library('opengl32') |
40
|
|
|
self.lib = ct.WinDLL(libFound) |
41
|
|
|
self.funcType = ct.WINFUNCTYPE |
42
|
|
|
elif self.osName == 'Darwin': # Mac OS X |
43
|
|
|
libraryPath = '/System/Library/Frameworks/OpenGL.framework' |
44
|
|
|
libFound = find_library(libraryPath) |
45
|
|
|
self.lib = ct.CDLL(libFound) |
46
|
|
|
self.funcType = ct.CFUNCTYPE |
47
|
|
|
|
48
|
|
|
def gl_func(self, name, returnType, paramTypes): |
49
|
|
|
''' Define and load an opengl function ''' |
50
|
|
|
function = self.funcType(returnType, *paramTypes) |
51
|
|
|
|
52
|
|
|
try: |
53
|
|
|
address = getattr(self.lib, name) |
54
|
|
|
except AttributeError: |
55
|
|
|
name = name.encode(encoding='UTF-8') |
56
|
|
|
address = glGetProcAddress(name) |
57
|
|
|
|
58
|
|
|
return ct.cast(address, function) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
_glbind = _BindGL() |
62
|
|
|
gl_func = _glbind.gl_func |
63
|
|
|
|
64
|
|
|
__all__ = ['gl_func', 'define_function'] |
65
|
|
|
|