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
use crate::graphics::{IntoQuad, Point, Quad, Rectangle};
/// A quad describing the portion of a resource in absolute coordinates.
///
/// Unlike a [`Quad`], the `source` coordinates of a [`Sprite`] are absolute. It
/// can be used as a convenient alternative.
///
/// [`Quad`]: struct.Quad.html
/// [`Sprite`]: struct.Sprite.html
#[derive(Debug, PartialEq, Clone)]
pub struct Sprite {
/// The portion of a resource that contains the sprite, in absolute
/// coordinates.
pub source: Rectangle<u16>,
/// The position where the sprite should be drawn.
pub position: Point,
/// The scale to apply to the sprite.
pub scale: (f32, f32),
}
impl Default for Sprite {
#[inline]
fn default() -> Sprite {
Sprite {
source: Rectangle {
x: 0,
y: 0,
width: 1,
height: 1,
},
position: Point::new(0.0, 0.0),
scale: (1.0, 1.0),
}
}
}
impl IntoQuad for Sprite {
fn into_quad(self, x_unit: f32, y_unit: f32) -> Quad {
Quad {
source: Rectangle {
x: self.source.x as f32 * x_unit,
y: self.source.y as f32 * y_unit,
width: self.source.width as f32 * x_unit,
height: self.source.height as f32 * y_unit,
},
position: self.position,
size: (
self.source.width as f32 * self.scale.0,
self.source.height as f32 * self.scale.1,
),
}
}
}