egaku2d_core/
sprite.rs

1use super::*;
2
3//pub use self::sprite_program::Vertex;
4
5pub struct SpriteSave {
6    _ns: NotSend,
7    pub(crate) buffer: vbo::StaticBuffer<sprite_program::Vertex>,
8}
9impl SpriteSave {
10    pub fn uniforms<'a>(
11        &'a self,
12        sys: &'a mut SimpleCanvas,
13        texture: &'a Texture,
14        radius: f32,
15    ) -> Uniforms<'a> {
16        let sqrt2: f32 = 1.41421356237;
17        let radius = radius * sqrt2;
18
19        let common = UniformCommon {
20            color: sys.color,
21            offset: sys.offset,
22        };
23        let un = SpriteProgramUniformValues { radius, texture };
24        Uniforms {
25            sys,
26            common,
27            un: UniformVals::Sprite(un),
28            buffer: self.buffer.get_info(),
29        }
30    }
31}
32
33pub struct SpriteSession {
34    pub(crate) verts: Vec<sprite_program::Vertex>,
35}
36
37impl SpriteSession {
38    pub fn new() -> Self {
39        SpriteSession { verts: Vec::new() }
40    }
41    ///Add a point sprite.
42    #[inline(always)]
43    pub fn add(&mut self, point: PointType, index: u16, rotation: f32) -> &mut Self {
44        let k = rotation.rem_euclid(core::f32::consts::PI * 2.);
45        let k = k / (core::f32::consts::PI * 2.);
46        let k = (k * (core::u16::MAX as f32)) as u16;
47
48        self.verts.push(sprite_program::Vertex {
49            pos: point,
50            index: index as u16,
51            rotation: k,
52        });
53        self
54    }
55
56    pub fn append(&mut self, other: &mut Self) {
57        self.verts.append(&mut other.verts);
58    }
59
60    ///Save this sprite session to into its own static buffer to be drawn later.
61    pub fn save(&mut self, _sys: &mut SimpleCanvas) -> SpriteSave {
62        SpriteSave {
63            _ns: ns(),
64            buffer: vbo::StaticBuffer::new(&self.verts),
65        }
66    }
67
68    pub fn send_and_uniforms<'a>(
69        &'a mut self,
70        sys: &'a mut SimpleCanvas,
71        texture: &'a Texture,
72        radius: f32,
73    ) -> Uniforms<'a> {
74        sys.sprite_buffer.send_to_gpu(&self.verts);
75
76        let sqrt2: f32 = 1.41421356237;
77        let radius = radius * sqrt2;
78
79        let common = UniformCommon {
80            color: sys.color,
81            offset: sys.offset,
82        };
83        let un = SpriteProgramUniformValues { radius, texture };
84
85        let buffer = sys.sprite_buffer.get_info(self.verts.len());
86        Uniforms {
87            common,
88            sys,
89            un: UniformVals::Sprite(un),
90            buffer,
91        }
92    }
93}
94
95#[derive(Debug)]
96pub struct Texture {
97    _ns: NotSend,
98    pub(crate) grid_dim: [u8; 2],
99    pub(crate) dim: [f32; 2],
100    pub(crate) id: GLuint,
101}
102
103impl Texture {
104    pub fn grid_dim(&self) -> [u8; 2] {
105        self.grid_dim
106    }
107    pub fn dim(&self) -> [f32; 2] {
108        self.dim
109    }
110    ///Create a texture index from a coordinate in the tile set.
111    ///The top left time maps to 0,0.
112    ///The x component grows to the right.
113    ///The y component grows downwards.
114    pub fn coord_to_index(&self, cell: [u8; 2]) -> u16 {
115        let cell = [cell[0] as u16, cell[1] as u16];
116
117        self.grid_dim[0] as u16 * cell[1] + cell[0]
118    }
119
120    pub unsafe fn new(textureid: GLuint, grid_dim: [u8; 2], dim: [f32; 2]) -> Texture {
121        Texture {
122            id: textureid,
123            grid_dim,
124            _ns: ns(),
125            dim,
126        }
127    }
128}