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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use nalgebra::{Affine2, Similarity2};

/// A sprite is a textured quad that can be rendered with a SpriteLayer.
///
/// Sprites can be textured using texture indices provided by the currently
/// bound texture atlas. Sprites can be translated, rotated, scaled, and skewed
/// to achieve the desired shape.
#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Sprite {
    transform: [f32; 6],
    uv_pos: [f32; 2],
    tint: [f32; 4],
    uv_size: [f32; 2],
    texture: i32,
    sampler: u32,
}

impl Default for Sprite {
    fn default() -> Self {
        Self {
            transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
            uv_pos: [0.0, 0.0],
            uv_size: [1.0, 1.0],
            tint: [1.0, 1.0, 1.0, 1.0],
            texture: -1,
            sampler: 0,
        }
    }
}

impl Sprite {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn with_tint(mut self, r: f32, g: f32, b: f32, a: f32) -> Self {
        self.tint = [r, g, b, a];
        self
    }

    pub fn with_uv(mut self, uv_pos: [f32; 2], uv_size: [f32; 2]) -> Self {
        self.uv_pos = uv_pos;
        self.uv_size = uv_size;
        self
    }

    pub fn with_sampler(mut self, sampler: u32) -> Self {
        self.sampler = sampler;
        self
    }

    pub fn with_texture(mut self, texture: i32) -> Self {
        self.texture = texture;
        self
    }

    pub fn with_similarity(self, similarity: &Similarity2<f32>) -> Self {
        self.with_transform(&nalgebra::convert(*similarity))
    }

    pub fn with_transform(mut self, transform: &Affine2<f32>) -> Self {
        self.transform = [
            transform[(0, 0)],
            transform[(1, 0)],
            transform[(0, 1)],
            transform[(1, 1)],
            transform[(0, 2)],
            transform[(1, 2)],
        ];
        self
    }
}