Module: SFML::Graphics::ShapeInspectable
- Included in:
- CircleShape, ConvexShape, RectangleShape, Shape
- Defined in:
- lib/sfml/graphics/shape_inspectable.rb
Overview
Methods shared across CircleShape / RectangleShape / ConvexShape: texture binding, geometry introspection, transform readout, dup. Including class must define CSFML_PREFIX (Symbol) and hold the FFI handle in @handle (same contract as Graphics::Transformable).
Texture binding follows the Sprite pattern: keep a Ruby reference to the bound texture in @texture so the GC doesn't collect the GPU resource while the shape still draws with it.
Instance Method Summary collapse
-
#dup ⇒ Object
(also: #clone)
Deep copy.
-
#geometric_center ⇒ Object
Centroid of the polygon's vertex list (CSFML 3.0+).
-
#global_bounds ⇒ Object
AABB after the shape's full transform — the world-space rect the renderer uses for culling and hit-tests.
-
#inverse_transform ⇒ Object
Returns the inverse transform.
-
#local_bounds ⇒ Object
AABB before transform — the rectangle the geometry would occupy if the shape were drawn at the origin with no rotation/scale.
-
#point(index) ⇒ Object
Single polygon vertex by index.
-
#set_texture(tex, reset_rect: false) ⇒ Object
reset_rect: truesnaps the texture-rect to the full texture size, which is usually what you want when binding a fresh texture. -
#texture ⇒ Object
Returns the texture.
-
#texture=(tex) ⇒ Object
Set the texture.
-
#texture_rect ⇒ Object
Returns the texture rect.
-
#texture_rect=(rect) ⇒ Object
Set the texture rect.
-
#transform ⇒ Object
Returns the transform.
Instance Method Details
#dup ⇒ Object Also known as: clone
Deep copy. Texture binding is shared (textures are GPU objects, cheap to alias); transform/colour state is independent.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 85 def dup ptr = _csfml(:copy, @handle) raise GraphicsError, "#{self.class.name}#dup returned NULL" if ptr.null? destroy_fn = C::Graphics.method(:"#{self.class::CSFML_PREFIX}_destroy") copy = self.class.allocate copy.instance_variable_set(:@handle, FFI::AutoPointer.new(ptr, destroy_fn)) copy.instance_variable_set(:@texture, @texture) if defined?(@texture) copy end |
#geometric_center ⇒ Object
Centroid of the polygon's vertex list (CSFML 3.0+).
57 58 59 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 57 def geometric_center Vector2.from_native(_csfml(:getGeometricCenter, @handle)) end |
#global_bounds ⇒ Object
AABB after the shape's full transform — the world-space rect the renderer uses for culling and hit-tests.
69 70 71 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 69 def global_bounds Rect.from_native(_csfml(:getGlobalBounds, @handle)) end |
#inverse_transform ⇒ Object
Returns the inverse transform.
79 80 81 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 79 def inverse_transform _csfml(:getInverseTransform, @handle) end |
#local_bounds ⇒ Object
AABB before transform — the rectangle the geometry would occupy if the shape were drawn at the origin with no rotation/scale.
63 64 65 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 63 def local_bounds Rect.from_native(_csfml(:getLocalBounds, @handle)) end |
#point(index) ⇒ Object
Single polygon vertex by index. The full list is exposed by the
subclass-specific #points method on ConvexShape; CircleShape
synthesises N points from its radius+point_count.
52 53 54 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 52 def point(index) Vector2.from_native(_csfml(:getPoint, @handle, Integer(index))) end |
#set_texture(tex, reset_rect: false) ⇒ Object
reset_rect: true snaps the texture-rect to the full texture size,
which is usually what you want when binding a fresh texture.
21 22 23 24 25 26 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 21 def set_texture(tex, reset_rect: false) raise ArgumentError, "expected SFML::Texture or nil" unless tex.nil? || tex.is_a?(Texture) _csfml(:setTexture, @handle, tex ? tex.handle : nil, reset_rect) @texture = tex self end |
#texture ⇒ Object
Returns the texture.
13 14 15 16 17 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 13 def texture ptr = _csfml(:getTexture, @handle) return nil if ptr.null? Texture.send(:_borrow, ptr) end |
#texture=(tex) ⇒ Object
Set the texture.
29 30 31 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 29 def texture=(tex) set_texture(tex, reset_rect: false) end |
#texture_rect ⇒ Object
Returns the texture rect.
34 35 36 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 34 def texture_rect Rect.from_native(_csfml(:getTextureRect, @handle)) end |
#texture_rect=(rect) ⇒ Object
Set the texture rect.
39 40 41 42 43 44 45 46 47 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 39 def texture_rect=(rect) raise ArgumentError, "texture_rect= requires a SFML::Rect" unless rect.is_a?(Rect) native = C::Graphics::IntRect.new native[:position][:x] = Integer(rect.x) native[:position][:y] = Integer(rect.y) native[:size][:x] = Integer(rect.width) native[:size][:y] = Integer(rect.height) _csfml(:setTextureRect, @handle, native) end |
#transform ⇒ Object
Returns the transform.
74 75 76 |
# File 'lib/sfml/graphics/shape_inspectable.rb', line 74 def transform _csfml(:getTransform, @handle) end |