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
//! Contains all batch drawing code. See the crate level documentation.

use super::*;
use core::marker::PhantomData;
pub struct BatchCircle<T, F> {
    buffer: vbo::GrowableBuffer<T>,
    func: F,
    _p: PhantomData<T>,
    _ns: NotSend,
}

impl<T, F: Fn(&T) -> &[f32; 2]> BatchCircle<T, F> {
    pub(crate) fn new(bots: &[T], func: F) -> BatchCircle<T, F> {
        let mut b = BatchCircle {
            buffer: vbo::GrowableBuffer::new(),
            func,
            _p: PhantomData,
            _ns: ns(),
        };
        b.buffer.send_to_gpu(bots);
        b
    }

    pub fn send_and_uniforms<'a>(
        &'a mut self,
        sys: &'a mut SimpleCanvas,
        bots: &[T],
        radius: f32,
    ) -> Uniforms<'a> {
        self.buffer.send_to_gpu(bots);

        let stride = if bots.len() < 2 {
            0i32
        } else {
            let first = (self.func)(&bots[0]);
            let second = (self.func)(&bots[1]);

            let a = first as *const _ as usize;
            let b = second as *const _ as usize;

            assert!(b > a);

            let tsize = core::mem::size_of::<T>();
            let diff = b - a;
            assert!(diff >= tsize);

            (diff) as i32
        };

        let common = UniformCommon {
            color: sys.color,
            offset: vec2same(0.0),
        };
        let un = ProgramUniformValues {
            mode: gl::POINTS,
            radius,
            stride,
            texture: None,
        };

        Uniforms {
            sys,
            common,
            un: UniformVals::Circle(un),
            buffer: self.buffer.get_info(bots.len()),
        }
    }
}