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(ImagePtr image, uint32_t 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<uint32_t, 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 uint32_t frametime = i->first + i->second.duration;
00066 m_framemap[frametime] = info;
00067 m_animation_endtime = frametime + duration;
00068 }
00069
00070 }
00071
00072 int32_t Animation::getFrameIndex(uint32_t timestamp) {
00073 int32_t val = -1;
00074 if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
00075 std::map<uint32_t, 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(int32_t index) const{
00083 int32_t size = m_frames.size();
00084 return size > 0 && index >= 0 && index < size;
00085 }
00086
00087 ImagePtr Animation::getFrame(int32_t index) {
00088 if (isValidIndex(index)) {
00089 ImagePtr image = m_frames[index].image;
00090 if(image->getState() == IResource::RES_NOT_LOADED) {
00091 image->load();
00092 }
00093 return image;
00094 } else {
00095 return ImagePtr();
00096 }
00097 }
00098
00099 ImagePtr Animation::getFrameByTimestamp(uint32_t timestamp) {
00100 ImagePtr val;
00101 if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
00102 std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
00103 --i;
00104 val = i->second.image;
00105 }
00106 if(val && val->getState() == IResource::RES_NOT_LOADED) {
00107 val->load();
00108 }
00109 return val;
00110 }
00111
00112 int32_t Animation::getFrameDuration(int32_t index) const{
00113 if (isValidIndex(index)) {
00114 return m_frames[index].duration;
00115 } else {
00116 return -1;
00117 }
00118 }
00119
00120 uint32_t Animation::getFrameCount() const {
00121 return m_frames.size();
00122 }
00123
00124 void Animation::setDirection(uint32_t direction) {
00125 m_direction %= 360;
00126 }
00127 }
00128