FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
animation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <string>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/base/exception.h"
32 #include "util/time/timemanager.h"
34 
35 #include "animation.h"
36 #include "image.h"
37 
38 namespace FIFE {
39 
41  IResource(createUniqueAnimationName(), loader),
42  m_action_frame(-1),
43  m_animation_endtime(-1),
44  m_direction(0) {
45  }
46 
47  Animation::Animation(const std::string& name, IResourceLoader* loader):
48  IResource(name, loader),
49  m_action_frame(-1),
51  m_direction(0) {
52  }
53 
55  // note: we don't need to free the images, as they are handled via
56  // smart references.
57  }
58 
59  size_t Animation::getSize() {
60  return 0;
61  }
62 
63  void Animation::load() {
64  if (m_loader){
65  m_loader->load(this);
66  } else {
68  loader.load(this);
69  }
71  }
72 
73  void Animation::free() {
74  std::vector<FrameInfo>::iterator it = m_frames.begin();
75  for (; it != m_frames.end(); ++it) {
76  (*it).image->free();
77  }
79  }
80 
82  free();
83  m_framemap.clear();
84  m_frames.clear();
85  m_action_frame = -1;
87  m_direction = 0;
88  }
89 
91  // automated counting for name generation, in case the user doesn't provide a name
92  static uint32_t uniqueNumber = 0;
93  static std::string baseName = "animation";
94 
95  std::ostringstream oss;
96  oss << uniqueNumber << "_" << baseName;
97 
98  const std::string name = oss.str();
99  ++uniqueNumber;
100 
101  return name;
102  }
103 
104  void Animation::addFrame(ImagePtr image, uint32_t duration) {
105  FrameInfo info;
106  info.index = m_frames.size();
107  info.duration = duration;
108  info.image = image;
109  m_frames.push_back(info);
110 
111  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.end());
112  if (i == m_framemap.begin()) {
113  m_framemap[0] = info;
114  m_animation_endtime = duration;
115  } else {
116  --i;
117  uint32_t frametime = i->first + i->second.duration;
118  m_framemap[frametime] = info;
119  m_animation_endtime = frametime + duration;
120  }
121 
122  }
123 
124  int32_t Animation::getFrameIndex(uint32_t timestamp) {
125  int32_t val = -1;
126  if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
127  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
128  --i;
129  val = i->second.index;
130  }
131  return val;
132  }
133 
134  bool Animation::isValidIndex(int32_t index) const{
135  int32_t size = m_frames.size();
136  return size > 0 && index >= 0 && index < size;
137  }
138 
139  ImagePtr Animation::getFrame(int32_t index) {
140  ImagePtr image;
141  if (isValidIndex(index)) {
142  image = m_frames[index].image;
143  if (image->getState() == IResource::RES_NOT_LOADED) {
144  image->load();
145  }
146  }
147  return image;
148  }
149 
151  ImagePtr val;
152  if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
153  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
154  --i;
155  val = i->second.image;
156  }
157  if(val && val->getState() == IResource::RES_NOT_LOADED) {
158  val->load();
159  }
160  return val;
161  }
162 
163  std::vector<ImagePtr> Animation::getFrames() {
164  std::vector<ImagePtr> frames;
165  for (std::vector<FrameInfo>::iterator it = m_frames.begin(); it != m_frames.end(); ++it) {
166  frames.push_back((*it).image);
167  }
168  return frames;
169  }
170 
171  int32_t Animation::getFrameDuration(int32_t index) const{
172  if (isValidIndex(index)) {
173  return m_frames[index].duration;
174  }
175  return -1;
176  }
177 
179  return m_frames.size();
180  }
181 
183  m_direction = direction % 360;
184  }
185 }
virtual ResourceState getState()
Definition: resource.h:70
virtual void load()
Definition: image.cpp:124
virtual void load(IResource *res)
std::vector< ImagePtr > getFrames()
Gets all frame images.
Definition: animation.cpp:163
void setDirection(uint32_t direction)
Animation direction tells how this animation is associated with movement when played starting from fr...
Definition: animation.cpp:182
IResourceLoader * m_loader
Definition: resource.h:80
int32_t m_animation_endtime
Definition: animation.h:162
Animation(IResourceLoader *loader=0)
Constructor.
Definition: animation.cpp:40
Contains information about one animation frame (duration + frame index + frame pointer) ...
Definition: animation.h:143
uint32_t m_direction
Definition: animation.h:164
ImagePtr getFrameByTimestamp(uint32_t timestamp)
Gets the frame image that matches the given timestamp.
Definition: animation.cpp:150
std::vector< FrameInfo > m_frames
Definition: animation.h:158
virtual void load()
Definition: animation.cpp:63
std::string createUniqueAnimationName()
Definition: animation.cpp:90
void invalidate()
Definition: animation.cpp:81
int32_t getFrameIndex(uint32_t timestamp)
Get the frame index that matches given timestamp.
Definition: animation.cpp:124
virtual void free()
Definition: animation.cpp:73
ResourceState m_state
Definition: resource.h:81
bool isValidIndex(int32_t index) const
Checks for animation frame index overflows.
Definition: animation.cpp:134
std::map< uint32_t, FrameInfo > m_framemap
Definition: animation.h:156
ImagePtr getFrame(int32_t index)
Gets the frame iamge that matches the given index.
Definition: animation.cpp:139
void addFrame(ImagePtr image, uint32_t duration)
Adds new frame into animation Frames must be added starting from first frame.
Definition: animation.cpp:104
uint32_t getFrameCount() const
Get the number of frames.
Definition: animation.cpp:178
virtual void load(IResource *resource)=0
~Animation()
Destructor.
Definition: animation.cpp:54
int32_t m_action_frame
Definition: animation.h:160
unsigned int uint32_t
Definition: core.h:40
virtual size_t getSize()
Definition: animation.cpp:59
int32_t getFrameDuration(int32_t index) const
Gets the frame duration for given (indexed) frame.
Definition: animation.cpp:171