FIFE  2008.0
animation.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <string>
00024 
00025 // 3rd party library includes
00026 #include <boost/lexical_cast.hpp>
00027 
00028 // FIFE includes
00029 // These includes are split up in two parts, separated by one empty line
00030 // First block: files included from the FIFE root src directory
00031 // Second block: files included from the same folder
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         // note: we don't need to free the images, as they are handled via
00049         // smart references.
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 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */