The Battle for Wesnoth  1.19.25+dev
animated.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2025
3  by Jeremy Rosen <jeremy.rosen@enst-bretagne.fr>
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.cpp
17  * Global timeline management for the animation system.
18  * Maintains two parallel timelines:
19  * - Normal timeline: Updated at real wall-clock rate
20  * - Accelerated timeline: Can be sped up/slowed down
21  * Call update_animation_timers() once per frame before advancing animations. */
22 
23 #include "animated.hpp"
24 
25 // Put these here to ensure that there's only
26 // one instance of the normal_timeline/accelerated_timeline variables
27 namespace {
28 
29  // Current time point on the normal (real-time) timeline.
30  // Deliberately initialized to now() (program start): the absolute value is arbitrary,
31  // only differences matter, and this keeps the first update's elapsed small.
32  std::chrono::steady_clock::time_point normal_timeline = std::chrono::steady_clock::now();
33 
34  // Current time point on the accelerated timeline (for unit movement, etc.)
35  std::chrono::steady_clock::time_point accelerated_timeline = normal_timeline;
36 }
37 
38 void update_animation_timers(double acceleration)
39 {
40  // Update normal timeline to current wall-clock time
41  const auto now = std::chrono::steady_clock::now();
42  const auto elapsed = now - normal_timeline;
43  normal_timeline = now;
44 
45  // Update accelerated timeline based on elapsed time and acceleration factor
46  accelerated_timeline += std::chrono::duration_cast<std::chrono::steady_clock::duration>(elapsed * acceleration);
47 }
48 
49 std::chrono::steady_clock::time_point get_current_animation_tick(bool uses_acceleration)
50 {
51  return uses_acceleration ? accelerated_timeline : normal_timeline;
52 }
void update_animation_timers(double acceleration)
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
Frame-based animation system with support for looping, acceleration, and time manipulation.