Thanks for the answer !
You can do something like this:
vertex = SFML::Vertex.new
# do changes
array[0] = vertex
# do changes
array[1] = vertex
But in this example, array is a "ruby array" ? Because I don't know how to do that with a VertexArray.
And I really can't use a ruby array in my project :
I draw a tilemap with 8x8 tiles on a window of 640x480, so 4800 quads.
This code with a VertexArray :
require 'sfml'
include SFML
class MainWindow
QUAD_SIZE = 8
def initialize(width, height)
@window = RenderWindow.new([width, height], "VertexArray")
@window.framerate_limit = 60
@vertices = VertexArray.new(Quads, 0)
for x in 0...@window.get_size.x/QUAD_SIZE
for y in 0...@window.get_size.y/QUAD_SIZE
color = [Color::Red, Color::Green, Color::Blue].sample
@vertices.append(Vertex.new([x*QUAD_SIZE, y*QUAD_SIZE], color))
@vertices.append(Vertex.new([(x+1)*QUAD_SIZE, y*QUAD_SIZE], color))
@vertices.append(Vertex.new([(x+1)*QUAD_SIZE, (y+1)*QUAD_SIZE], color))
@vertices.append(Vertex.new([x*QUAD_SIZE, (y+1)*QUAD_SIZE], color))
end
end
update
end
def update
while @window.open?
@window.each_event do |event|
@window.close if event.type == Event::Closed
end
@window.clear
@window.draw(@vertices)
@window.display
end
end
end
MainWindow.new(640, 480)
Run smoothly with stable 60FPS
But the same code with a ruby array instead of a VertexArray :
require 'sfml'
include SFML
class MainWindow
QUAD_SIZE = 8
def initialize(width, height)
@window = RenderWindow.new([width, height], "VertexArray")
@window.framerate_limit = 60
@vertices = Array.new
for x in 0...@window.get_size.x/QUAD_SIZE
for y in 0...@window.get_size.y/QUAD_SIZE
color = [Color::Red, Color::Green, Color::Blue].sample
@vertices.push(Vertex.new([x*QUAD_SIZE, y*QUAD_SIZE], color))
@vertices.push(Vertex.new([(x+1)*QUAD_SIZE, y*QUAD_SIZE], color))
@vertices.push(Vertex.new([(x+1)*QUAD_SIZE, (y+1)*QUAD_SIZE], color))
@vertices.push(Vertex.new([x*QUAD_SIZE, (y+1)*QUAD_SIZE], color))
end
end
update
end
def update
while @window.open?
@window.each_event do |event|
@window.close if event.type == Event::Closed
end
@window.clear
@window.draw(@vertices, Quads, RenderStates::Default)
@window.display
end
end
end
MainWindow.new(640, 480)
Give me around 20FPS.
+ I really need a way to modify a particular Vertex :
I have animated tiles, and it's pretty slow to clear the VertexArray, loop through all the 4800tiles, re-create each Vertex just to change the texture coordinates of one tile :/