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

Instance Method Details

#dupObject Also known as: clone

Deep copy. Texture binding is shared (textures are GPU objects, cheap to alias); transform/colour state is independent.

Raises:



79
80
81
82
83
84
85
86
87
88
# File 'lib/sfml/graphics/shape_inspectable.rb', line 79

def dup
  ptr = _csfml(:copy, @handle)
  raise Error, "#{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_centerObject

Centroid of the polygon’s vertex list (CSFML 3.0+).



53
54
55
# File 'lib/sfml/graphics/shape_inspectable.rb', line 53

def geometric_center
  Vector2.from_native(_csfml(:getGeometricCenter, @handle))
end

#global_boundsObject

AABB after the shape’s full transform — the world-space rect the renderer uses for culling and hit-tests.



65
66
67
# File 'lib/sfml/graphics/shape_inspectable.rb', line 65

def global_bounds
  Rect.from_native(_csfml(:getGlobalBounds, @handle))
end

#inverse_transformObject



73
74
75
# File 'lib/sfml/graphics/shape_inspectable.rb', line 73

def inverse_transform
  _csfml(:getInverseTransform, @handle)
end

#local_boundsObject

AABB before transform — the rectangle the geometry would occupy if the shape were drawn at the origin with no rotation/scale.



59
60
61
# File 'lib/sfml/graphics/shape_inspectable.rb', line 59

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.



48
49
50
# File 'lib/sfml/graphics/shape_inspectable.rb', line 48

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.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/sfml/graphics/shape_inspectable.rb', line 20

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

#textureObject



12
13
14
15
16
# File 'lib/sfml/graphics/shape_inspectable.rb', line 12

def texture
  ptr = _csfml(:getTexture, @handle)
  return nil if ptr.null?
  Texture.send(:_borrow, ptr)
end

#texture=(tex) ⇒ Object



27
28
29
# File 'lib/sfml/graphics/shape_inspectable.rb', line 27

def texture=(tex)
  set_texture(tex, reset_rect: false)
end

#texture_rectObject



31
32
33
# File 'lib/sfml/graphics/shape_inspectable.rb', line 31

def texture_rect
  Rect.from_native(_csfml(:getTextureRect, @handle))
end

#texture_rect=(rect) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
# File 'lib/sfml/graphics/shape_inspectable.rb', line 35

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

#transformObject



69
70
71
# File 'lib/sfml/graphics/shape_inspectable.rb', line 69

def transform
  _csfml(:getTransform, @handle)
end