Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
animator.cpp
1/*
2 Wizard Engine
3 Copyright (C) 2023-2024 Zana Domán
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
23#define __WIZARD_ENGINE_INTERNAL__
24
27
28wze::animator::animator(
29 std::vector<std::pair<std::shared_ptr<texture>, uint16_t>> const& frames,
30 std::vector<std::weak_ptr<animatable>> const& targets) {
31 this->frames() = frames;
32 this->targets() = targets;
33 set_current_frame(0);
34 _remaining_time = 0;
35}
36
37std::vector<std::pair<std::shared_ptr<wze::texture>, uint16_t>> const&
38wze::animator::frames() const {
39 return _frames;
40}
41
42std::vector<std::pair<std::shared_ptr<wze::texture>, uint16_t>>&
43wze::animator::frames() {
44 return _frames;
45}
46
47std::vector<std::weak_ptr<wze::animatable>> const&
48wze::animator::targets() const {
49 return _targets;
50}
51
52std::vector<std::weak_ptr<wze::animatable>>& wze::animator::targets() {
53 return _targets;
54}
55
56size_t wze::animator::current_frame() const {
57 return _current_frame;
58}
59
60void wze::animator::set_current_frame(size_t current_frame) {
61 _current_frame = current_frame;
62}
63
64bool wze::animator::play() {
65 uint32_t elapsed_time;
66 bool looped;
67
68 if (frames().empty()) {
69 return false;
70 }
71
72 if ((bool)frames().at(current_frame()).second) {
73 elapsed_time = (uint32_t)timer::delta_time() + _remaining_time;
74 set_current_frame(current_frame() +
75 elapsed_time / frames().at(current_frame()).second);
76 _remaining_time = elapsed_time % frames().at(current_frame()).second;
77 } else {
78 set_current_frame(current_frame() + 1);
79 _remaining_time = 0;
80 }
81
82 looped = frames().size() <= current_frame();
83 if (looped) {
84 set_current_frame(current_frame() % frames().size());
85 }
86
87 targets().erase(
88 std::remove_if(targets().begin(), targets().end(),
89 [&](std::weak_ptr<animatable> const& target) -> bool {
90 std::shared_ptr<animatable> locked;
91
92 locked = target.lock();
93 if (locked) {
94 if (locked->animated()) {
95 locked->set_texture(
96 frames().at(current_frame()).first);
97 }
98 return false;
99 }
100
101 return true;
102 }),
103 targets().end());
104
105 return looped;
106}
107
108void wze::animator::reset() {
109 set_current_frame(0);
110 _remaining_time = 0;
111}
Animates animatable objects.
static float delta_time()
Gets the current delta time in milliseconds.
Definition timer.cpp:41
Timer modul.