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(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();  //return an invalid image .
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 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */