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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::*;
use aseprite::*;
use std::time::Duration;
quick_error! {
#[derive(Debug)]
pub enum AnimationError {
NoFrameTagsInMetadata {
description("no frame tags field in metadata")
}
NoMatchingTag {
description("no tag found which is equal to the passed tag")
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum AnimationStatus {
Repeat,
Playing,
Stopped,
}
#[derive(Debug, Copy, Clone)]
pub struct Animation {
frame_start: usize,
frame_end: usize,
frame_current: usize,
duration: f64,
repeat: bool,
}
impl Animation {
pub fn start(frame_start: usize, frame_end: usize, repeat: bool) -> Self {
Animation {
frame_start,
frame_end,
repeat,
duration: 0.0,
frame_current: frame_start,
}
}
pub fn start_from_tag(
buffer: &AnimationBlitBuffer,
tag: String,
repeat: bool,
) -> Result<Self, AnimationError> {
let frame_tags = match buffer.info.meta.frame_tags {
Some(ref t) => t,
None => return Err(AnimationError::NoFrameTagsInMetadata),
};
for frame in frame_tags {
if tag == frame.name {
return Ok(Animation::start(
frame.from as usize,
frame.to as usize,
repeat,
));
}
}
Err(AnimationError::NoMatchingTag)
}
pub fn update(
&mut self,
buffer: &AnimationBlitBuffer,
dt: Duration,
) -> Result<AnimationStatus, Box<dyn Error>> {
if !self.repeat && self.frame_current > self.frame_end {
return Ok(AnimationStatus::Stopped);
}
self.duration += dt.as_secs() as f64 + dt.subsec_nanos() as f64 * 1e-9;
let mut frame = &buffer.info.frames[self.frame_current];
while self.duration > frame.duration as f64 / 1000.0 {
self.duration -= frame.duration as f64 / 1000.0;
self.frame_current += 1;
if self.frame_current > self.frame_end {
break;
}
frame = &buffer.info.frames[self.frame_current];
}
while self.frame_current > self.frame_end {
self.frame_current -= self.frame_end - self.frame_start;
if !self.repeat {
return Ok(AnimationStatus::Stopped);
}
}
if self.repeat {
return Ok(AnimationStatus::Repeat);
}
Ok(AnimationStatus::Playing)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AnimationBlitBuffer {
buffer: BlitBuffer,
info: SpritesheetData,
}
impl AnimationBlitBuffer {
pub fn new(buffer: BlitBuffer, info: SpritesheetData) -> Self {
AnimationBlitBuffer { buffer, info }
}
pub fn blit_frame(
&self,
dst: &mut [u32],
dst_width: usize,
offset: (i32, i32),
frame: usize,
) -> Result<(), Box<dyn Error>> {
let frame = &self.info.frames[frame];
let rect = (
frame.frame.x as i32,
frame.frame.y as i32,
frame.frame.w as i32,
frame.frame.h as i32,
);
self.buffer.blit_rect(dst, dst_width, offset, rect);
Ok(())
}
pub fn blit(
&self,
dst: &mut [u32],
dst_width: usize,
offset: (i32, i32),
info: &Animation,
) -> Result<(), Box<dyn Error>> {
self.blit_frame(dst, dst_width, offset, info.frame_current)
}
pub fn save<P>(&self, path: P) -> Result<(), Box<dyn Error>>
where
P: AsRef<Path>,
{
let mut file = File::create(path)?;
{
let mut writer = BufWriter::new(&mut file);
serialize_into(&mut writer, &self)?;
}
file.sync_all()?;
Ok(())
}
pub fn open<P>(path: P) -> Result<Self, Box<dyn Error>>
where
P: AsRef<Path>,
{
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
AnimationBlitBuffer::from_memory(&data[..])
}
pub fn from_memory(buffer: &[u8]) -> Result<Self, Box<dyn Error>> {
let buffer = deserialize(buffer)?;
Ok(buffer)
}
}