raylib-bindings 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/raylib.rb CHANGED
@@ -1,154 +1,178 @@
1
- # Yet another raylib wrapper for Ruby
2
- #
3
- # * https://github.com/vaiorabbit/raylib-bindings
4
-
5
- require 'ffi'
6
- require_relative 'raylib_main.rb'
7
- require_relative 'raymath.rb'
8
- require_relative 'rlgl.rb'
9
- require_relative 'raygui.rb'
10
- require_relative 'physac.rb'
11
-
12
- module Raylib
13
-
14
- extend FFI::Library
15
-
16
- @@raylib_import_done = false
17
- def self.load_lib(libpath, raygui_libpath: nil, physac_libpath: nil)
18
-
19
- unless @@raylib_import_done
20
- begin
21
- lib_paths = [libpath, raygui_libpath, physac_libpath].compact
22
-
23
- ffi_lib_flags :now, :global
24
- ffi_lib *lib_paths
25
- setup_symbols()
26
-
27
- setup_raygui_symbols() if raygui_libpath != nil
28
- setup_physac_symbols() if physac_libpath != nil
29
- rescue => error
30
- puts error
31
- end
32
- end
33
-
34
- end
35
-
36
- def self.setup_symbols()
37
- setup_raylib_symbols()
38
- setup_raymath_symbols()
39
- setup_rlgl_symbols()
40
- end
41
-
42
- #
43
- # Color helper
44
- #
45
-
46
- def Color.from_u8(r = 0, g = 0, b = 0, a = 255)
47
- instance = Color.new
48
- instance[:r] = r
49
- instance[:g] = g
50
- instance[:b] = b
51
- instance[:a] = a
52
- return instance
53
- end
54
-
55
- LIGHTGRAY = Color.from_u8(200, 200, 200, 255)
56
- GRAY = Color.from_u8(130, 130, 130, 255)
57
- DARKGRAY = Color.from_u8(80, 80, 80, 255)
58
- YELLOW = Color.from_u8(253, 249, 0, 255)
59
- GOLD = Color.from_u8(255, 203, 0, 255)
60
- ORANGE = Color.from_u8(255, 161, 0, 255)
61
- PINK = Color.from_u8(255, 109, 194, 255)
62
- RED = Color.from_u8(230, 41, 55, 255)
63
- MAROON = Color.from_u8(190, 33, 55, 255)
64
- GREEN = Color.from_u8(0, 228, 48, 255)
65
- LIME = Color.from_u8(0, 158, 47, 255)
66
- DARKGREEN = Color.from_u8(0, 117, 44, 255)
67
- SKYBLUE = Color.from_u8(102, 191, 255, 255)
68
- BLUE = Color.from_u8(0, 121, 241, 255)
69
- DARKBLUE = Color.from_u8(0, 82, 172, 255)
70
- PURPLE = Color.from_u8(200, 122, 255, 255)
71
- VIOLET = Color.from_u8(135, 60, 190, 255)
72
- DARKPURPLE = Color.from_u8(112, 31, 126, 255)
73
- BEIGE = Color.from_u8(211, 176, 131, 255)
74
- BROWN = Color.from_u8(127, 106, 79, 255)
75
- DARKBROWN = Color.from_u8(76, 63, 47, 255)
76
-
77
- WHITE = Color.from_u8(255, 255, 255, 255)
78
- BLACK = Color.from_u8(0, 0, 0, 255)
79
- BLANK = Color.from_u8(0, 0, 0, 0)
80
- MAGENTA = Color.from_u8(255, 0, 255, 255)
81
- RAYWHITE = Color.from_u8(245, 245, 245, 255)
82
-
83
- #
84
- # Math helper
85
- #
86
-
87
- def Vector2.create(x = 0, y = 0)
88
- instance = Vector2.new
89
- instance[:x] = x
90
- instance[:y] = y
91
- return instance
92
- end
93
-
94
- def Vector3.create(x = 0, y = 0, z = 0)
95
- instance = Vector3.new
96
- instance[:x] = x
97
- instance[:y] = y
98
- instance[:z] = z
99
- return instance
100
- end
101
-
102
- def Vector4.create(x = 0, y = 0, z = 0, w = 0)
103
- instance = Vector4.new
104
- instance[:x] = x
105
- instance[:y] = y
106
- instance[:z] = z
107
- instance[:w] = w
108
- return instance
109
- end
110
-
111
- def Quaternion.create(x = 0, y = 0, z = 0, w = 0)
112
- instance = Quaternion.new
113
- instance[:x] = x
114
- instance[:y] = y
115
- instance[:z] = z
116
- instance[:w] = w
117
- return instance
118
- end
119
-
120
- def Rectangle.create(x = 0, y = 0, width = 0, height = 0)
121
- instance = Rectangle.new
122
- instance[:x] = x
123
- instance[:y] = y
124
- instance[:width] = width
125
- instance[:height] = height
126
- return instance
127
- end
128
-
129
- def BoundingBox.create(*args)
130
- case args.size
131
- when 2
132
- instance = BoundingBox.new
133
- instance[:min] = args[0] # min
134
- instance[:max] = args[1] # max
135
- return instance
136
- when 6
137
- instance = BoundingBox.new
138
- instance[:min] = Vector3.create(args[0], args[1], args[2]) # min_x, min_y, min_z
139
- instance[:max] = Vector3.create(args[3], args[4], args[5]) # max_x, max_y, max_z
140
- return instance
141
- else
142
- raise ArgumentError.new "BoundingBox.create : Number of arguments must be 2 or 6"
143
- end
144
- end
145
-
146
- def Vector3ToFloat(v)
147
- return Vector3ToFloatV(mat)[:v].to_a
148
- end
149
-
150
- def MatrixToFloat(mat)
151
- return MatrixToFloatV(mat)[:v].to_a
152
- end
153
-
154
- end
1
+ # Yet another raylib wrapper for Ruby
2
+ #
3
+ # * https://github.com/vaiorabbit/raylib-bindings
4
+
5
+ require 'ffi'
6
+ require 'fileutils'
7
+ require_relative 'raylib_main.rb'
8
+ require_relative 'raymath.rb'
9
+ require_relative 'rlgl.rb'
10
+ require_relative 'raygui.rb'
11
+ require_relative 'physac.rb'
12
+
13
+ module Raylib
14
+
15
+ extend FFI::Library
16
+
17
+ @@raylib_import_done = false
18
+ def self.load_lib(libpath, raygui_libpath: nil, physac_libpath: nil)
19
+
20
+ unless @@raylib_import_done
21
+ begin
22
+ lib_paths = [libpath, raygui_libpath, physac_libpath].compact
23
+
24
+ ffi_lib_flags :now, :global
25
+ ffi_lib *lib_paths
26
+ setup_symbols()
27
+
28
+ setup_raygui_symbols() if raygui_libpath != nil
29
+ setup_physac_symbols() if physac_libpath != nil
30
+ rescue => error
31
+ puts error
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ def self.setup_symbols()
38
+ setup_raylib_symbols()
39
+ setup_raymath_symbols()
40
+ setup_rlgl_symbols()
41
+ end
42
+
43
+ #
44
+ # Color helper
45
+ #
46
+
47
+ def Color.from_u8(r = 0, g = 0, b = 0, a = 255)
48
+ instance = Color.new
49
+ instance[:r] = r
50
+ instance[:g] = g
51
+ instance[:b] = b
52
+ instance[:a] = a
53
+ return instance
54
+ end
55
+
56
+ LIGHTGRAY = Color.from_u8(200, 200, 200, 255)
57
+ GRAY = Color.from_u8(130, 130, 130, 255)
58
+ DARKGRAY = Color.from_u8(80, 80, 80, 255)
59
+ YELLOW = Color.from_u8(253, 249, 0, 255)
60
+ GOLD = Color.from_u8(255, 203, 0, 255)
61
+ ORANGE = Color.from_u8(255, 161, 0, 255)
62
+ PINK = Color.from_u8(255, 109, 194, 255)
63
+ RED = Color.from_u8(230, 41, 55, 255)
64
+ MAROON = Color.from_u8(190, 33, 55, 255)
65
+ GREEN = Color.from_u8(0, 228, 48, 255)
66
+ LIME = Color.from_u8(0, 158, 47, 255)
67
+ DARKGREEN = Color.from_u8(0, 117, 44, 255)
68
+ SKYBLUE = Color.from_u8(102, 191, 255, 255)
69
+ BLUE = Color.from_u8(0, 121, 241, 255)
70
+ DARKBLUE = Color.from_u8(0, 82, 172, 255)
71
+ PURPLE = Color.from_u8(200, 122, 255, 255)
72
+ VIOLET = Color.from_u8(135, 60, 190, 255)
73
+ DARKPURPLE = Color.from_u8(112, 31, 126, 255)
74
+ BEIGE = Color.from_u8(211, 176, 131, 255)
75
+ BROWN = Color.from_u8(127, 106, 79, 255)
76
+ DARKBROWN = Color.from_u8(76, 63, 47, 255)
77
+
78
+ WHITE = Color.from_u8(255, 255, 255, 255)
79
+ BLACK = Color.from_u8(0, 0, 0, 255)
80
+ BLANK = Color.from_u8(0, 0, 0, 0)
81
+ MAGENTA = Color.from_u8(255, 0, 255, 255)
82
+ RAYWHITE = Color.from_u8(245, 245, 245, 255)
83
+
84
+ #
85
+ # Math helper
86
+ #
87
+
88
+ def Vector2.create(x = 0, y = 0)
89
+ instance = Vector2.new
90
+ instance[:x] = x
91
+ instance[:y] = y
92
+ return instance
93
+ end
94
+
95
+ def Vector3.create(x = 0, y = 0, z = 0)
96
+ instance = Vector3.new
97
+ instance[:x] = x
98
+ instance[:y] = y
99
+ instance[:z] = z
100
+ return instance
101
+ end
102
+
103
+ def Vector4.create(x = 0, y = 0, z = 0, w = 0)
104
+ instance = Vector4.new
105
+ instance[:x] = x
106
+ instance[:y] = y
107
+ instance[:z] = z
108
+ instance[:w] = w
109
+ return instance
110
+ end
111
+
112
+ def Quaternion.create(x = 0, y = 0, z = 0, w = 0)
113
+ instance = Quaternion.new
114
+ instance[:x] = x
115
+ instance[:y] = y
116
+ instance[:z] = z
117
+ instance[:w] = w
118
+ return instance
119
+ end
120
+
121
+ def Rectangle.create(x = 0, y = 0, width = 0, height = 0)
122
+ instance = Rectangle.new
123
+ instance[:x] = x
124
+ instance[:y] = y
125
+ instance[:width] = width
126
+ instance[:height] = height
127
+ return instance
128
+ end
129
+
130
+ def BoundingBox.create(*args)
131
+ case args.size
132
+ when 2
133
+ instance = BoundingBox.new
134
+ instance[:min] = args[0] # min
135
+ instance[:max] = args[1] # max
136
+ return instance
137
+ when 6
138
+ instance = BoundingBox.new
139
+ instance[:min] = Vector3.create(args[0], args[1], args[2]) # min_x, min_y, min_z
140
+ instance[:max] = Vector3.create(args[3], args[4], args[5]) # max_x, max_y, max_z
141
+ return instance
142
+ else
143
+ raise ArgumentError.new "BoundingBox.create : Number of arguments must be 2 or 6"
144
+ end
145
+ end
146
+
147
+ def Vector3ToFloat(v)
148
+ return Vector3ToFloatV(mat)[:v].to_a
149
+ end
150
+
151
+ def MatrixToFloat(mat)
152
+ return MatrixToFloatV(mat)[:v].to_a
153
+ end
154
+
155
+ #
156
+ # Generate sample code
157
+ #
158
+ def self.template
159
+ # Copy template code to user's current directory
160
+ example_path = Gem::Specification.find_by_name('raylib-bindings').full_gem_path + '/examples'
161
+ template_code_src = example_path + '/template.rb'
162
+ unless File.exist? template_code_src
163
+ $stderr.puts "[Error] Raylib.template : Template source #{template_code_src} not found"
164
+ return false
165
+ end
166
+
167
+ template_code_dst = Dir.getwd + '/template.rb'
168
+ if File.exist? template_code_dst
169
+ $stderr.puts "[Error] Raylib.template : Template destination #{template_code_dst} already exists"
170
+ return false
171
+ end
172
+
173
+ $stderr.puts "[Info] Raylib.template : #{template_code_src} => #{template_code_dst}"
174
+ FileUtils.copy template_code_src, template_code_dst
175
+ $stderr.puts "[Info] Raylib.template : Done"
176
+ end
177
+
178
+ end