raylib-bindings 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/raylib.rb CHANGED
@@ -1,178 +1,178 @@
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
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