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.
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ModelRef — a user-class wrapper over Raylib::Model.
4
+ #
5
+ # Why this exists: a Ruby method defined on a *native* class has no arm in a
6
+ # polymorphic dispatch, so `models.each { |model| model.material(0) }` raises
7
+ # NoMethodError when the array holds native Model views (see
8
+ # tools/repro/upstream/a_native_class_methods/). A *user class* does get arms.
9
+ #
10
+ # Every reader and writer below therefore calls a module ffi_func with @model
11
+ # (the native view) as its argument — the shape that resolves from a poly
12
+ # receiver. Delegating instead to a Ruby method on Model through the ivar
13
+ # (`@model.material_count`) only works while the ivar's type is pinned to Model
14
+ # and goes poly (NoMethodError) as soon as it is a union, which is what two gem
15
+ # corpus programs hit.
16
+ #
17
+ # The Model factories return this wrapper, and the helpers that take a Model
18
+ # unwrap it again with #model.
19
+ module Raylib
20
+ class ModelRef
21
+ def initialize(model)
22
+ @model = model
23
+ end
24
+
25
+ # The underlying native view, for the helpers that hand it to C.
26
+ def model = @model
27
+ alias pointer model
28
+
29
+ def material(index = 0) = Material.new(Raylib.rl_Model_material_ptr(@model, index), @model)
30
+ def mesh(index = 0) = Mesh.new(Raylib.rl_Model_mesh_ptr(@model, index), @model)
31
+ def bone_info(index = 0) = BoneInfo.new(Raylib.rl_Model_bone_ptr(@model, index), @model)
32
+
33
+ def bind_pose_transform(index = 0) =
34
+ Transform.new(Raylib.rl_Model_bind_pose_ptr(@model, index), @model)
35
+
36
+ def transform
37
+ m = Matrix.new
38
+ Raylib.rl_Model_transform(@model, m)
39
+ m
40
+ end
41
+
42
+ def transform=(matrix)
43
+ Raylib.rl_Model_set_transform(@model, matrix)
44
+ end
45
+
46
+ def skeleton = ModelSkeleton.new(Raylib.rl_Model_skeleton_ptr(@model), @model)
47
+
48
+ def skeleton=(s)
49
+ Raylib.rl_Model_set_skeleton(@model, s)
50
+ end
51
+
52
+ # counts, gem names and snake names
53
+ def meshCount = Raylib.rl_m_Model_meshCount_v(@model)
54
+ def materialCount = Raylib.rl_m_Model_materialCount_v(@model)
55
+ def boneCount = Raylib.rl_m_Model_boneCount_v(@model)
56
+ def mesh_count = meshCount
57
+ def material_count = materialCount
58
+ def bone_count = boneCount
59
+
60
+ # pointer fields, gem names
61
+ def meshes = Ptr.new(Raylib.rl_m_Model_meshes(@model))
62
+ def materials = Ptr.new(Raylib.rl_m_Model_materials(@model))
63
+ def meshMaterial = Ptr.new(Raylib.rl_m_Model_meshMaterial(@model))
64
+ def currentPose = Ptr.new(Raylib.rl_m_Model_currentPose(@model))
65
+ def boneMatrices = Ptr.new(Raylib.rl_m_Model_boneMatrices(@model))
66
+
67
+ def meshes=(v)
68
+ Raylib.rl_Model_set_meshes_ptr(@model, Raylib.ptr_addr(v))
69
+ end
70
+
71
+ def materials=(v)
72
+ Raylib.rl_Model_set_materials_ptr(@model, Raylib.ptr_addr(v))
73
+ end
74
+
75
+ def meshMaterial=(v)
76
+ Raylib.rl_Model_set_meshMaterial_ptr(@model, Raylib.ptr_addr(v))
77
+ end
78
+
79
+ def currentPose=(v)
80
+ Raylib.rl_Model_set_currentPose_ptr(@model, Raylib.ptr_addr(v))
81
+ end
82
+
83
+ def boneMatrices=(v)
84
+ Raylib.rl_Model_set_boneMatrices_ptr(@model, Raylib.ptr_addr(v))
85
+ end
86
+
87
+ # The field index, reads only. The writes stay out until Model#[]= no longer
88
+ # refuses a nullable value ("incompatible types when assigning to type
89
+ # sp_oint from type sp_int"); the writers above cover the fields directly.
90
+ def [](key)
91
+ case key
92
+ when :meshCount then meshCount
93
+ when :materialCount then materialCount
94
+ when :boneCount then boneCount
95
+ else raise KeyError, "no such field: #{key}"
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Pointer-typed struct fields, gem style: `mesh.vertices = MemAlloc(bytes)` then
4
+ # `mesh.vertices.put_array_of_float32(byte_offset, [floats])`. The readers wrap
5
+ # the field address in Raylib::Ptr, which adds the gem's method spelling (the
6
+ # package's own Pointer writes arrays at offset 0; the gem's take a byte offset).
7
+ # The writers are native_method declarations ([:any], and the C side unboxes the
8
+ # three shapes a pointer value arrives as -- see rl_p_unbox in raylib_adapter.c).
9
+ #
10
+ # Compat entry only: the base cannot name FFI::Pointer.
11
+
12
+ module Raylib
13
+ # A pointer value over a raw address, with the gem's spellings (the package's
14
+ # own Pointer writes arrays at offset 0; the gem's take a byte offset). A
15
+ # plain class, not an FFI::Pointer subclass: under AOT a subclass of the
16
+ # package class inherits neither its constructor ("expected 0") nor its
17
+ # accessors ("undefined method 'address'").
18
+ #
19
+ # Its one ivar IS the address, and the C side of the raw-pointer field writers
20
+ # reads it in place, because the frozen ABI cannot call #address from C (see
21
+ # rl_p_unbox in raylib_adapter.c). For that read to be defined, @address must
22
+ # have the SAME layout in every program -- and its inferred type is otherwise
23
+ # program-dependent (measured: sp_int in scene binaries, sp_RbVal once the ffi
24
+ # package loads: a polymorphic `address`/`to_i` send -- the package's Pointer
25
+ # family shares the names -- merges the arms' return types, and a method that
26
+ # returns the ivar *is* the ivar to the type analyzer, which then widens it).
27
+ # So initialize deliberately boxes it: [address, nil] cannot become a typed
28
+ # int array, its .first is an sp_RbVal, and the ivar is an INT-boxed int in
29
+ # every program (measured: sp_RbVal iv_address here even with no operand on
30
+ # the ivar at all -- the widening is unavoidable, so it is forced instead).
31
+ # rl_p_unbox then reads the box's payload, which also makes the package's own
32
+ # FFI::Pointer/MemoryPointer objects (first ivar @address, likewise boxed)
33
+ # unboxable -- the corpus writes a field through all three shapes.
34
+ #
35
+ # Never assign a non-Integer to @address: a boxed STR/FLOAT payload under the
36
+ # OBJ branch reads the wrong union arm, exactly the bug class this fixes.
37
+ #
38
+ # reference/gem_corpus/13_pointer_fields.rb round-trips fields through their
39
+ # readers, a bare address, and an FFI::Pointer, loading the ffi package; it is
40
+ # the guard that all of this stayed true.
41
+ class Ptr
42
+ def initialize(address)
43
+ @address = [address, nil].first
44
+ end
45
+
46
+ def address = @address
47
+ def to_i = @address
48
+
49
+ # byte-offset pointer arithmetic, the gem's shape. (@address is a box, so
50
+ # arithmetic goes through polymorphic dispatch; copying to a local first is
51
+ # just where the operand shape is explicit.)
52
+ def +(offset)
53
+ addr = @address
54
+ addr + offset
55
+ end
56
+
57
+ # the gem's scalar-array write: byte offset, then each element
58
+ def put_array_of_float32(offset, values)
59
+ addr = @address
60
+ i = 0
61
+ while i < values.length
62
+ Raylib.rl_PtrWriteFloat(addr, offset + i * 4, values[i].to_f)
63
+ i += 1
64
+ end
65
+ end
66
+
67
+ # 16-bit PCM, the same shape (byte offset, then each element)
68
+ def put_array_of_int16(offset, values)
69
+ addr = @address
70
+ i = 0
71
+ while i < values.length
72
+ Raylib.rl_PtrWriteInt16(addr, offset + i * 2, values[i].to_i)
73
+ i += 1
74
+ end
75
+ end
76
+ end
77
+
78
+ # The gem's INLINE float arrays (VrDeviceInfo#lensDistortionValues,
79
+ # VrStereoConfig#scale, ...) element-index as value arrays:
80
+ # `device.lensDistortionValues[1] = 0.22` reads/writes one float32. That is
81
+ # the only spelling MRI accepts there -- FFI::Struct#[]= on an array member
82
+ # raises NotImplementedError ("cannot set array field"), measured on
83
+ # raylib-bindings 0.8.4 -- so a scene that must run on both engines indexes
84
+ # elements. Ptr deliberately does NOT grow these methods: it also wraps
85
+ # byte-pointer fields (Image#data), where float32 elements would corrupt
86
+ # silently. Known gap: MRI's InlineArray also answers #to_ptr/#to_a/#each
87
+ # (length-aware); ours is element access only.
88
+ #
89
+ # Same one-ivar boxed-address layout as Ptr (see the note there): the address
90
+ # must stay an INT-boxed first ivar so rl_p_unbox can read it in place.
91
+ class FloatArrayPtr
92
+ def initialize(address)
93
+ @address = [address, nil].first
94
+ end
95
+
96
+ def address = @address
97
+ def to_i = @address
98
+
99
+ def [](index)
100
+ addr = @address
101
+ Raylib.rl_PtrReadFloat(addr, index.to_i * 4)
102
+ end
103
+
104
+ def []=(index, value)
105
+ addr = @address
106
+ f = value.to_f
107
+ Raylib.rl_PtrWriteFloat(addr, index.to_i * 4, f)
108
+ value
109
+ end
110
+ end
111
+
112
+ # raylib's inline int arrays (Shader#locs) index by byte offset like the
113
+ # gem's FFI::Pointer: `(shader.locs + 4 * index).write_int32(loc)`.
114
+ class IntArrayPtr
115
+ def initialize(address)
116
+ @address = [address, nil].first
117
+ end
118
+
119
+ def address = @address
120
+ def to_i = @address
121
+
122
+ def +(offset)
123
+ IntArrayPtr.new(address + offset)
124
+ end
125
+
126
+ def write_int32(value)
127
+ Raylib.rl_PtrWriteInt32(address, 0, value)
128
+ end
129
+
130
+ def read_int32
131
+ Raylib.rl_PtrReadInt32(address, 0)
132
+ end
133
+ end
134
+
135
+ class Image
136
+ def data = Ptr.new(Raylib.rl_m_Image_data(self))
137
+ end
138
+
139
+ class Mesh
140
+ def vertices = Ptr.new(Raylib.rl_m_Mesh_vertices(self))
141
+ def texcoords = Ptr.new(Raylib.rl_m_Mesh_texcoords(self))
142
+ def texcoords2 = Ptr.new(Raylib.rl_m_Mesh_texcoords2(self))
143
+ def normals = Ptr.new(Raylib.rl_m_Mesh_normals(self))
144
+ def tangents = Ptr.new(Raylib.rl_m_Mesh_tangents(self))
145
+ def colors = Ptr.new(Raylib.rl_m_Mesh_colors(self))
146
+ def indices = Ptr.new(Raylib.rl_m_Mesh_indices(self))
147
+ def animVertices = Ptr.new(Raylib.rl_m_Mesh_animVertices(self))
148
+ def animNormals = Ptr.new(Raylib.rl_m_Mesh_animNormals(self))
149
+ def boneIndices = Ptr.new(Raylib.rl_m_Mesh_boneIndices(self))
150
+ def boneWeights = Ptr.new(Raylib.rl_m_Mesh_boneWeights(self))
151
+ def vboId = Ptr.new(Raylib.rl_m_Mesh_vboId(self))
152
+ end
153
+ end