The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
animated.tpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2005 - 2025
3 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4 Copyright (C) 2004 by Philippe Plantier <ayin@anathas.org>
5 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY.
13
14 See the COPYING file for more details.
15*/
16
17/**
18 * @file animated.tpp
19 * Template implementation for frame-based animations.
20 */
21
22
23// ============================================================================
24// Static Members
25// ============================================================================
26
27template<typename T>
28const T animated<T>::void_value_ = T();
29
30
31// ============================================================================
32// Construction
33// ============================================================================
34
35template<typename T>
36inline animated<T>::animated(const std::chrono::milliseconds& start_time)
37 : starting_frame_time_(start_time)
38 , max_animation_time_(0)
39 , frames_()
40 , current_frame_index_(0)
41 , started_(false)
42 , paused_(false)
43 , finished_(false)
44 , needs_first_update_(false)
45 , cycles_(false)
46 , uses_acceleration_(false)
47 , animation_start_tick_()
48 , next_frame_tick_()
49 , pause_tick_()
50 , is_static_(true)
51{
52}
53
54template<typename T>
55inline animated<T>::animated(const animated<T>::anim_description& cfg, const std::chrono::milliseconds& start_time, bool force_change)
56 : starting_frame_time_(start_time)
57 , max_animation_time_(0)
58 , frames_()
59 , current_frame_index_(0)
60 , started_(false)
61 , paused_(false)
62 , finished_(false)
63 , needs_first_update_(false)
64 , cycles_(false)
65 , uses_acceleration_(false)
66 , animation_start_tick_()
67 , next_frame_tick_()
68 , pause_tick_()
69 , is_static_(true)
70{
71 for(const auto& [duration, value] : cfg) {
72 add_frame(duration, value, force_change);
73 }
74}
75
76template<typename T>
77inline void animated<T>::add_frame(const std::chrono::milliseconds& duration, const T& value, bool force_change)
78{
79 // NOTE: We cannot use emplace_back here, because the value may be a reference into the same vector,
80 // which case emplace_back could invalidate it before the new frame is constructed.
81
82 if(!frames_.empty()) {
83 const auto& last_frame = frames_.back();
84 frames_.push_back(frame{duration, value, last_frame.start_time_ + last_frame.duration_});
85 is_static_ = false;
86 } else {
87 is_static_ = !force_change;
88 frames_.push_back(frame{duration, value, starting_frame_time_});
89 }
90}
91
92
93// ============================================================================
94// Playback Control
95// ============================================================================
96
97template<typename T>
98inline void animated<T>::start_animation(const std::chrono::milliseconds& start_time_offset, bool cycles)
99{
100 started_ = true;
101 paused_ = false;
102 finished_ = false;
103 cycles_ = cycles;
104 current_frame_index_ = 0;
105 const auto current_tick = get_current_animation_tick(uses_acceleration_);
106 animation_start_tick_ = current_tick - start_time_offset;
107 if(!frames_.empty()) {
108 next_frame_tick_ = animation_start_tick_ + frames_[0].start_time_ + frames_[0].duration_;
109 needs_first_update_ = true;
110 }
111}
112
113template<typename T>
114inline void animated<T>::pause_animation()
115{
116 if(!started_ || paused_) {
117 return;
118 }
119 paused_ = true;
120 pause_tick_ = get_current_animation_tick(uses_acceleration_);
121}
122
123template<typename T>
124inline void animated<T>::resume_animation()
125{
126 if(!paused_) {
127 return;
128 }
129
130 const auto current_tick = get_current_animation_tick(uses_acceleration_);
131 const auto pause_duration = current_tick - pause_tick_;
132
133 // Shift the timeline forward by the pause duration
134 animation_start_tick_ += pause_duration;
135 next_frame_tick_ += pause_duration;
136
137 paused_ = false;
138}
139
140template<typename T>
141inline void animated<T>::advance_to_current_frame()
142{
143 needs_first_update_ = false;
144 if(frames_.empty()) {
145 return;
146 }
147
148 const auto current_tick = get_current_animation_tick(uses_acceleration_);
149
150 // If a cycling animation is more than a whole cycle behind (e.g., it went un-updated
151 // while offscreen), skip forward in whole cycles so the loop below never has to walk
152 // more than one cycle of frames.
153 if(cycles_) {
154 const std::chrono::milliseconds animation_duration = get_animation_duration();
155 if(animation_duration > std::chrono::milliseconds{0} && current_tick > (next_frame_tick_ + animation_duration)) {
156 const auto behind = current_tick - next_frame_tick_;
157 const auto skipped = (behind / animation_duration) * animation_duration; // The int division truncates to whole cycles.
158 next_frame_tick_ += skipped;
159 animation_start_tick_ += skipped;
160 }
161 }
162
163 // Advance frame by frame until we reach the frame containing the current time
164 while(current_tick >= next_frame_tick_) {
165 std::size_t next_index = current_frame_index_ + 1;
166
167 // Non-looping animation finished, stay on last frame
168 if(!cycles_ && next_index == frames_.size()) {
169 finished_ = true;
170 return;
171 }
172
173 // Advance to the next frame, wrapping around when looping
174 current_frame_index_ = next_index % frames_.size();
175 if(current_frame_index_ == 0) {
176 animation_start_tick_ += get_animation_duration();
177 }
178 next_frame_tick_ += frames_[current_frame_index_].duration_;
179 }
180}
181
182
183// ============================================================================
184// Playback State Queries
185// ============================================================================
186
187template<typename T>
188inline bool animated<T>::need_update() const
189{
190 if(needs_first_update_) {
191 return true;
192 }
193 if(is_static_ || !started_ || paused_ || finished_ || frames_.empty()) {
194 return false;
195 }
196 return get_current_animation_tick(uses_acceleration_) >= next_frame_tick_;
197}
198
199template<typename T>
200inline bool animated<T>::animation_finished() const
201{
202 return finished_ || frames_.empty() || !started_ || cycles_ || (get_elapsed_time() >= get_end_time());
203}
204
205
206// ============================================================================
207// Time Queries and Manipulation
208// ============================================================================
209
210template<typename T>
211inline std::chrono::steady_clock::time_point animated<T>::get_playback_tick() const
212{
213 return paused_ ? pause_tick_ : get_current_animation_tick(uses_acceleration_);
214}
215
216template<typename T>
217inline std::chrono::milliseconds animated<T>::get_elapsed_time() const
218{
219 if(!started_) {
220 return starting_frame_time_;
221 }
222
223 // Calculate elapsed time since animation started
224 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(get_playback_tick() - animation_start_tick_);
225
226 // Apply maximum time cap if set
227 if(max_animation_time_ > std::chrono::milliseconds{0} && elapsed > max_animation_time_) {
228 return max_animation_time_;
229 }
230
231 return elapsed;
232}
233
234template<typename T>
235inline void animated<T>::apply_time_offset(const std::chrono::milliseconds& time)
236{
237 // Adjust the anchor point to make the current tick correspond to the requested time offset
238 animation_start_tick_ -= time;
239
240 // Restart animation from beginning; advance_to_current_frame() will sync on next call
241 if(!frames_.empty()) {
242 current_frame_index_ = 0;
243 finished_ = false;
244 needs_first_update_ = true;
245 next_frame_tick_ = animation_start_tick_ + frames_[0].start_time_ + frames_[0].duration_;
246 }
247}
248
249template<typename T>
250inline void animated<T>::set_duration_limit(const std::chrono::milliseconds& time)
251{
252 max_animation_time_ = time;
253}
254
255template<typename T>
256inline std::chrono::milliseconds animated<T>::get_animation_duration() const
257{
258 return get_end_time() - get_begin_time();
259}
260
261template<typename T>
262inline std::chrono::milliseconds animated<T>::get_begin_time() const
263{
264 return starting_frame_time_;
265}
266
267template<typename T>
268inline std::chrono::milliseconds animated<T>::get_end_time() const
269{
270 if(frames_.empty()) {
271 return starting_frame_time_;
272 }
273 const auto& last_frame = frames_.back();
274 return last_frame.start_time_ + last_frame.duration_;
275}
276
277template<typename T>
278inline void animated<T>::set_begin_time(const std::chrono::milliseconds& new_begin_time)
279{
280 const auto shift = new_begin_time - starting_frame_time_;
281 starting_frame_time_ = new_begin_time;
282 for(auto& frame : frames_) {
283 frame.start_time_ += shift;
284 }
285}
286
287// ============================================================================
288// Frame Queries
289// ============================================================================
290
291template<typename T>
292inline const T& animated<T>::get_current_frame() const
293{
294 if(frames_.empty()) {
295 return void_value_;
296 }
297 return frames_[current_frame_index_].value_;
298}
299
300template<typename T>
301inline std::chrono::milliseconds animated<T>::get_current_frame_begin_time() const
302{
303 if(frames_.empty()) {
304 return starting_frame_time_;
305 }
306 return frames_[current_frame_index_].start_time_;
307}
308
309template<typename T>
310inline std::chrono::milliseconds animated<T>::get_current_frame_end_time() const
311{
312 if(frames_.empty()) {
313 return starting_frame_time_;
314 }
315 const auto& frame = frames_[current_frame_index_];
316 return frame.start_time_ + frame.duration_;
317}
318
319template<typename T>
320inline std::chrono::milliseconds animated<T>::get_time_in_current_frame() const
321{
322 if(frames_.empty() || !started_) {
323 return std::chrono::milliseconds{0};
324 }
325 const auto current_tick = get_playback_tick();
326 const auto& frame = frames_[current_frame_index_];
327
328 // How much of this frame has elapsed, clamped to [0, duration].
329 const auto time_until_next = std::chrono::duration_cast<std::chrono::milliseconds>(next_frame_tick_ - current_tick);
330 const auto elapsed_in_frame = frame.duration_ - time_until_next;
331 return std::clamp(elapsed_in_frame, std::chrono::milliseconds{0}, frame.duration_);
332}
333
334template<typename T>
335inline const T& animated<T>::get_first_frame() const
336{
337 if(frames_.empty()) {
338 return void_value_;
339 }
340 return frames_[0].value_;
341}
342
343template<typename T>
344inline const T& animated<T>::get_frame(std::size_t n) const
345{
346 if(n >= frames_.size()) {
347 return void_value_;
348 }
349 return frames_[n].value_;
350}
351
352template<typename T>
353inline const T& animated<T>::get_last_frame() const
354{
355 if(frames_.empty()) {
356 return void_value_;
357 }
358 return frames_.back().value_;
359}
360
361template<typename T>
362inline std::size_t animated<T>::get_frames_count() const
363{
364 return frames_.size();
365}
366
367
368// ============================================================================
369// Protected Methods (for unit_animation)
370// ============================================================================
371
372template<typename T>
373void animated<T>::remove_frames_until(const std::chrono::milliseconds& new_starting_time)
374{
375 // Remove frames from the beginning until we reach the target time
376 while(starting_frame_time_ < new_starting_time && !frames_.empty()) {
377 starting_frame_time_ += frames_[0].duration_;
378 frames_.erase(frames_.begin());
379 }
380}
381
382template<typename T>
383inline void animated<T>::set_end_time(const std::chrono::milliseconds& new_ending_time)
384{
385 if(frames_.empty()) {
386 return;
387 }
388
389 // Cut-off before the animation begins: keep nothing but a zero-length first frame
390 if(new_ending_time <= starting_frame_time_) {
391 frames_.erase(std::next(frames_.begin()), frames_.end());
392 frames_.front().duration_ = std::chrono::milliseconds{0};
393 return;
394 }
395
396 // Cut-off at or past the current end: extend the last frame to reach it
397 const auto current_end_time = get_end_time();
398 if(new_ending_time >= current_end_time) {
399 frames_.back().duration_ += new_ending_time - current_end_time;
400 return;
401 }
402
403 // The cut-off lands within a frame: walk to it
404 auto current_start_time = starting_frame_time_;
405 auto it = frames_.begin();
406
407 while(it != frames_.end()) {
408 const auto next_end_time = current_start_time + it->duration_;
409
410 // Check if the cut-off point is within or at the end of this frame
411 if(next_end_time >= new_ending_time) {
412 // Truncate this frame to time remaining (a no-op if the cut-off is exactly at its end)
413 it->duration_ = new_ending_time - current_start_time;
414
415 // Erase all SUBSEQUENT frames (keep the current one)
416 frames_.erase(std::next(it), frames_.end());
417 return;
418 }
419
420 current_start_time = next_end_time;
421 ++it;
422 }
423}