sfml_xt/graphics/
vertex_vec_ext.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use sfml::{
    graphics::{Color, FloatRect, IntRect, Vertex},
    system::Vector2,
};

/// Convenience methods for a `Vec` of vertices (vertex array).
pub trait VertexVecExt {
    /// Push a quad to a vector of quad primitives
    fn push_quad(&mut self, dst_rect: FloatRect, texture_rect: IntRect, color: Color);
}

impl VertexVecExt for Vec<Vertex> {
    fn push_quad(&mut self, dst_rect: FloatRect, texture_rect: IntRect, color: Color) {
        self.extend_from_slice(&[
            Vertex {
                position: Vector2::new(dst_rect.left, dst_rect.top),
                color,
                tex_coords: Vector2::new(texture_rect.left as f32, texture_rect.top as f32),
            },
            Vertex {
                position: Vector2::new(dst_rect.left + dst_rect.width, dst_rect.top),
                color,
                tex_coords: Vector2::new(
                    (texture_rect.left + texture_rect.width) as f32,
                    texture_rect.top as f32,
                ),
            },
            Vertex {
                position: Vector2::new(
                    dst_rect.left + dst_rect.width,
                    dst_rect.top + dst_rect.height,
                ),
                color,
                tex_coords: Vector2::new(
                    (texture_rect.left + texture_rect.width) as f32,
                    (texture_rect.top + texture_rect.height) as f32,
                ),
            },
            Vertex {
                position: Vector2::new(dst_rect.left, dst_rect.top + dst_rect.height),
                color,
                tex_coords: Vector2::new(
                    texture_rect.left as f32,
                    (texture_rect.top + texture_rect.height) as f32,
                ),
            },
        ]);
    }
}