00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string>
00024
00025
00026 #include <boost/lexical_cast.hpp>
00027
00028
00029
00030
00031
00032 #include "util/base/exception.h"
00033 #include "util/time/timemanager.h"
00034
00035 #include "animation.h"
00036 #include "image.h"
00037 #include "util/structures/rect.h"
00038
00039 namespace FIFE {
00040
00041 Animation::Animation():
00042 m_action_frame(-1),
00043 m_animation_endtime(-1),
00044 m_direction(0) {
00045 }
00046
00047 Animation::~Animation() {
00048
00049
00050 }
00051
00052 void Animation::addFrame(ResourcePtr image, unsigned int duration) {
00053 FrameInfo info;
00054 info.index = m_frames.size();
00055 info.duration = duration;
00056 info.image = image;
00057 m_frames.push_back(info);
00058
00059 std::map<unsigned int, FrameInfo>::const_iterator i(m_framemap.end());
00060 if (i == m_framemap.begin()) {
00061 m_framemap[0] = info;
00062 m_animation_endtime = duration;
00063 } else {
00064 --i;
00065 unsigned int frametime = i->first + i->second.duration;
00066 m_framemap[frametime] = info;
00067 m_animation_endtime = frametime + duration;
00068 }
00069
00070 }
00071
00072 int Animation::getFrameIndex(unsigned int timestamp) {
00073 int val = -1;
00074 if ((static_cast<int>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
00075 std::map<unsigned int, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
00076 --i;
00077 val = i->second.index;
00078 }
00079 return val;
00080 }
00081
00082 bool Animation::isValidIndex(int index) const{
00083 int size = m_frames.size();
00084 return size > 0 && index >= 0 && index < size;
00085 }
00086
00087 Image* Animation::getFrame(int index) {
00088 if (isValidIndex(index)) {
00089 return m_frames[index].image.get<Image>();
00090 } else {
00091 return NULL;
00092 }
00093 }
00094
00095 Image* Animation::getFrameByTimestamp(unsigned int timestamp) {
00096 return getFrame(getFrameIndex(timestamp));
00097 }
00098
00099 int Animation::getFrameDuration(int index) const{
00100 if (isValidIndex(index)) {
00101 return m_frames[index].duration;
00102 } else {
00103 return -1;
00104 }
00105 }
00106
00107 unsigned int Animation::getNumFrames() const {
00108 return m_frames.size();
00109 }
00110
00111 void Animation::setDirection(unsigned int direction) {
00112 m_direction %= 360;
00113 }
00114 }
00115