-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
animated.tpp
380 lines (336 loc) · 9.81 KB
/
animated.tpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
Copyright (C) 2004 by Philippe Plantier <ayin@anathas.org>
Copyright (C) 2005 - 2016 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file animated.tpp
* Templates related to animations.
*/
#include "global.hpp"
#include <climits>
#include <SDL.h>
#include "animated.hpp"
template<typename T, typename T_void_value>
const T animated<T,T_void_value>::void_value_ = T_void_value()();
template<typename T, typename T_void_value>
inline animated<T,T_void_value>::animated(int start_time) :
starting_frame_time_(start_time),
does_not_change_(true),
started_(false),
force_next_update_(false),
frames_(),
start_tick_(0),
cycles_(false),
acceleration_(1),
last_update_tick_(0),
current_frame_key_(0)
{
}
template<typename T, typename T_void_value>
inline animated<T,T_void_value>::animated(const std::vector<std::pair<int,T> > &cfg, int start_time, bool force_change ):
starting_frame_time_(start_time),
does_not_change_(true),
started_(false),
force_next_update_(false),
frames_(),
start_tick_(0),
cycles_(false),
acceleration_(1),
last_update_tick_(0),
current_frame_key_(0)
{
typename std::vector< std::pair<int,T> >::const_iterator itor = cfg.begin();
for (; itor != cfg.end(); ++itor) {
add_frame(itor->first,itor->second,force_change);
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::add_frame(int duration, const T& value,bool force_change)
{
if (frames_.empty() ) {
does_not_change_=!force_change;
frames_.push_back( frame(duration,value,starting_frame_time_));
} else {
does_not_change_=false;
frames_.push_back( frame(duration,value,frames_.back().start_time_+frames_.back().duration_));
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::start_animation(int start_time, bool cycles)
{
started_ = true;
last_update_tick_ = get_current_animation_tick();
acceleration_ = 1.0; //assume acceleration is 1, this will be fixed at first update_last_draw_time
start_tick_ = last_update_tick_ +
static_cast<int>(( starting_frame_time_ - start_time)/acceleration_);
cycles_ = cycles;
if (acceleration_ <= 0) {
acceleration_ = 1;
}
current_frame_key_= 0;
force_next_update_ = !frames_.empty();
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::update_last_draw_time(double acceleration)
{
if (acceleration > 0 && acceleration_ != acceleration) {
int tmp = tick_to_time(last_update_tick_);
acceleration_ = acceleration;
start_tick_ = last_update_tick_ +
static_cast<int>(( starting_frame_time_ - tmp)/acceleration_);
}
if (!started_ && start_tick_ != 0) {
// animation is paused
start_tick_ += get_current_animation_tick() - last_update_tick_;
}
last_update_tick_ = get_current_animation_tick();
if (force_next_update_) {
force_next_update_ = false;
return;
}
if (does_not_change_) {
return;
}
// Always update last_update_tick_, for the animation_time functions to work.
if (!started_) {
return;
}
if (frames_.empty()) {
does_not_change_ = true;
return;
}
if (cycles_) {
while(get_animation_time() > get_end_time()){ // cut extra time
start_tick_ += std::max<int>(static_cast<int>(get_animation_duration()/acceleration_),1);
current_frame_key_ = 0;
}
}
if (get_current_frame_end_time() < get_animation_time() && // catch up
get_current_frame_end_time() < get_end_time()) {// don't go after the end
current_frame_key_++;
}
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::need_update() const
{
if (force_next_update_) {
return true;
}
if (does_not_change_) {
return false;
}
if (frames_.empty()) {
return false;
}
if (!started_ && start_tick_ == 0) {
return false;
}
if (get_current_animation_tick() >
static_cast<int>(get_current_frame_end_time() /
acceleration_ + start_tick_)) {
return true;
}
return false;
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::animation_finished_potential() const
{
if (frames_.empty()) {
return true;
}
if (!started_ && start_tick_ == 0) {
return true;
}
if (cycles_ ) {
return true;
}
if (tick_to_time(get_current_animation_tick()) > get_end_time()) {
return true;
}
return false;
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::animation_finished() const
{
if (frames_.empty()) {
return true;
}
if (!started_ && start_tick_ == 0) {
return true;
}
if (cycles_) {
return true;
}
if (get_animation_time() > get_end_time()) {
return true;
}
return false;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_time_potential() const
{
if (!started_ && start_tick_ == 0 ) {
return starting_frame_time_;
}
return tick_to_time(get_current_animation_tick());
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_time() const
{
if (!started_ && start_tick_ == 0 ) {
return starting_frame_time_;
}
return tick_to_time(last_update_tick_);
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_animation_time(int time)
{
start_tick_ = last_update_tick_ +
static_cast<int>((starting_frame_time_ - time) / acceleration_);
current_frame_key_= 0;
force_next_update_ = true;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_duration() const
{
return get_end_time() - get_begin_time();
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_current_frame() const
{
if (frames_.empty()) {
return void_value_;
}
return frames_[current_frame_key_].value_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_begin_time() const
{
if (frames_.empty()) {
return starting_frame_time_;
}
return frames_[current_frame_key_].start_time_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_end_time() const
{
if (frames_.empty()) {
return starting_frame_time_;
}
return get_current_frame_begin_time() +get_current_frame_duration();
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_duration() const
{
if (frames_.empty()) {
return 0;
}
return frames_[current_frame_key_].duration_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_time() const
{
if (frames_.empty()) {
return 0;
}
//FIXME: get_animation_time() use acceleration but get_current_frame_begin_time() doesn't ?
return std::max<int>(0,get_animation_time() - get_current_frame_begin_time());
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_first_frame() const
{
if (frames_.empty()) {
return void_value_;
}
return frames_[0].value_;
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_frame(size_t n) const
{
if (n >= frames_.size()) {
return void_value_;
}
return frames_[n].value_;
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_last_frame() const
{
if (frames_.empty()) {
return void_value_;
}
return frames_.back().value_;
}
template<typename T, typename T_void_value>
inline size_t animated<T,T_void_value>::get_frames_count() const
{
return frames_.size();
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_begin_time() const
{
return starting_frame_time_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::time_to_tick(int animation_time) const
{
if (!started_ && start_tick_ == 0) {
return 0;
}
return start_tick_ + static_cast<int>((animation_time-starting_frame_time_) / acceleration_);
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::tick_to_time(int animation_tick) const
{
if (!started_ && start_tick_ == 0) {
return 0;
}
return static_cast<int>(
(static_cast<double>(animation_tick - start_tick_) *
acceleration_) + starting_frame_time_);
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_end_time() const
{
if (frames_.empty()) {
return starting_frame_time_;
}
return frames_.back().start_time_ + frames_.back().duration_;
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::remove_frames_until(int new_starting_time)
{
while (starting_frame_time_ < new_starting_time && !frames_.empty()) {
starting_frame_time_ += frames_[0].duration_;
frames_.erase(frames_.begin());
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_end_time(int new_ending_time)
{
int last_start_time = starting_frame_time_;
typename std::vector<frame>::iterator current_frame = frames_.begin();
while (last_start_time < new_ending_time && current_frame != frames_.end()) {
last_start_time += current_frame->duration_;
++current_frame;
}
// at this point last_start_time is set to the beginning of the first frame past the end
// or set to frames_.end()
frames_.erase(current_frame,frames_.end());
frames_.back().duration_ += new_ending_time - last_start_time;
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_begin_time(int new_begin_time)
{
const int variation = new_begin_time - starting_frame_time_;
starting_frame_time_ += variation;
for (typename std::vector<frame>::iterator itor = frames_.begin(); itor != frames_.end() ; ++itor) {
itor->start_time_ += variation;
}
}