Skip to content

Commit

Permalink
examples/vertex-buffers: Make the example more dynamic (mouse clicks …
Browse files Browse the repository at this point in the history
…add vertices)
  • Loading branch information
crumblingstatue committed Oct 21, 2024
1 parent 1f2f3ae commit 65da240
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions examples/vertex-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sfml::{
graphics::{
Color, PrimitiveType, RenderTarget, RenderWindow, Vertex, VertexBuffer, VertexBufferUsage,
},
window::{Event, Style},
window::{mouse::Button, Event, Style},
SfResult,
};

Expand All @@ -15,23 +15,37 @@ fn main() -> SfResult<()> {
)?;
window.set_vertical_sync_enabled(true);

let mut vertex_buffer =
VertexBuffer::new(PrimitiveType::LINE_STRIP, 6, VertexBufferUsage::STATIC)?;

let vertices = [
let mut vertices = vec![
Vertex::with_pos_color((20.0, 30.0).into(), Color::GREEN),
Vertex::with_pos_color((30.0, 30.0).into(), Color::GREEN),
Vertex::with_pos_color((40.0, 40.0).into(), Color::GREEN),
Vertex::with_pos_color((50.0, 50.0).into(), Color::GREEN),
Vertex::with_pos_color((60.0, 60.0).into(), Color::GREEN),
Vertex::with_pos_color((50.0, 80.0).into(), Color::GREEN),
];
let mut vertex_buffer = VertexBuffer::new(
PrimitiveType::LINE_STRIP,
vertices.len(),
VertexBufferUsage::DYNAMIC,
)?;
vertex_buffer.update(&vertices, 0)?;

'mainloop: loop {
while let Some(e) = window.poll_event() {
if e == Event::Closed {
break 'mainloop;
match e {
Event::Closed => break 'mainloop,
Event::MouseButtonPressed {
button: Button::Left,
x,
y,
} => {
vertices.push(Vertex::with_pos_color(
(x as f32, y as f32).into(),
Color::GREEN,
));
vertex_buffer.update(&vertices, 0)?;
}
_ => {}
}
}
// Clear the window
Expand Down

0 comments on commit 65da240

Please sign in to comment.