The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
animated.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2004 - 2025
3 by Philippe Plantier <ayin@anathas.org>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16/** @file animated.hpp
17 * Frame-based animation system with support for looping, acceleration, and time manipulation.
18 * The animation system uses two parallel timelines:
19 * - Normal timeline: Real wall-clock time for UI animations, effects, etc.
20 * - Accelerated timeline: Can be sped up/slowed down for unit movement, attacks, etc. */
21
22#pragma once
23
24#include <algorithm>
25#include <chrono>
26#include <vector>
27
28// ============================================================================
29// Global Animation Timeline Management
30// ============================================================================
31
32/** Updates both animation timelines.
33 * Should be called once per frame before advancing any animations.
34 * @param acceleration Multiplier for the accelerated timeline (1.0 = normal speed) */
35void update_animation_timers(double acceleration = 1.0);
36
37/** Gets the current time point for animations.
38 * @param uses_acceleration If true, returns accelerated timeline; otherwise normal timeline
39 * @return Current time point on the selected timeline */
40std::chrono::steady_clock::time_point get_current_animation_tick(bool uses_acceleration);
41
42
43// ============================================================================
44// Frame-Based Animation Template
45// ============================================================================
46
47template<typename T>
49{
50public:
51 typedef std::pair<std::chrono::milliseconds, T> frame_description;
52 typedef std::vector<frame_description> anim_description;
53
54 // ========================================================================
55 // Construction
56 // ========================================================================
57
58 /** Creates an animation with no frames; add_frame() populates it afterward.
59 * @param start_time Logical start time of the animation timeline (default 0ms) */
60 animated(const std::chrono::milliseconds& start_time = std::chrono::milliseconds{0});
61
62 /** Creates an animation with its frame list already built from cfg.
63 * @param cfg Sequence of (duration, value) pairs to build the frame list from
64 * @param start_time Logical start time of the animation timeline
65 * @param force_change See add_frame(). Applies to the whole sequence built from cfg. */
66 explicit animated(const anim_description& cfg,
67 const std::chrono::milliseconds& start_time = std::chrono::milliseconds{0},
68 bool force_change = false);
69
70 virtual ~animated() = default;
71
72 /** Appends a frame, starting where the previous one ends (or at start_time, if first).
73 * @param duration How long this frame should be displayed
74 * @param value The data/state for this frame
75 * @param force_change Forces is_static() false even if this is the only frame added so
76 * far; use when the frame's own value can change over time despite the frame count. */
77 void add_frame(const std::chrono::milliseconds& duration, const T& value, bool force_change = false);
78
79 // ========================================================================
80 // Playback Control
81 // ========================================================================
82
83 /** Anchors the timeline to the current tick, then begins playback.
84 * @param start_time_offset How far into the animation to start (0 = from beginning)
85 * @param cycles If true, loop back to the first frame on reaching the end; if false, stop there */
86 void start_animation(const std::chrono::milliseconds& start_time_offset, bool cycles = false);
87
88 /** Freezes the timeline in place. Playback continues from the same point via resume_animation(). */
90
91 /** Shifts the timeline forward by the time spent paused, then continues playback. */
93
94 /** Moves current_frame_index_ forward to match the current tick, one frame at a time
95 * (or in whole cycles, if far enough behind). Marks the animation finished() if it
96 * runs out of frames without cycles(). */
98
99 // ========================================================================
100 // Playback State Queries
101 // ========================================================================
102
103 /** True once the current tick has reached the current frame's end, meaning
104 * advance_to_current_frame() has a new frame to move to. Also true once after
105 * start_animation() or apply_time_offset(), even for a static animation, so the
106 * owner redraws the new first frame. */
107 bool need_update() const;
108
109 /** True if playback has run past the last frame, is not yet started, or has no frames.
110 * Always true for a cycling animation, since it never runs out. */
111 bool animation_finished() const;
112
113 /** True if the animation loops back to the first frame instead of stopping at the last. */
114 bool cycles() const { return cycles_; }
115
116 // ========================================================================
117 // Time Queries and Manipulation
118 // ========================================================================
119
120 /** Time since playback began, per get_playback_tick(); for a cycling animation, since the
121 * current cycle began. 0 if not started.
122 * Capped at the limit set by set_duration_limit(), if any. */
123 std::chrono::milliseconds get_elapsed_time() const;
124
125 /** Shifts the timeline so the current tick corresponds to the given animation time,
126 * then restarts frame tracking from the beginning to resync.
127 * Used to desynchronize otherwise-identical animations for visual variety.
128 * @param time e.g. 300ms starts the animation as if it had been playing for 300ms already */
129 void apply_time_offset(const std::chrono::milliseconds& time);
130
131 /** @param time Elapsed time past which animation_finished() reports true regardless of
132 * actual progress (0 = no limit). Does not affect frame playback, only that query. */
133 void set_duration_limit(const std::chrono::milliseconds& time);
134
135 /** get_end_time() minus get_begin_time(): how long one pass through all frames takes. */
136 std::chrono::milliseconds get_animation_duration() const;
137
138 /** Animation-time offset of the first frame's start. Default 0ms; see set_begin_time(). */
139 std::chrono::milliseconds get_begin_time() const;
140
141 /** Animation-time offset where the last frame ends.
142 * For 3 seconds of frames starting at 500ms (see get_begin_time()), this returns 3500ms. */
143 std::chrono::milliseconds get_end_time() const;
144
145 /** Rebases the timeline onto a new begin time, offsetting every frame's start_time_
146 * by the difference so their durations and order are unchanged. */
147 void set_begin_time(const std::chrono::milliseconds& new_begin_time);
148
149 // ========================================================================
150 // Frame Queries
151 // ========================================================================
152
153 /** Animation-time offset where the current frame starts. */
154 std::chrono::milliseconds get_current_frame_begin_time() const;
155
156 /** Animation-time offset where the current frame ends. */
157 std::chrono::milliseconds get_current_frame_end_time() const;
158
159 /** How far into its own duration the current frame is, clamped to [0, duration]. */
160 std::chrono::milliseconds get_time_in_current_frame() const;
161
162 const T& get_current_frame() const;
163 const T& get_first_frame() const;
164 const T& get_frame(std::size_t n) const;
165 const T& get_last_frame() const;
166
167 std::size_t get_frames_count() const;
168
169 // ========================================================================
170 // Optimization and Configuration
171 // ========================================================================
172
173 /** Clears the flag returned by is_static(). Needed because is_static_ is set purely from
174 * frame count: a single frame can still change visually over time through means this
175 * class doesn't track (e.g. unit_frame's interpolated offset/alpha/image parameters),
176 * in which case the owner must call this to keep need_update() reporting true. */
177 void force_change() { is_static_ = false; }
178
179 /** True when the single-unchanging-frame optimization is enabled: need_update() will
180 * always report false, since there is never a different frame to advance to.
181 * Set from add_frame()/the constructor when the animation has exactly one frame, unless
182 * force_change requests otherwise; cleared permanently by add_frame() past the first frame,
183 * or by force_change(). */
184 bool is_static() const { return is_static_; }
185
186 /** Selects which of the two global timelines get_current_animation_tick()
187 * reads for this animation: accelerated if true, normal if false. */
188 void set_uses_acceleration(bool uses_accel) { uses_acceleration_ = uses_accel; }
189
190 /** Which timeline this animation currently reads; see set_uses_acceleration(). */
191 bool uses_acceleration() const { return uses_acceleration_; }
192
193 static const T void_value_; // MSVC: the frame constructor below requires this to be public
194
195protected:
196 friend class unit_animation;
197
198 /** Drops whole frames from the front while their end time is still before starting_time,
199 * then advances the begin time by their durations. May overshoot starting_time if it
200 * falls inside a remaining frame, rather than split that frame.
201 * @param starting_time Animation time the new begin time should reach */
202 void remove_frames_until(const std::chrono::milliseconds& starting_time);
203
204 /** Reduces or extends the frame list so get_end_time() becomes ending_time: drops frames
205 * after the cut and shortens the one it falls in, extends the last frame if ending_time is
206 * later, or (if ending_time is before the current begin time) collapses to a zero-length
207 * first frame.
208 * @param ending_time Animation time the new end time should reach */
209 void set_end_time(const std::chrono::milliseconds& ending_time);
210
211private:
212 /** The tick get_elapsed_time() and get_time_in_current_frame() treat as "now": frozen at
213 * pause_tick_ while paused_, otherwise the live tick from get_current_animation_tick().
214 * Playback bookkeeping (start/pause/resume/advance) reads get_current_animation_tick()
215 * directly instead, since it needs the live clock even while paused_. */
216 std::chrono::steady_clock::time_point get_playback_tick() const;
217
218 // Internal frame representation
219 struct frame
220 {
221 std::chrono::milliseconds duration_; // How long this frame lasts
222 T value_; // The frame data
223 std::chrono::milliseconds start_time_; // When this frame starts (in animation time)
224 };
225
226 // Animation timeline configuration
227 std::chrono::milliseconds starting_frame_time_; // Logical start time of the animation
228 std::chrono::milliseconds max_animation_time_; // Optional duration cap (0 = unlimited)
229
230 // Frame storage and tracking
231 std::vector<frame> frames_; // All frames in sequence
232 std::size_t current_frame_index_; // Which frame is currently displayed
233
234 // Playback state
235 bool started_; // Whether values are initialized by start_animation()
236 bool paused_; // True while frozen at pause_tick_; see get_playback_tick()
237 bool finished_; // Set by advance_to_current_frame() when a non-cycling animation runs out of frames
238 bool needs_first_update_; // Set on (re)start, cleared by advance_to_current_frame(); see need_update()
239 bool cycles_; // Whether the animation loops/cycles
240 bool uses_acceleration_; // Which timeline to follow, normal or accelerated.
241
242 // Timeline synchronization
243 // The animation's position is tracked by relating animation time to real timeline ticks.
244 std::chrono::steady_clock::time_point animation_start_tick_; // Real tick corresponding to animation time 0; shifted by resume, apply_time_offset() and each cycle wrap.
245 std::chrono::steady_clock::time_point next_frame_tick_; // Real tick when next frame should display
246 std::chrono::steady_clock::time_point pause_tick_; // Real tick when pause_animation() was called
247
248 // Optimization flag
249 bool is_static_; // True if all frames identical (skip updates)
250};
251
252// NOTE: this needs to be down here or the templates won't build.
253#include "animated.tpp"
void update_animation_timers(double acceleration=1.0)
Updates both animation timelines.
Definition animated.cpp:38
std::chrono::steady_clock::time_point get_current_animation_tick(bool uses_acceleration)
Gets the current time point for animations.
Definition animated.cpp:49
bool is_static() const
True when the single-unchanging-frame optimization is enabled: need_update() will always report false...
Definition animated.hpp:184
std::chrono::milliseconds get_time_in_current_frame() const
How far into its own duration the current frame is, clamped to [0, duration].
animated(const anim_description &cfg, const std::chrono::milliseconds &start_time=std::chrono::milliseconds{0}, bool force_change=false)
Creates an animation with its frame list already built from cfg.
bool needs_first_update_
Definition animated.hpp:238
std::vector< frame > frames_
Definition animated.hpp:231
bool uses_acceleration() const
Which timeline this animation currently reads; see set_uses_acceleration().
Definition animated.hpp:191
void resume_animation()
Shifts the timeline forward by the time spent paused, then continues playback.
std::chrono::milliseconds get_begin_time() const
Animation-time offset of the first frame's start.
std::chrono::milliseconds get_current_frame_begin_time() const
Animation-time offset where the current frame starts.
virtual ~animated()=default
std::chrono::milliseconds max_animation_time_
Definition animated.hpp:228
bool uses_acceleration_
Definition animated.hpp:240
std::pair< std::chrono::milliseconds, T > frame_description
Definition animated.hpp:51
void set_uses_acceleration(bool uses_accel)
Selects which of the two global timelines get_current_animation_tick() reads for this animation: acce...
Definition animated.hpp:188
bool started_
Definition animated.hpp:235
std::chrono::steady_clock::time_point next_frame_tick_
Definition animated.hpp:245
void add_frame(const std::chrono::milliseconds &duration, const T &value, bool force_change=false)
Appends a frame, starting where the previous one ends (or at start_time, if first).
std::size_t current_frame_index_
Definition animated.hpp:232
void apply_time_offset(const std::chrono::milliseconds &time)
Shifts the timeline so the current tick corresponds to the given animation time, then restarts frame ...
void pause_animation()
Freezes the timeline in place.
const T & get_first_frame() const
bool is_static_
Definition animated.hpp:249
std::vector< frame_description > anim_description
Definition animated.hpp:52
void start_animation(const std::chrono::milliseconds &start_time_offset, bool cycles=false)
Anchors the timeline to the current tick, then begins playback.
std::chrono::steady_clock::time_point get_playback_tick() const
The tick get_elapsed_time() and get_time_in_current_frame() treat as "now": frozen at pause_tick_ whi...
const T & get_frame(std::size_t n) const
void set_end_time(const std::chrono::milliseconds &ending_time)
Reduces or extends the frame list so get_end_time() becomes ending_time: drops frames after the cut a...
void force_change()
Clears the flag returned by is_static().
Definition animated.hpp:177
std::chrono::steady_clock::time_point pause_tick_
Definition animated.hpp:246
bool finished_
Definition animated.hpp:237
bool need_update() const
True once the current tick has reached the current frame's end, meaning advance_to_current_frame() ha...
bool animation_finished() const
True if playback has run past the last frame, is not yet started, or has no frames.
std::chrono::milliseconds get_elapsed_time() const
Time since playback began, per get_playback_tick(); for a cycling animation, since the current cycle ...
std::chrono::milliseconds starting_frame_time_
Definition animated.hpp:227
void remove_frames_until(const std::chrono::milliseconds &starting_time)
Drops whole frames from the front while their end time is still before starting_time,...
bool cycles_
Definition animated.hpp:239
const T & get_current_frame() const
bool paused_
Definition animated.hpp:236
std::size_t get_frames_count() const
void set_begin_time(const std::chrono::milliseconds &new_begin_time)
Rebases the timeline onto a new begin time, offsetting every frame's start_time_ by the difference so...
std::chrono::steady_clock::time_point animation_start_tick_
Definition animated.hpp:244
std::chrono::milliseconds get_animation_duration() const
get_end_time() minus get_begin_time(): how long one pass through all frames takes.
void set_duration_limit(const std::chrono::milliseconds &time)
static const T void_value_
Definition animated.hpp:193
std::chrono::milliseconds get_end_time() const
Animation-time offset where the last frame ends.
std::chrono::milliseconds get_current_frame_end_time() const
Animation-time offset where the current frame ends.
animated(const std::chrono::milliseconds &start_time=std::chrono::milliseconds{0})
Creates an animation with no frames; add_frame() populates it afterward.
const T & get_last_frame() const
bool cycles() const
True if the animation loops back to the first frame instead of stopping at the last.
Definition animated.hpp:114
void advance_to_current_frame()
Moves current_frame_index_ forward to match the current tick, one frame at a time (or in whole cycles...
const config * cfg
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
std::chrono::milliseconds duration_
Definition animated.hpp:221
std::chrono::milliseconds start_time_
Definition animated.hpp:223
static map_location::direction n