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
use crate::color::RGBA8;
use crate::graphics::{TextureSection, VertexAttribute, VertexDescriptor, VertexInputType, VertexOutputType};
use crate::math::AABB2D;
use cgmath::*;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Sprite {
pub pos: Vector3<f32>,
pub size: Vector2<u16>,
pub texture: TextureSection,
pub color: RGBA8,
pub rotation: u16,
}
impl VertexDescriptor for Sprite {
const ATTRIBUTES: &'static [VertexAttribute] = &[
VertexAttribute::new(3, VertexInputType::F32, VertexOutputType::F32),
VertexAttribute::new(2, VertexInputType::U16, VertexOutputType::F32),
VertexAttribute::new(4, VertexInputType::U16, VertexOutputType::NormalizedF32),
VertexAttribute::new(4, VertexInputType::U8, VertexOutputType::NormalizedF32),
VertexAttribute::new(1, VertexInputType::U16, VertexOutputType::NormalizedF32),
];
}
impl Default for Sprite {
fn default() -> Sprite {
Sprite {
pos: Vector3::new(0.0, 0.0, 0.0),
size: Vector2::new(100, 100),
texture: TextureSection::default(),
color: RGBA8::WHITE,
rotation: 0,
}
}
}
impl Sprite {
pub fn new(
pos: Vector3<f32>,
size: Vector2<f32>,
texture: TextureSection,
color: RGBA8,
rotation: f32,
) -> Sprite {
Sprite {
pos,
size: {
let x = (size.x as u32) & 0xFFFF;
let y = (size.y as u32) & 0xFFFF;
Vector2::new(x as u16, y as u16)
},
texture,
color,
rotation: (rotation.fract() * 65536.0) as u16,
}
}
pub fn new_raw(
pos: Vector3<f32>,
size: Vector2<u16>,
texture: TextureSection,
color: RGBA8,
rotation: u16,
) -> Sprite {
Sprite {
pos,
size,
texture,
color,
rotation,
}
}
}
impl From<Sprite> for AABB2D {
fn from(sprite: Sprite) -> Self {
AABB2D::from_pos_size(sprite.pos.truncate(), sprite.size.cast().unwrap())
}
}