opengl-bindings2 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ module GL
2
+
3
+ GL_FUNCTIONS_MAP = {}
4
+ @@gl_dll = nil
5
+
6
+ # Open dll/dylib/so for symbol import
7
+ # - Note that OpenGL APIs won't be available until you call import_symbols
8
+ def self.open_lib(lib_path: nil)
9
+ if lib_path == nil
10
+ case self.get_platform
11
+ when :OPENGL_PLATFORM_WINDOWS
12
+ lib_path = 'C:/Windows/System32/opengl32.dll'
13
+ when :OPENGL_PLATFORM_MACOSX
14
+ lib_path = '/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib'
15
+ else
16
+ lib_path = 'libGL.so' # not tested
17
+ end
18
+ end
19
+ @@gl_dll = Fiddle.dlopen(lib_path)
20
+ end
21
+
22
+ # Import OpenGL APIs
23
+ # - Call this after OpenGL context becomes vailable (e.g.: glfwMakeContextCurrent)
24
+ def self.import_symbols(output_error: false)
25
+ GL_FUNCTION_SYMBOLS.each do |sym|
26
+ begin
27
+ bind_command(sym) if GL_FUNCTIONS_MAP[sym] == nil || GL_FUNCTIONS_MAP[sym].ptr == 0
28
+ rescue
29
+ $stderr.puts("[Warning] opengl_common.rb : Failed to import #{sym}.") if output_error
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.load_lib(lib_path = nil, output_import_error = false)
35
+ if lib_path == nil
36
+ lib_path = case self.get_platform
37
+ when :OPENGL_PLATFORM_WINDOWS
38
+ 'C:/Windows/System32/opengl32.dll'
39
+ when :OPENGL_PLATFORM_MACOSX
40
+ '/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib'
41
+ else
42
+ libGL.so # not tested
43
+ end
44
+ end
45
+
46
+ open_lib(lib_path: lib_path)
47
+ import_symbols(output_error: output_import_error)
48
+ end
49
+
50
+ # [OBSOLETE]
51
+ def self.load_dll(lib = nil, path = nil)
52
+ $stderr.puts "[Warning] opengl_common.rb : OpenGL.load_dll is deprecated, use OpenGL.load_lib instead"
53
+ self.load_lib(lib, path)
54
+ end
55
+
56
+ # [INTERNAL]
57
+ def self.get_command( sym )
58
+ if GL_FUNCTIONS_MAP[sym] == nil
59
+ bind_command( sym )
60
+ end
61
+ return GL_FUNCTIONS_MAP[sym]
62
+ end
63
+
64
+ # [INTERNAL]
65
+ def self.bind_command( sym )
66
+ begin
67
+ GL_FUNCTIONS_MAP[sym] = Fiddle::Function.new( @@gl_dll[sym.to_s],
68
+ GL_FUNCTIONS_ARGS_MAP[sym],
69
+ GL_FUNCTIONS_RETVAL_MAP[sym] )
70
+ rescue
71
+ if self.get_platform == :OPENGL_PLATFORM_WINDOWS
72
+ func_ptr = wglGetProcAddress(sym.to_s)
73
+ GL_FUNCTIONS_MAP[sym] = Fiddle::Function.new( func_ptr,
74
+ GL_FUNCTIONS_ARGS_MAP[sym],
75
+ GL_FUNCTIONS_RETVAL_MAP[sym] )
76
+ end
77
+ raise RuntimeError if GL_FUNCTIONS_MAP[sym] == nil
78
+ end
79
+ end
80
+
81
+ end