comparison engine/core/video/animation.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, 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
41 Animation::Animation():
42 m_location(""),
43 m_action_frame(-1),
44 m_animation_endtime(-1),
45 m_direction(0) {
46 }
47
48 Animation::~Animation() {
49 std::vector<FrameInfo>::const_iterator i(m_frames.begin());
50 while (i != m_frames.end()) {
51 i->img->decRef();
52 i++;
53 }
54 }
55
56 void Animation::addFrame(Image* image, unsigned int duration) {
57 image->addRef();
58
59 FrameInfo info;
60 info.index = m_frames.size();
61 info.duration = duration;
62 info.img = image;
63 m_frames.push_back(info);
64
65 std::map<unsigned int, FrameInfo>::const_iterator i(m_framemap.end());
66 if (i == m_framemap.begin()) {
67 m_framemap[0] = info;
68 m_animation_endtime = duration;
69 } else {
70 --i;
71 unsigned int frametime = i->first + i->second.duration;
72 m_framemap[frametime] = info;
73 m_animation_endtime = frametime + duration;
74 }
75
76 }
77
78 int Animation::getFrameIndex(unsigned int timestamp) {
79 int val = -1;
80 if ((static_cast<int>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
81 std::map<unsigned int, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
82 --i;
83 val = i->second.index;
84 }
85 return val;
86 }
87
88 bool Animation::isValidIndex(int index) {
89 int size = m_frames.size();
90 if ((size == 0) || (index > (size - 1)) || (index < 0)) {
91 return false;
92 }
93 return true;
94 }
95
96 Image* Animation::getFrame(int index) {
97 if (isValidIndex(index)) {
98 return m_frames[index].img;
99 } else {
100 return NULL;
101 }
102 }
103
104 Image* Animation::getFrameByTimestamp(unsigned int timestamp) {
105 return getFrame(getFrameIndex(timestamp));
106 }
107
108 int Animation::getFrameDuration(int index) {
109 if (isValidIndex(index)) {
110 return m_frames[index].duration;
111 } else {
112 return -1;
113 }
114 }
115
116 unsigned int Animation::getNumFrames() const {
117 return m_frames.size();
118 }
119
120 void Animation::setDirection(unsigned int direction) {
121 m_direction = direction % 360;
122 }
123 }
124 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */