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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::{
    mesh::{IndexedMesh, MappedIndexedMesh},
    Context,
};

pub struct QuadIndex(usize);

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Quad<T> {
    pub vertices: [T; 4],
}

impl<T> Quad<T>
where
    T: Copy,
{
    pub fn map<N, F>(&self, f: F) -> Quad<N>
    where
        F: Fn(T) -> N,
    {
        Quad {
            vertices: [
                f(self.vertices[0]),
                f(self.vertices[1]),
                f(self.vertices[2]),
                f(self.vertices[3]),
            ],
        }
    }
    pub fn zip<N>(&self, other: Quad<N>) -> Quad<(T, N)>
    where
        N: Copy,
    {
        Quad {
            vertices: [
                (self.vertices[0], other.vertices[0]),
                (self.vertices[1], other.vertices[1]),
                (self.vertices[2], other.vertices[2]),
                (self.vertices[3], other.vertices[3]),
            ],
        }
    }
}

impl<A> From<super::viewport::Viewport<A>> for Quad<(A, A)>
where
    A: Copy + std::ops::Add<Output = A>,
{
    fn from(viewport: super::viewport::Viewport<A>) -> Self {
        let ((x, y), (width, height)) = (viewport.position(), viewport.dimensions());
        let top_left = (x, y);
        let bottom_left = (x, y + height);
        let top_right = (x + width, y);
        let bottom_right = (x + width, y + height);
        Quad {
            vertices: [top_left, bottom_left, top_right, bottom_right],
        }
    }
}

#[derive(Debug)]
pub struct QuadBatch<T> {
    mesh: MappedIndexedMesh<T, u16>,
    count: usize,
    capacity: usize,
}

impl<T> QuadBatch<T>
where
    T: super::vertex::Vertex + Default + Clone,
{
    pub fn new(gl: &mut Context, capacity: usize) -> Result<Self, super::GraphicsError> {
        let vertex_capacity = capacity * 4;
        let index_capacity = capacity * 6;

        let indices = {
            // 0---2
            // | / |
            // 1---3
            let mut indices: Vec<u16> = Vec::with_capacity(index_capacity);
            for i in 0..capacity {
                let vi = (i * 4) as u16;
                indices.push(vi);
                indices.push(vi + 1);
                indices.push(vi + 2);
                indices.push(vi + 2);
                indices.push(vi + 1);
                indices.push(vi + 3);
            }
            indices
        };

        let mut mesh =
            MappedIndexedMesh::with_data(gl, vec![T::default(); vertex_capacity], indices)?;
        mesh.set_draw_range(Some(0..0));

        Ok(Self {
            mesh,
            count: 0,
            capacity,
        })
    }

    pub fn count(&self) -> usize {
        self.count
    }

    pub fn push(&mut self, quad: Quad<T>) -> QuadIndex {
        assert!(
            self.count < self.capacity,
            "Adding too many quads to QuadBatch"
        );
        let index = QuadIndex(self.count);
        self.mesh.set_vertices(&quad.vertices, self.count * 4);
        self.count += 1;
        self.mesh.set_draw_range(Some(0..(self.count * 6)));
        index
    }

    pub fn get_quad(&self, index: QuadIndex) -> Option<Quad<T>>
    where
        T: std::marker::Copy,
    {
        let index = index.0;
        if index >= self.count {
            None
        } else {
            let index = index * 4;
            let mut vertices = [T::default(); 4];
            for (dst, src) in vertices
                .iter_mut()
                .zip(self.mesh.get_vertices()[index..index + 4].iter())
            {
                *dst = *src;
            }
            Some(Quad { vertices })
        }
    }

    pub fn insert(&mut self, index: QuadIndex, quad: Quad<T>) {
        self.mesh.set_vertices(&quad.vertices, index.0 * 4);
    }

    pub fn clear(&mut self) {
        self.count = 0;
        self.mesh.set_draw_range(Some(0..0));
    }

    pub fn unmap(&mut self, ctx: &mut Context) -> super::Geometry<&IndexedMesh<T, u16>> {
        let draw_range = self.mesh.draw_range();
        let mesh = self.mesh.unmap(ctx);
        super::Geometry {
            mesh,
            draw_range,
            draw_mode: super::DrawMode::Triangles,
            instance_count: 1,
        }
    }
}