FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
animation.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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(ResourcePtr image, unsigned int 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<unsigned int, 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  unsigned int frametime = i->first + i->second.duration;
66  m_framemap[frametime] = info;
67  m_animation_endtime = frametime + duration;
68  }
69 
70  }
71 
72  int Animation::getFrameIndex(unsigned int timestamp) {
73  int val = -1;
74  if ((static_cast<int>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
75  std::map<unsigned int, 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(int index) const{
83  int size = m_frames.size();
84  return size > 0 && index >= 0 && index < size;
85  }
86 
87  Image* Animation::getFrame(int index) {
88  if (isValidIndex(index)) {
89  return m_frames[index].image.get<Image>();
90  } else {
91  return NULL;
92  }
93  }
94 
95  Image* Animation::getFrameByTimestamp(unsigned int timestamp) {
96  return getFrame(getFrameIndex(timestamp));
97  }
98 
99  int Animation::getFrameDuration(int index) const{
100  if (isValidIndex(index)) {
101  return m_frames[index].duration;
102  } else {
103  return -1;
104  }
105  }
106 
107  unsigned int Animation::getNumFrames() const {
108  return m_frames.size();
109  }
110 
111  void Animation::setDirection(unsigned int direction) {
112  m_direction %= 360;
113  }
114 }
115 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */