raylib-spinel 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE.md +16 -0
- data/README.md +135 -0
- data/lib/physac.h +1987 -0
- data/lib/physac_compat.rb +43 -0
- data/lib/physac_ffi.rb +128 -0
- data/lib/raygui.h +6479 -0
- data/lib/raygui_compat.rb +116 -0
- data/lib/raygui_ffi.rb +714 -0
- data/lib/raylib_audio_fields.rb +115 -0
- data/lib/raylib_camera_fields.rb +16 -0
- data/lib/raylib_compat.rb +72 -0
- data/lib/raylib_ffi.rb +4093 -0
- data/lib/raylib_field_index.rb +593 -0
- data/lib/raylib_handles.rb +427 -0
- data/lib/raylib_handles_ffi.rb +255 -0
- data/lib/raylib_include.rb +2414 -0
- data/lib/raylib_model_ref.rb +99 -0
- data/lib/raylib_pointer_fields.rb +153 -0
- data/lib/raylib_ruby.rb +614 -0
- data/lib/raylib_sizes.rb +128 -0
- data/lib/raylib_struct_fields.rb +102 -0
- data/lib/raylib_structs.rb +174 -0
- data/lib/raymath_ffi.rb +1014 -0
- data/lib/rcamera_ffi.rb +113 -0
- data/lib/rl_handle_views.h +83 -0
- data/lib/rlgl_constants.rb +382 -0
- data/lib/rlgl_ffi.rb +1016 -0
- data/raylib-spinel/physac.rb +13 -0
- data/raylib-spinel/raygui.rb +14 -0
- data/raylib-spinel/raymath.rb +15 -0
- data/raylib-spinel/rcamera.rb +15 -0
- data/raylib-spinel/rlgl.rb +13 -0
- data/raylib-spinel.rb +56 -0
- data/raylib.rb +37 -0
- data/spin.toml +21 -0
- metadata +97 -0
data/lib/raylib_ruby.rb
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
# raylib_ruby.rb — MRI/CRuby FFI layer using raylib-bindings gem
|
|
2
|
+
# Provides PascalCase API matching Spinel's raylib_adapter.o
|
|
3
|
+
# Loaded conditionally via the RUBY_ENGINE branch in raylib-spinel.rb
|
|
4
|
+
#
|
|
5
|
+
# With method_naming: :original, the gem defines PascalCase methods
|
|
6
|
+
# (BeginDrawing, ClearBackground, etc.) that match our adapter's signatures.
|
|
7
|
+
# We only add custom helpers here.
|
|
8
|
+
|
|
9
|
+
# --- Live reloading (MRI only) ---
|
|
10
|
+
# If the main program set $live_reload_game_lib / $live_reload_game_modules
|
|
11
|
+
# before requiring raylib-spinel, start a file watcher that loads changed
|
|
12
|
+
# game files and signals the main loop to reinitialize.
|
|
13
|
+
if $live_reload_game_lib
|
|
14
|
+
require 'listen'
|
|
15
|
+
require 'debug'
|
|
16
|
+
|
|
17
|
+
$reload_requested = false
|
|
18
|
+
$reload_mutex = Mutex.new
|
|
19
|
+
|
|
20
|
+
listener = Listen.to($live_reload_game_lib) do |modified, added, _removed|
|
|
21
|
+
(modified + added).each do |file|
|
|
22
|
+
next unless $live_reload_game_modules.key?(file)
|
|
23
|
+
|
|
24
|
+
mod_name = $live_reload_game_modules[file]
|
|
25
|
+
begin
|
|
26
|
+
load(file)
|
|
27
|
+
$reload_mutex.synchronize do
|
|
28
|
+
$reload_mod = mod_name
|
|
29
|
+
$reload_requested = true
|
|
30
|
+
end
|
|
31
|
+
puts "[live-reload] reloaded #{mod_name} from #{File.basename(file)}"
|
|
32
|
+
rescue StandardError => e
|
|
33
|
+
warn "[live-reload] error reloading #{file}: #{e}"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
listener.start
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
module Raylib
|
|
42
|
+
# Require the raylib-bindings gem's raylib.rb by absolute path. A plain
|
|
43
|
+
# `require "raylib"` is ambiguous: RubyGems resolves it to the gem literally
|
|
44
|
+
# named "raylib" (if installed) instead of this gem, whose only entry point
|
|
45
|
+
# is also lib/raylib.rb. Requiring by full path sidesteps gem-name resolution
|
|
46
|
+
# so `ruby --yjit main.rb` works without bundle exec.
|
|
47
|
+
raylib_bindings_root = Gem::Specification.find_by_name('raylib-bindings').full_gem_path
|
|
48
|
+
require File.join(raylib_bindings_root, 'lib', 'raylib')
|
|
49
|
+
|
|
50
|
+
shared_lib_path = raylib_bindings_root + '/lib/'
|
|
51
|
+
|
|
52
|
+
case RUBY_PLATFORM
|
|
53
|
+
when /mswin|msys|mingw|cygwin/
|
|
54
|
+
Raylib.load_lib(shared_lib_path + 'libraylib.dll',
|
|
55
|
+
raygui_libpath: shared_lib_path + 'raygui.dll',
|
|
56
|
+
physac_libpath: shared_lib_path + 'physac.dll',
|
|
57
|
+
method_naming: :original)
|
|
58
|
+
when /darwin/
|
|
59
|
+
arch = RUBY_PLATFORM.split('-')[0]
|
|
60
|
+
Raylib.load_lib(shared_lib_path + "libraylib.#{arch}.dylib",
|
|
61
|
+
raygui_libpath: shared_lib_path + "raygui.#{arch}.dylib",
|
|
62
|
+
physac_libpath: shared_lib_path + "physac.#{arch}.dylib",
|
|
63
|
+
method_naming: :original)
|
|
64
|
+
when /linux/
|
|
65
|
+
arch = RUBY_PLATFORM.split('-')[0]
|
|
66
|
+
Raylib.load_lib(shared_lib_path + "libraylib.#{arch}.so",
|
|
67
|
+
raygui_libpath: shared_lib_path + "raygui.#{arch}.so",
|
|
68
|
+
physac_libpath: shared_lib_path + "physac.#{arch}.so",
|
|
69
|
+
method_naming: :original)
|
|
70
|
+
else
|
|
71
|
+
raise "Unknown OS: #{RUBY_PLATFORM}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# ---- load guard ----
|
|
75
|
+
# load_lib warns (it does not raise) when the native library cannot be
|
|
76
|
+
# opened. Without this guard the first symptom of a missing libOpenGL /
|
|
77
|
+
# X11 runtime is the NameError at the UpdateCamera alias far below, which
|
|
78
|
+
# names neither the library nor the reason.
|
|
79
|
+
unless Raylib.respond_to?(:InitWindow)
|
|
80
|
+
raise "raylib-spinel: raylib-bindings' libraylib did not attach (its " \
|
|
81
|
+
"load_lib printed the reason just above — typically a missing " \
|
|
82
|
+
"libOpenGL.so.0 / X11 runtime for the gem's prebuilt binary)"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# ---- DrawTextInt (custom — adapter has rl_DrawTextInt, gem doesn't) ----
|
|
86
|
+
def self.DrawTextInt(prefix, value, x, y, size, color)
|
|
87
|
+
DrawText("#{prefix}#{value}", x, y, size, color)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# ---- SaveRenderTextureImage ----
|
|
91
|
+
# AOT twin in raylib_ffi.rb. Saves a render texture's canvas (y-flipped back
|
|
92
|
+
# to top-down) to path. Lives here so the gem-only image.pointer call is
|
|
93
|
+
# never compiled by Spinel.
|
|
94
|
+
def self.SaveRenderTextureImage(target, path)
|
|
95
|
+
image = LoadImageFromTexture(target.texture)
|
|
96
|
+
ImageFlipVertical(image.pointer)
|
|
97
|
+
ExportImage(image, path)
|
|
98
|
+
UnloadImage(image)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# ---- raygui helpers as module functions ----
|
|
102
|
+
# The gem's value-returning helpers (RGuiCheckBox, RGuiSlider, RGuiListView,
|
|
103
|
+
# ... in raygui_helper.rb) are module *instance* methods, available to code
|
|
104
|
+
# that does `include Raylib`. Scenes call them as Raylib.RGuiX like every
|
|
105
|
+
# other binding, so mirror the whole instance surface onto the module.
|
|
106
|
+
# Methods defined directly on the singleton (the snake_case aliases, the
|
|
107
|
+
# Vector2/Color factories, ...) take precedence over the included ones.
|
|
108
|
+
singleton_class.include(Raylib)
|
|
109
|
+
|
|
110
|
+
# ---- FilePathList#path (mirror of the Spinel-side helper) ----
|
|
111
|
+
# The gem's FilePathList stores a char** in `paths`; expose the same
|
|
112
|
+
# per-entry reader the Spinel adapter provides, so directory scenes can
|
|
113
|
+
# write files.path(i) on both engines.
|
|
114
|
+
class FilePathList
|
|
115
|
+
def path(index)
|
|
116
|
+
return '' if index < 0 || index >= count
|
|
117
|
+
|
|
118
|
+
ptr = paths.get_pointer(index * FFI.type_size(:pointer))
|
|
119
|
+
return '' if ptr.null?
|
|
120
|
+
|
|
121
|
+
ptr.read_string
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class ModelAnimations
|
|
126
|
+
def cleanup
|
|
127
|
+
Raylib.UnloadModelAnimations(@anim_ptrs, @anims.length)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# ---- UpdateCamera ----
|
|
132
|
+
# The gem binds raylib's UpdateCamera(Camera*, int) and expects a raw
|
|
133
|
+
# pointer; Spinel passes the native Camera object straight to the same
|
|
134
|
+
# shim. Accept the Camera object (an FFI::Struct / Camera3D) here and hand
|
|
135
|
+
# FFI its pointer so game code is identical on both backends.
|
|
136
|
+
class << self
|
|
137
|
+
alias __binding_UpdateCamera UpdateCamera
|
|
138
|
+
def UpdateCamera(camera, mode)
|
|
139
|
+
__binding_UpdateCamera(camera.pointer, mode)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# ---- Vector3 helper (mirror of the Color.new override above) ----
|
|
144
|
+
# Under Spinel, Vector3.new(x,y,z) lowers to the native rl_Vector3_new.
|
|
145
|
+
# Under MRI, Vector3 is an FFI::Struct whose positional-arg new is treated
|
|
146
|
+
# as a layout array, so accept the 3-float x/y/z form to match Spinel.
|
|
147
|
+
class Vector3
|
|
148
|
+
class << self
|
|
149
|
+
def new(*args)
|
|
150
|
+
if args.length == 3
|
|
151
|
+
allocate.tap { |v| v.set(*args) }
|
|
152
|
+
else
|
|
153
|
+
super
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# ---- Color helper ----
|
|
160
|
+
# Under Spinel, Color.new(r,g,b,a) lowers to the native rl_Color_new. Under
|
|
161
|
+
# MRI, Raylib::Color is an FFI::Struct whose default .new treats positional
|
|
162
|
+
# args as a layout array, so accept the 4-int RGBA form to match Spinel.
|
|
163
|
+
class Color
|
|
164
|
+
class << self
|
|
165
|
+
def new(*args)
|
|
166
|
+
if args.length == 4
|
|
167
|
+
allocate.tap { |c| c.set(*args) }
|
|
168
|
+
else
|
|
169
|
+
super
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Color factories — same logic as the Spinel side in raylib_ffi.rb.
|
|
174
|
+
def from_u8(r = 0, g = 0, b = 0, a = 255)
|
|
175
|
+
Color.new(r, g, b, a)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def from_u32(rgba = 255)
|
|
179
|
+
Color.new(
|
|
180
|
+
(rgba >> 24) & 0xFF,
|
|
181
|
+
(rgba >> 16) & 0xFF,
|
|
182
|
+
(rgba >> 8) & 0xFF,
|
|
183
|
+
rgba & 0xFF
|
|
184
|
+
)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# ---- PI ----
|
|
190
|
+
# raylib-bindings defines no PI; Spinel's raylib_ffi.rb does. Mirror it so
|
|
191
|
+
# game code can use Raylib::PI on both backends.
|
|
192
|
+
PI = 3.141592653589793
|
|
193
|
+
|
|
194
|
+
# ---- Positional factories (Vector2/Vector4/Matrix) ----
|
|
195
|
+
# Twins of the Spinel-side helpers in raylib_ffi.rb. The gem's native ctors
|
|
196
|
+
# are no-arg out-params too; .new(x, y) would treat the args as an FFI
|
|
197
|
+
# layout description, so construction goes through these factories on both
|
|
198
|
+
# backends (Spinel inlines Klass.new for native structs and cannot honor a
|
|
199
|
+
# .new override).
|
|
200
|
+
def self.Vector2(x, y)
|
|
201
|
+
Vector2.allocate.tap { |v| v.set(x, y) }
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def self.Vector4(x, y, z, w)
|
|
205
|
+
Vector4.allocate.tap { |v| v.set(x, y, z, w) }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def self.Matrix(values)
|
|
209
|
+
Matrix.allocate.tap do |m|
|
|
210
|
+
m.m0 = values[0]
|
|
211
|
+
m.m4 = values[1]
|
|
212
|
+
m.m8 = values[2]
|
|
213
|
+
m.m12 = values[3]
|
|
214
|
+
m.m1 = values[4]
|
|
215
|
+
m.m5 = values[5]
|
|
216
|
+
m.m9 = values[6]
|
|
217
|
+
m.m13 = values[7]
|
|
218
|
+
m.m2 = values[8]
|
|
219
|
+
m.m6 = values[9]
|
|
220
|
+
m.m10 = values[10]
|
|
221
|
+
m.m14 = values[11]
|
|
222
|
+
m.m3 = values[12]
|
|
223
|
+
m.m7 = values[13]
|
|
224
|
+
m.m11 = values[14]
|
|
225
|
+
m.m15 = values[15]
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# ---- LoadFontAtlas (custom — the Spinel adapter builds the font in C) ----
|
|
230
|
+
# LoadFontData + GenImageFontAtlas + texture, mirroring rl_LoadFontAtlas_ptr
|
|
231
|
+
# (used by the SDF text scene for custom glyph ranges / packing).
|
|
232
|
+
def self.LoadFontAtlas(path, font_size, load_glyph_count, atlas_glyph_count,
|
|
233
|
+
padding, pack_method, font_type)
|
|
234
|
+
size_buf = FFI::MemoryPointer.new(:int, 1)
|
|
235
|
+
data = LoadFileData(path, size_buf)
|
|
236
|
+
loaded_buf = FFI::MemoryPointer.new(:int, 1)
|
|
237
|
+
glyphs = LoadFontData(data, size_buf.read_int32, font_size, nil,
|
|
238
|
+
load_glyph_count, font_type, loaded_buf)
|
|
239
|
+
recs_buf = FFI::MemoryPointer.new(:pointer, 1)
|
|
240
|
+
atlas = GenImageFontAtlas(glyphs, recs_buf, atlas_glyph_count, font_size,
|
|
241
|
+
padding, pack_method)
|
|
242
|
+
font = Font.new
|
|
243
|
+
font[:baseSize] = font_size
|
|
244
|
+
font[:glyphCount] = atlas_glyph_count
|
|
245
|
+
font[:glyphPadding] = padding
|
|
246
|
+
font[:texture] = LoadTextureFromImage(atlas)
|
|
247
|
+
font[:recs] = recs_buf.get_pointer(0)
|
|
248
|
+
font[:glyphs] = glyphs
|
|
249
|
+
UnloadImage(atlas)
|
|
250
|
+
UnloadFileData(data)
|
|
251
|
+
font
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# ---- camera component helpers (mirror of the Spinel-side wrappers) ----
|
|
255
|
+
# Scenes read camera.position.x etc. through these because Spinel's
|
|
256
|
+
# polymorphic field dispatch cannot resolve Camera#position once physac's
|
|
257
|
+
# PhysicsBodyData#position is in the translation unit (the require "raylib"
|
|
258
|
+
# entry loads it).
|
|
259
|
+
# ---- font glyph metrics (mirror of the Spinel-side wrappers) ----
|
|
260
|
+
# ---- gamepad name (mirror of the Spinel :str wrapper) ----
|
|
261
|
+
# The gem binds GetGamepadName as :pointer; scenes want the device name.
|
|
262
|
+
def self.GamepadName(gamepad)
|
|
263
|
+
ptr = GetGamepadName(gamepad)
|
|
264
|
+
ptr.nil? ? '' : ptr.read_string
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def self.MultMatrixf(matrix)
|
|
268
|
+
rlMultMatrixf(matrix)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def self.RenderTextureTexture(target)
|
|
272
|
+
target.texture
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def self.BoundingBoxMin(box) = box.min
|
|
276
|
+
def self.BoundingBoxMax(box) = box.max
|
|
277
|
+
|
|
278
|
+
def self.FilePathListPath(list, index)
|
|
279
|
+
list.path(index)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def self.TransformTranslation(xform) = xform.t
|
|
283
|
+
|
|
284
|
+
def self.FontTexture(font)
|
|
285
|
+
font.texture
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def self.FontGlyphAdvanceX(font, index)
|
|
289
|
+
GlyphInfo.new(font.glyphs + index * GlyphInfo.size).advanceX
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def self.FontGlyphRecWidth(font, index)
|
|
293
|
+
Rectangle.new(font.recs + index * Rectangle.size).width
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def self.CameraPositionX(camera) = camera.position.x
|
|
297
|
+
def self.CameraPositionY(camera) = camera.position.y
|
|
298
|
+
def self.CameraPositionZ(camera) = camera.position.z
|
|
299
|
+
def self.CameraTargetX(camera) = camera.target.x
|
|
300
|
+
def self.CameraTargetY(camera) = camera.target.y
|
|
301
|
+
def self.CameraTargetZ(camera) = camera.target.z
|
|
302
|
+
|
|
303
|
+
# ---- rlgl: the Spinel side exposes it as `Rlgl`, the gem on Raylib ----
|
|
304
|
+
# Scenes write Rlgl.rlBegin(RL_TRIANGLES) etc. The alias is installed at the
|
|
305
|
+
# bottom of this file (top level) so the constant resolves like on Spinel.
|
|
306
|
+
|
|
307
|
+
# ---- physac struct helpers (mirror of the Spinel physac shims) ----
|
|
308
|
+
# physac.h makes PhysicsBody opaque; the Spinel side exposes these two field
|
|
309
|
+
# helpers. Mirror them over the gem's PhysicsBodyData layout.
|
|
310
|
+
def self.PhysicsBodySetEnabled(body, enabled)
|
|
311
|
+
PhysicsBodyData.new(body)[:enabled] = enabled ? true : false
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def self.PhysicsBodyGetPosY(body)
|
|
315
|
+
PhysicsBodyData.new(body)[:position].y
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# ---- File text pointer helpers (mirror of the Spinel adapter) ----
|
|
319
|
+
# LoadFileText hands back a heap buffer owned by raylib; reading it into a
|
|
320
|
+
# Ruby String and freeing must go through the same pointer, so the *Ptr pair
|
|
321
|
+
# (and PtrToString) is the portable spelling.
|
|
322
|
+
def self.LoadFileTextPtr(file_name)
|
|
323
|
+
LoadFileText(file_name)
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def self.PtrToString(ptr)
|
|
327
|
+
ptr.read_string
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def self.UnloadFileTextPtr(ptr)
|
|
331
|
+
UnloadFileText(ptr)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# ---- DrawTexturePacked (custom — the Spinel adapter unpacks the tint) ----
|
|
335
|
+
# Draw a sprite with a packed 0xRRGGBBAA tint. Tints are per-bunny random, so
|
|
336
|
+
# the Color is reused and its channels rewritten instead of being allocated
|
|
337
|
+
# (or memoised) per sprite.
|
|
338
|
+
DRAW_TEXTURE_COLOR = Color.new(0, 0, 0, 255)
|
|
339
|
+
|
|
340
|
+
def self.DrawTexturePacked(texture, x, y, rgba)
|
|
341
|
+
c = DRAW_TEXTURE_COLOR
|
|
342
|
+
c.r = (rgba >> 24) & 0xFF
|
|
343
|
+
c.g = (rgba >> 16) & 0xFF
|
|
344
|
+
c.b = (rgba >> 8) & 0xFF
|
|
345
|
+
c.a = rgba & 0xFF
|
|
346
|
+
DrawTexture(texture, x, y, c)
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# ---- DrawPixelsPacked (custom — the Spinel adapter batches the pool) ----
|
|
350
|
+
# Draw `count` 1x1 pixels from parallel pools: x/y floats and packed
|
|
351
|
+
# 0xRRGGBBAA colours. The AOT twin emits the whole pool in one immediate-mode
|
|
352
|
+
# rlBegin/rlEnd; here each pixel is a normal DrawPixel and the (tiny) palette
|
|
353
|
+
# is memoised so no Color is allocated in the loop.
|
|
354
|
+
DRAW_PIXEL_COLORS = {}
|
|
355
|
+
|
|
356
|
+
def self.DrawPixelsPacked(xs, ys, colors, count)
|
|
357
|
+
i = 0
|
|
358
|
+
while i < count
|
|
359
|
+
rgba = colors[i]
|
|
360
|
+
c = DRAW_PIXEL_COLORS[rgba]
|
|
361
|
+
if c.nil?
|
|
362
|
+
c = Color.new((rgba >> 24) & 0xFF, (rgba >> 16) & 0xFF,
|
|
363
|
+
(rgba >> 8) & 0xFF, rgba & 0xFF)
|
|
364
|
+
DRAW_PIXEL_COLORS[rgba] = c
|
|
365
|
+
end
|
|
366
|
+
DrawPixel(xs[i].to_i, ys[i].to_i, c)
|
|
367
|
+
i += 1
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# ---- Model mesh/skeleton helpers (mirror of the Spinel ModelRef) ----
|
|
372
|
+
# The gem's helper surface has material/material_count/bone_count; scenes
|
|
373
|
+
# also index meshes. Its bone_count/bone_info/bind_pose_transform still read
|
|
374
|
+
# the raylib 5 field layout (Model[:boneCount]), but this gem's own Model
|
|
375
|
+
# struct keeps the skeleton under Model[:skeleton] -- so those three are
|
|
376
|
+
# overridden here with the layout the gem actually binds.
|
|
377
|
+
class Model
|
|
378
|
+
# The Spinel views answer #pointer with themselves (a :ptr slot hands C
|
|
379
|
+
# their struct address; see raylib_handles.rb). Mirror that: a scene that
|
|
380
|
+
# keeps `native = model.pointer` and then reads fields off the native
|
|
381
|
+
# surface would otherwise get FFI's raw MemoryPointer here, which has no
|
|
382
|
+
# field accessors. Passing the struct where a pointer is wanted still works
|
|
383
|
+
# (FFI converts it), so the change is compatible.
|
|
384
|
+
def pointer = self
|
|
385
|
+
|
|
386
|
+
def mesh(index = 0)
|
|
387
|
+
Mesh.new(self[:meshes] + index * FFI::NativeType::POINTER.size)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def mesh_count
|
|
391
|
+
self[:meshCount]
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def bone_count
|
|
395
|
+
self[:skeleton][:boneCount]
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# The Spinel Model view keeps a synthetic boneCount (the adapter reads it
|
|
399
|
+
# out of the embedded skeleton), because raylib moved the field there in
|
|
400
|
+
# 5.0 while scenes/gem code still spell it on the model. Mirror it, field
|
|
401
|
+
# index included (:boneCount is not a layout member here).
|
|
402
|
+
def boneCount = self[:skeleton][:boneCount]
|
|
403
|
+
|
|
404
|
+
def boneCount=(v)
|
|
405
|
+
self[:skeleton][:boneCount] = v
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def [](key)
|
|
409
|
+
key == :boneCount ? boneCount : super
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def []=(key, value)
|
|
413
|
+
key == :boneCount ? self.boneCount = value : super
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def bone_info(index)
|
|
417
|
+
BoneInfo.new(self[:skeleton][:bones] + index * BoneInfo.size)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def bind_pose_transform(index)
|
|
421
|
+
Transform.new(self[:skeleton][:bindPose] + index * Transform.size)
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# gem helper name the Spinel ModelSkeleton view also answers
|
|
426
|
+
class ModelSkeleton
|
|
427
|
+
def bone_count = boneCount
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# ---- INLINE struct-array writers (mirror of the Spinel boxed-pointer writers) ----
|
|
431
|
+
# The gem's FFI structs cannot assign an INLINE array member at all: its
|
|
432
|
+
# generated writers (`def projection=(v) self[:projection] = v end`) end in
|
|
433
|
+
# FFI::StructLayout::Array#put, which raises
|
|
434
|
+
# NotImplementedError: cannot set array field
|
|
435
|
+
# (measured on raylib-bindings 0.8.4). The Spinel adapter implements the same
|
|
436
|
+
# writers as a memcpy from the boxed pointer (rl_VrStereoConfig_set_projection
|
|
437
|
+
# & co in raylib_adapter.c), so a scene that installs a block of matrices,
|
|
438
|
+
# floats or ints keeps one spelling on both engines. Mirror that here: copy
|
|
439
|
+
# byte_size bytes from the source pointer into the field's inline storage.
|
|
440
|
+
# Element-index writes (`device.lensDistortionValues[0] = 0.22`) need none of
|
|
441
|
+
# this -- they work through the gem's InlineArray already (core_vr_simulator).
|
|
442
|
+
module InlineArrayWriter
|
|
443
|
+
private
|
|
444
|
+
|
|
445
|
+
# `source` is whatever a pointer-typed value arrives as: the gem's
|
|
446
|
+
# MemAlloc returns FFI::Pointer, an FFI::MemoryPointer is one too, and a
|
|
447
|
+
# bare address (Integer) is accepted like the adapter's rl_p_unbox does.
|
|
448
|
+
# The gem's own InlineArray (the field read back) answers to_ptr, which is
|
|
449
|
+
# what `field = field` hands over.
|
|
450
|
+
def write_inline_array(field, source, byte_size)
|
|
451
|
+
src = case source
|
|
452
|
+
when FFI::Pointer then source
|
|
453
|
+
when Integer then FFI::Pointer.new(source)
|
|
454
|
+
else source.to_ptr
|
|
455
|
+
end
|
|
456
|
+
(pointer + self.class.offset_of(field)).put_bytes(0, src.read_bytes(byte_size))
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# The adapter's char-array writers are snprintf(field, sizeof field, "%s"):
|
|
460
|
+
# copy at most byte_size-1 bytes and NUL-terminate.
|
|
461
|
+
def write_inline_string(field, value, byte_size)
|
|
462
|
+
raw = value.to_s.b
|
|
463
|
+
raw = raw.byteslice(0, byte_size - 1) if raw.bytesize >= byte_size
|
|
464
|
+
padded = raw + ("\0" * (byte_size - raw.bytesize))
|
|
465
|
+
(pointer + self.class.offset_of(field)).put_bytes(0, padded)
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
class VrStereoConfig
|
|
470
|
+
include InlineArrayWriter
|
|
471
|
+
|
|
472
|
+
def projection=(v) write_inline_array(:projection, v, Matrix.size * 2) end
|
|
473
|
+
|
|
474
|
+
def viewOffset=(v) write_inline_array(:viewOffset, v, Matrix.size * 2) end
|
|
475
|
+
|
|
476
|
+
def leftLensCenter=(v) write_inline_array(:leftLensCenter, v, 2 * FFI.type_size(:float)) end
|
|
477
|
+
|
|
478
|
+
def rightLensCenter=(v) write_inline_array(:rightLensCenter, v, 2 * FFI.type_size(:float)) end
|
|
479
|
+
|
|
480
|
+
def leftScreenCenter=(v) write_inline_array(:leftScreenCenter, v, 2 * FFI.type_size(:float)) end
|
|
481
|
+
|
|
482
|
+
def rightScreenCenter=(v)
|
|
483
|
+
write_inline_array(:rightScreenCenter, v, 2 * FFI.type_size(:float))
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
def scale=(v) write_inline_array(:scale, v, 2 * FFI.type_size(:float)) end
|
|
487
|
+
|
|
488
|
+
def scaleIn=(v) write_inline_array(:scaleIn, v, 2 * FFI.type_size(:float)) end
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
class VrDeviceInfo
|
|
492
|
+
include InlineArrayWriter
|
|
493
|
+
|
|
494
|
+
def lensDistortionValues=(v)
|
|
495
|
+
write_inline_array(:lensDistortionValues, v, 4 * FFI.type_size(:float))
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
def chromaAbCorrection=(v)
|
|
499
|
+
write_inline_array(:chromaAbCorrection, v, 4 * FFI.type_size(:float))
|
|
500
|
+
end
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
class AutomationEvent
|
|
504
|
+
include InlineArrayWriter
|
|
505
|
+
|
|
506
|
+
def params=(v) write_inline_array(:params, v, 4 * FFI.type_size(:int)) end
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
class Material
|
|
510
|
+
include InlineArrayWriter
|
|
511
|
+
|
|
512
|
+
def params=(v) write_inline_array(:params, v, 4 * FFI.type_size(:float)) end
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
class MaterialMap
|
|
516
|
+
# Spinel's MaterialMap.new(address, owner) wraps raylib memory
|
|
517
|
+
# (native_new [:any, :any] -> rl_MaterialMap_view_any). FFI's Struct#new
|
|
518
|
+
# only takes an FFI::Pointer -- an Integer is read as an anonymous layout
|
|
519
|
+
# spec, which is the "unable to resolve type ''" TypeError -- and the owner
|
|
520
|
+
# is a Spinel lifetime hint with no FFI equivalent (the view points into
|
|
521
|
+
# the Material's own map array, which the caller keeps alive). Accept both
|
|
522
|
+
# shapes, so `MaterialMap.new(map_ptr.address, material)` is portable.
|
|
523
|
+
class << self
|
|
524
|
+
alias __binding_new new
|
|
525
|
+
|
|
526
|
+
def new(*args)
|
|
527
|
+
return __binding_new(*args) unless args.length == 2
|
|
528
|
+
|
|
529
|
+
address = args[0]
|
|
530
|
+
__binding_new(address.is_a?(FFI::Pointer) ? address : FFI::Pointer.new(address))
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
class BoneInfo
|
|
536
|
+
include InlineArrayWriter
|
|
537
|
+
|
|
538
|
+
def name=(v) write_inline_string(:name, v, 32) end
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
class ModelAnimation
|
|
542
|
+
include InlineArrayWriter
|
|
543
|
+
|
|
544
|
+
def name=(v) write_inline_string(:name, v, 32) end
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# ---- Rectangle factory (mirror of the Spinel-side helper) ----
|
|
548
|
+
# raylib_ffi.rb defines Raylib::Rectangle(x, y, w, h) next to the Vector2/4
|
|
549
|
+
# factories; scenes write Raylib::Rectangle(...) in places, so provide the
|
|
550
|
+
# same call shape here (the gem's own factory is Rectangle.create).
|
|
551
|
+
def self.Rectangle(x, y, width, height)
|
|
552
|
+
Rectangle.create(x, y, width, height)
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# ---- shader uniform writers (twins of the Spinel rl_SetShaderValue* shims) ----
|
|
556
|
+
# AOT declares these over raylib_adapter.c with plain float arguments
|
|
557
|
+
# (raylib_ffi.rb); the gem only binds SetShaderValue(shader, loc, void*, type),
|
|
558
|
+
# so pack the values into one scratch buffer and hand FFI the pointer. Scene
|
|
559
|
+
# code sees the same 4/5/6/7-argument shapes on both engines.
|
|
560
|
+
SHADER_VALUE_BUF = FFI::MemoryPointer.new(:float, 4)
|
|
561
|
+
|
|
562
|
+
def self.rl_SetShaderValueFloat(shader, loc, value, type)
|
|
563
|
+
SHADER_VALUE_BUF.put_float32(0, value.to_f)
|
|
564
|
+
SetShaderValue(shader, loc, SHADER_VALUE_BUF, type)
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
def self.rl_SetShaderValueVec2(shader, loc, x, y, type)
|
|
568
|
+
SHADER_VALUE_BUF.put_float32(0, x.to_f)
|
|
569
|
+
SHADER_VALUE_BUF.put_float32(4, y.to_f)
|
|
570
|
+
SetShaderValue(shader, loc, SHADER_VALUE_BUF, type)
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
def self.rl_SetShaderValueVec3(shader, loc, x, y, z, type)
|
|
574
|
+
SHADER_VALUE_BUF.put_float32(0, x.to_f)
|
|
575
|
+
SHADER_VALUE_BUF.put_float32(4, y.to_f)
|
|
576
|
+
SHADER_VALUE_BUF.put_float32(8, z.to_f)
|
|
577
|
+
SetShaderValue(shader, loc, SHADER_VALUE_BUF, type)
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
def self.rl_SetShaderValueVec4(shader, loc, x, y, z, w, type)
|
|
581
|
+
SHADER_VALUE_BUF.put_float32(0, x.to_f)
|
|
582
|
+
SHADER_VALUE_BUF.put_float32(4, y.to_f)
|
|
583
|
+
SHADER_VALUE_BUF.put_float32(8, z.to_f)
|
|
584
|
+
SHADER_VALUE_BUF.put_float32(12, w.to_f)
|
|
585
|
+
SetShaderValue(shader, loc, SHADER_VALUE_BUF, type)
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
# ---- snake_case aliases (MRI gem :snake_case naming) ----
|
|
589
|
+
# The *_ffi.rb files carry static alias blocks for Spinel; derive the same
|
|
590
|
+
# names at runtime here so gem-style snake_case code runs identically under
|
|
591
|
+
# CRuby. Conversion mirrors raylib_main.rb's entries loop.
|
|
592
|
+
class << self
|
|
593
|
+
Raylib.singleton_methods.each do |m|
|
|
594
|
+
s = m.to_s.gsub(/([A-Z]+)([A-Z0-9][a-z])/, '\1_\2')
|
|
595
|
+
.gsub(/([a-z\d])([A-Z0-9])/, '\1_\2')
|
|
596
|
+
.downcase
|
|
597
|
+
s = s.gsub('vector_3', 'vector3_') if s.include?('vector_3')
|
|
598
|
+
s = s.gsub('vector_2', 'vector2_') if s.include?('vector_2')
|
|
599
|
+
s = s.chop if s.end_with?('_')
|
|
600
|
+
next if s == m.to_s || Raylib.respond_to?(s)
|
|
601
|
+
|
|
602
|
+
alias_method s, m
|
|
603
|
+
# The gem's snake mode attaches names as module *and* instance methods
|
|
604
|
+
# (attach_function + module_function); mirror that so `include Raylib` +
|
|
605
|
+
# bare snake calls work like on the Spinel side.
|
|
606
|
+
Raylib.send(:define_method, s) { |*args| Raylib.send(m, *args) }
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
# rlgl compat: the Spinel build exposes the immediate-mode API (and its RL_*
|
|
612
|
+
# constants) as a top-level `Rlgl` module; the gem binds the same functions on
|
|
613
|
+
# Raylib. Alias the constant so scene code keeps one spelling.
|
|
614
|
+
Object.const_set(:Rlgl, Raylib) unless Object.const_defined?(:Rlgl)
|