Class: SFML::VertexArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sfml/graphics/vertex_array.rb

Overview

Batched geometry: a single CSFML draw call shipping many vertices to the GPU at once. This is how you draw thousands of particles, custom meshes, or tile maps without the per-shape overhead.

va = SFML::VertexArray.new(:triangles)
va << SFML::Vertex.new([100, 100], color: SFML::Color.red)
va << SFML::Vertex.new([200, 100], color: SFML::Color.green)
va << SFML::Vertex.new([150, 200], color: SFML::Color.blue)
window.draw(va)

Primitive types control how vertices form geometry:

:points          one isolated point per vertex
:lines           pairs of vertices form line segments
:line_strip      consecutive vertices form a chain of segments
:triangles       triples form independent triangles
:triangle_strip  each new vertex extends the strip
:triangle_fan    every vertex shares a fan-out with vertex 0

Constant Summary collapse

PRIMITIVE_TYPES =

Order matches sfPrimitiveType in CSFML/Graphics/PrimitiveType.h.

%i[points lines line_strip triangles triangle_strip triangle_fan].freeze
PRIMITIVE_INDEX =
PRIMITIVE_TYPES.each_with_index.to_h.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primitive_type = :points, vertices = nil) ⇒ VertexArray

Returns a new instance of VertexArray.

Raises:



26
27
28
29
30
31
32
33
# File 'lib/sfml/graphics/vertex_array.rb', line 26

def initialize(primitive_type = :points, vertices = nil)
  ptr = C::Graphics.sfVertexArray_create
  raise Error, "sfVertexArray_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfVertexArray_destroy))

  self.primitive_type = primitive_type
  vertices&.each { |v| append(v) }
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



112
113
114
# File 'lib/sfml/graphics/vertex_array.rb', line 112

def handle
  @handle
end

Instance Method Details

#[](index) ⇒ Object

Read a vertex by index, or nil if out of range. Returns a fresh SFML::Vertex copy — mutate via ‘va = new_vertex` to write back, not through the returned object.



72
73
74
75
76
77
# File 'lib/sfml/graphics/vertex_array.rb', line 72

def [](index)
  i = Integer(index)
  return nil if i < 0 || i >= size
  ptr = C::Graphics.sfVertexArray_getVertex(@handle, i)
  Vertex.from_native(C::Graphics::Vertex.new(ptr))
end

#[]=(index, vertex) ⇒ Object

Raises:

  • (IndexError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sfml/graphics/vertex_array.rb', line 79

def []=(index, vertex)
  i = Integer(index)
  # CSFML's sfVertexArray_getVertex aborts the process on out-of-range
  # access (it asserts), so we have to bounds-check on the Ruby side.
  raise IndexError, "vertex index #{i} out of range (size: #{size})" if i < 0 || i >= size

  ptr = C::Graphics.sfVertexArray_getVertex(@handle, i)
  cv = C::Graphics::Vertex.new(ptr)
  cv[:position][:x]   = vertex.position.x.to_f
  cv[:position][:y]   = vertex.position.y.to_f
  cv[:color][:r]      = vertex.color.r
  cv[:color][:g]      = vertex.color.g
  cv[:color][:b]      = vertex.color.b
  cv[:color][:a]      = vertex.color.a
  cv[:tex_coords][:x] = vertex.tex_coords.x.to_f
  cv[:tex_coords][:y] = vertex.tex_coords.y.to_f
  vertex
end

#append(vertex) ⇒ Object Also known as: <<



63
64
65
66
# File 'lib/sfml/graphics/vertex_array.rb', line 63

def append(vertex)
  C::Graphics.sfVertexArray_append(@handle, vertex.to_native)
  self
end

#boundsObject



104
105
106
# File 'lib/sfml/graphics/vertex_array.rb', line 104

def bounds
  Rect.from_native(C::Graphics.sfVertexArray_getBounds(@handle))
end

#clearObject



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

def clear
  C::Graphics.sfVertexArray_clear(@handle)
  self
end

#draw_on(target, states_ptr = nil) ⇒ Object

:nodoc:



108
109
110
# File 'lib/sfml/graphics/vertex_array.rb', line 108

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

#eachObject



98
99
100
101
102
# File 'lib/sfml/graphics/vertex_array.rb', line 98

def each
  return enum_for(:each) unless block_given?
  size.times { |i| yield self[i] }
  self
end

#empty?Boolean

Returns:

  • (Boolean)


51
# File 'lib/sfml/graphics/vertex_array.rb', line 51

def empty? = size.zero?

#primitive_typeObject



35
36
37
# File 'lib/sfml/graphics/vertex_array.rb', line 35

def primitive_type
  PRIMITIVE_TYPES[C::Graphics.sfVertexArray_getPrimitiveType(@handle)] || :unknown
end

#primitive_type=(type) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sfml/graphics/vertex_array.rb', line 39

def primitive_type=(type)
  code = PRIMITIVE_INDEX.fetch(type) do
    raise ArgumentError,
          "Unknown primitive type: #{type.inspect}. Expected one of: #{PRIMITIVE_TYPES.inspect}"
  end
  C::Graphics.sfVertexArray_setPrimitiveType(@handle, code)
end

#resize(n) ⇒ Object



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

def resize(n)
  C::Graphics.sfVertexArray_resize(@handle, Integer(n))
  self
end

#sizeObject Also known as: length, count



47
# File 'lib/sfml/graphics/vertex_array.rb', line 47

def size = C::Graphics.sfVertexArray_getVertexCount(@handle)