Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
animation.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <cmath>
22 #include <unordered_map>
23 #include <memory>
24 #include <vector>
25 #include <string>
26 #include "signals/signal.h"
27 #include "utils/throttle.h"
28 
29 namespace smlt {
30 
32  std::string animation_name;
33  float duration;
34 };
35 
36 class KeyFrameAnimated;
37 
38 typedef sig::signal<void (KeyFrameAnimated*, const std::string&)> SignalAnimationAdded;
39 
41  DEFINE_SIGNAL(SignalAnimationAdded, signal_animation_added);
42 public:
43  virtual ~KeyFrameAnimated() {}
44 
45  void add_sequence(const std::string& name, const std::vector<AnimationSequenceStage>& stages);
46  void add_animation(const std::string& name, uint32_t start_frame, uint32_t end_frame, float fps);
47  void add_animation(const std::string& name, uint32_t start_frame, uint32_t end_frame);
48 
49  bool has_animations() const { return !animations_.empty(); }
50  uint32_t animation_count() const { return animations_.size(); }
51 
52  void set_default_fps(float fps);
53  float default_fps() const;
54 protected:
55  //Animation stuff
56  friend class KeyFrameAnimationState;
57 
58  struct Animation {
59  Animation():
60  duration(0) {}
61 
62  Animation(float duration, uint32_t start, uint32_t end):
63  duration(duration),
64  frames(std::make_pair(start, end)) {}
65 
66  float duration;
67  std::pair<uint32_t, uint32_t> frames;
68  };
69 
70  typedef std::unordered_map<std::string, std::shared_ptr<Animation> > AnimationMap;
71  AnimationMap animations_;
72  std::string first_animation_;
73 
74  Animation* animation(const std::string& name) {
75  auto it = animations_.find(name);
76  if(it == animations_.end()) return nullptr;
77 
78  return (*it).second.get();
79  }
80 
81 private:
82  float default_fps_ = 7.0f; // This is a common frame rate on MD2 models
83 };
84 
85 // args: current_frame, next_frame, interp
86 typedef std::function<void (int32_t, int32_t, float)> AnimationUpdatedCallback;
87 
89 public:
91  KeyFrameAnimated* animatable,
92  AnimationUpdatedCallback refresh_animation_state
93  );
94 
95  virtual ~KeyFrameAnimationState() {
96  on_animation_added_.disconnect();
97  }
98 
99  void play_first_animation();
100  void play_animation(const std::string& name);
101  void queue_next_animation(const std::string& name);
102  void play_sequence(const std::string& name);
103 
104  void override_playing_animation_duration(const float new_duration);
105  void update(float dt);
106 
107  uint32_t current_frame() const { return current_frame_; }
108  uint32_t next_frame() const { return next_frame_; }
109  float interp() const { return interp_; }
110 
111 private:
112  KeyFrameAnimated* animatable_;
113 
114  KeyFrameAnimated::Animation* current_animation_ = nullptr;
115  KeyFrameAnimated::Animation* next_animation_ = nullptr;
116  float current_animation_duration_ = 0.0f;
117 
118  uint32_t current_frame_ = 0;
119  uint32_t next_frame_ = 0;
120  float interp_ = 0.0f;
121 
122  AnimationUpdatedCallback refresh_animation_state_;
123  sig::connection on_animation_added_;
124 
125  Throttle throttle_ = Throttle(60);
126 };
127 
128 
129 }
smlt::sig::Connection
Definition: signal.h:65
smlt
Definition: animation.cpp:25
smlt::KeyFrameAnimated::Animation
Definition: animation.h:58
smlt::Throttle
Definition: throttle.h:5
smlt::KeyFrameAnimated
Definition: animation.h:40
smlt::KeyFrameAnimationState
Definition: animation.h:88
smlt::AnimationSequenceStage
Definition: animation.h:31
smlt::sig::signal
Definition: signal.h:319