Class: SFML::ConvexShape

Inherits:
Object
  • Object
show all
Includes:
Graphics::Transformable
Defined in:
lib/sfml/graphics/convex_shape.rb

Overview

An arbitrary convex polygon. Define its outline by listing points in order (clockwise or counter-clockwise — just stay consistent). The shape must remain convex — drawing a non-convex point set produces visual artifacts (CSFML doesn’t validate).

star = SFML::ConvexShape.new(
  points: [[60, 0], [80, 40], [120, 50], [90, 80],
           [100, 120], [60, 95], [20, 120], [30, 80],
           [0, 50], [40, 40]],
  fill_color: SFML::Color.yellow,
)
window.draw(star)

Constant Summary collapse

CSFML_PREFIX =
:sfConvexShape

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graphics::Transformable

#move, #origin, #origin=, #position, #position=, #rotate, #rotation, #rotation=, #scale, #scale=, #scale_by

Constructor Details

#initialize(points: nil, **opts) ⇒ ConvexShape

Returns a new instance of ConvexShape.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sfml/graphics/convex_shape.rb', line 18

def initialize(points: nil, **opts)
  ptr = C::Graphics.sfConvexShape_create
  raise Error, "sfConvexShape_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfConvexShape_destroy))

  self.points = points if points
  self.fill_color        = opts[:fill_color]        if opts.key?(:fill_color)
  self.outline_color     = opts[:outline_color]     if opts.key?(:outline_color)
  self.outline_thickness = opts[:outline_thickness] if opts.key?(:outline_thickness)
  self.position          = opts[:position]          if opts.key?(:position)
  self.origin            = opts[:origin]            if opts.key?(:origin)
  self.rotation          = opts[:rotation]          if opts.key?(:rotation)
  self.scale             = opts[:scale]             if opts.key?(:scale)
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



80
81
82
# File 'lib/sfml/graphics/convex_shape.rb', line 80

def handle
  @handle
end

Instance Method Details

#draw_on(target, states_ptr = nil) ⇒ Object

:nodoc:



76
77
78
# File 'lib/sfml/graphics/convex_shape.rb', line 76

def draw_on(target, states_ptr = nil) # :nodoc:
  target._draw_native(:ConvexShape, @handle, states_ptr)
end

#fill_colorObject



58
# File 'lib/sfml/graphics/convex_shape.rb', line 58

def fill_color = Color.from_native(C::Graphics.sfConvexShape_getFillColor(@handle))

#fill_color=(c) ⇒ Object



60
61
62
# File 'lib/sfml/graphics/convex_shape.rb', line 60

def fill_color=(c)
  C::Graphics.sfConvexShape_setFillColor(@handle, c.to_native)
end

#outline_colorObject



64
# File 'lib/sfml/graphics/convex_shape.rb', line 64

def outline_color = Color.from_native(C::Graphics.sfConvexShape_getOutlineColor(@handle))

#outline_color=(c) ⇒ Object



66
67
68
# File 'lib/sfml/graphics/convex_shape.rb', line 66

def outline_color=(c)
  C::Graphics.sfConvexShape_setOutlineColor(@handle, c.to_native)
end

#outline_thicknessObject



70
# File 'lib/sfml/graphics/convex_shape.rb', line 70

def outline_thickness = C::Graphics.sfConvexShape_getOutlineThickness(@handle)

#outline_thickness=(t) ⇒ Object



72
73
74
# File 'lib/sfml/graphics/convex_shape.rb', line 72

def outline_thickness=(t)
  C::Graphics.sfConvexShape_setOutlineThickness(@handle, t.to_f)
end

#point_countObject



33
# File 'lib/sfml/graphics/convex_shape.rb', line 33

def point_count = C::Graphics.sfConvexShape_getPointCount(@handle)

#pointsObject

Returns the polygon vertices as an Array of Vector2.



36
37
38
39
40
41
# File 'lib/sfml/graphics/convex_shape.rb', line 36

def points
  n = point_count
  (0...n).map do |i|
    Vector2.from_native(C::Graphics.sfConvexShape_getPoint(@handle, i))
  end
end

#points=(list) ⇒ Object

Replace every point. Accepts a list of [x, y] arrays or Vector2s.



44
45
46
47
48
49
50
# File 'lib/sfml/graphics/convex_shape.rb', line 44

def points=(list)
  C::Graphics.sfConvexShape_setPointCount(@handle, list.length)
  list.each_with_index do |pt, i|
    vec = pt.is_a?(Vector2) ? pt : Vector2.new(*pt)
    C::Graphics.sfConvexShape_setPoint(@handle, i, vec.to_native_f)
  end
end

#set_point(index, point) ⇒ Object

Mutate a single vertex by index.



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

def set_point(index, point)
  vec = point.is_a?(Vector2) ? point : Vector2.new(*point)
  C::Graphics.sfConvexShape_setPoint(@handle, Integer(index), vec.to_native_f)
end