Class: SFML::ConvexShape

Inherits:
Object
  • Object
show all
Includes:
Graphics::ShapeInspectable, 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::ShapeInspectable

#dup, #geometric_center, #global_bounds, #inverse_transform, #local_bounds, #point, #set_texture, #texture, #texture=, #texture_rect, #texture_rect=, #transform

Methods included from Graphics::Transformable

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

Constructor Details

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

Build a ConvexShape. points: may be passed up-front (Array of [x, y] / Vector2) or omitted and set later via #points=.

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sfml/graphics/convex_shape.rb', line 21

def initialize(points: nil, **opts)
  ptr = C::Graphics.sfConvexShape_create
  raise GraphicsError, "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.texture           = opts[:texture]           if opts.key?(:texture)
  self.texture_rect      = opts[:texture_rect]      if opts.key?(:texture_rect)
  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

#handle ⇒ Object (readonly)

:nodoc:



93
94
95
# File 'lib/sfml/graphics/convex_shape.rb', line 93

def handle
  @handle
end

Instance Method Details

#draw_on(target, states_ptr = nil) ⇒ Object

Returns the draw on.



89
90
91
# File 'lib/sfml/graphics/convex_shape.rb', line 89

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

#fill_color ⇒ Object

Interior fill color.



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

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

#fill_color=(c) ⇒ Object

Set the fill color.



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

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

#outline_color ⇒ Object

Outline color (only visible when #outline_thickness > 0).



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

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

#outline_color=(c) ⇒ Object

Set the outline color.



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

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

#outline_thickness ⇒ Object

Outline thickness in pixels — negative draws inward.



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

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

#outline_thickness=(t) ⇒ Object

Set the outline thickness.



84
85
86
# File 'lib/sfml/graphics/convex_shape.rb', line 84

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

#point_count ⇒ Object

Number of polygon vertices currently set.



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

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

#points ⇒ Object

Returns the polygon vertices as an Array of Vector2.



42
43
44
45
46
47
# File 'lib/sfml/graphics/convex_shape.rb', line 42

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.



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

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.



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

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