FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
animation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 #include <boost/lexical_cast.hpp>
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/base/exception.h"
33 #include "util/time/timemanager.h"
34 
35 #include "animation.h"
36 #include "image.h"
37 #include "util/structures/rect.h"
38 
39 namespace FIFE {
40 
42  m_action_frame(-1),
43  m_animation_endtime(-1),
44  m_direction(0) {
45  }
46 
48  // note: we don't need to free the images, as they are handled via
49  // smart references.
50  }
51 
52  void Animation::addFrame(ImagePtr image, uint32_t duration) {
53  FrameInfo info;
54  info.index = m_frames.size();
55  info.duration = duration;
56  info.image = image;
57  m_frames.push_back(info);
58 
59  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.end());
60  if (i == m_framemap.begin()) {
61  m_framemap[0] = info;
62  m_animation_endtime = duration;
63  } else {
64  --i;
65  uint32_t frametime = i->first + i->second.duration;
66  m_framemap[frametime] = info;
67  m_animation_endtime = frametime + duration;
68  }
69 
70  }
71 
72  int32_t Animation::getFrameIndex(uint32_t timestamp) {
73  int32_t val = -1;
74  if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
75  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
76  --i;
77  val = i->second.index;
78  }
79  return val;
80  }
81 
82  bool Animation::isValidIndex(int32_t index) const{
83  int32_t size = m_frames.size();
84  return size > 0 && index >= 0 && index < size;
85  }
86 
87  ImagePtr Animation::getFrame(int32_t index) {
88  if (isValidIndex(index)) {
89  ImagePtr image = m_frames[index].image;
90  if(image->getState() == IResource::RES_NOT_LOADED) {
91  image->load();
92  }
93  return image;
94  } else {
95  return ImagePtr(); //return an invalid image .
96  }
97  }
98 
100  ImagePtr val;
101  if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
102  std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
103  --i;
104  val = i->second.image;
105  }
106  if(val && val->getState() == IResource::RES_NOT_LOADED) {
107  val->load();
108  }
109  return val;
110  }
111 
112  int32_t Animation::getFrameDuration(int32_t index) const{
113  if (isValidIndex(index)) {
114  return m_frames[index].duration;
115  } else {
116  return -1;
117  }
118  }
119 
121  return m_frames.size();
122  }
123 
125  m_direction %= 360;
126  }
127 }
128 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
virtual ResourceState getState()
Definition: resource.h:70
virtual void load()
Definition: image.cpp:124
Animation()
Constructor.
Definition: animation.cpp:41
int32_t getFrameDuration(int32_t index) const
Gets the frame duration for given (indexed) frame.
Definition: animation.cpp:112
void setDirection(uint32_t direction)
Animation direction tells how this animation is associated with movement when played starting from fr...
Definition: animation.cpp:124
int32_t m_animation_endtime
Definition: animation.h:151
SharedPtr< Image > ImagePtr
Definition: image.h:42
Contains information about one animation frame (duration + frame index + frame pointer) ...
Definition: animation.h:134
uint32_t m_direction
Definition: animation.h:153
ImagePtr getFrameByTimestamp(uint32_t timestamp)
Gets the frame image that matches the given timestamp.
Definition: animation.cpp:99
std::vector< FrameInfo > m_frames
Definition: animation.h:147
uint32_t getFrameCount() const
Get the number of frames.
Definition: animation.cpp:120
int32_t getFrameIndex(uint32_t timestamp)
Get the frame index that matches given timestamp.
Definition: animation.cpp:72
std::map< uint32_t, FrameInfo > m_framemap
Definition: animation.h:145
ImagePtr getFrame(int32_t index)
Gets the frame iamge that matches the given index.
Definition: animation.cpp:87
void addFrame(ImagePtr image, uint32_t duration)
Adds new frame into animation Frames must be added starting from first frame.
Definition: animation.cpp:52
~Animation()
Destructor.
Definition: animation.cpp:47
bool isValidIndex(int32_t index) const
Checks for animation frame index overflows.
Definition: animation.cpp:82
unsigned int uint32_t
Definition: core.h:40