Debian Codegraph @

engine/core/video/animation.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library 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 GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <string>
24
25 // 3rd party library includes
26
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/base/exception.h"
32 #include "util/time/timemanager.h"
33 #include "loaders/native/video/resourceanimationloader.h"
34
35 #include "animation.h"
36 #include "image.h"
37
38 namespace FIFE {
39
40 Animation::Animation(IResourceLoader* loader):
41 IResource(createUniqueAnimationName(), loader),
42 m_action_frame(-1),
43 m_animation_endtime(-1),
44 m_direction(0) {
45 }
46
47 Animation::Animation(const std::string& name, IResourceLoader* loader):
48 IResource(name, loader),
49 m_action_frame(-1),
50 m_animation_endtime(-1),
51 m_direction(0) {
52 }
53
54 Animation::~Animation() {
55 // note: we don't need to free the images, as they are handled via
56 // smart references.
57 }
58
59 size_t Animation::getSize() {
60 return 0;
61 }
62
63 void Animation::load() {
64 if (m_loader){
65 m_loader->load(this);
66 } else {
67 ResourceAnimationLoader loader;
68 loader.load(this);
69 }
70 m_state = IResource::RES_LOADED;
71 }
72
73 void Animation::free() {
74 std::vector<FrameInfo>::iterator it = m_frames.begin();
75 for (; it != m_frames.end(); ++it) {
76 (*it).image->free();
77 }
78 m_state = IResource::RES_NOT_LOADED;
79 }
80
81 void Animation::invalidate() {
82 free();
83 m_framemap.clear();
84 m_frames.clear();
85 m_action_frame = -1;
86 m_animation_endtime = -1;
87 m_direction = 0;
88 }
89
90 std::string Animation::createUniqueAnimationName() {
91 // automated counting for name generation, in case the user doesn't provide a name
92 static uint32_t uniqueNumber = 0;
93 static std::string baseName = "animation";
94
95 std::ostringstream oss;
96 oss << uniqueNumber << "_" << baseName;
97
98 const std::string name = oss.str();
99 ++uniqueNumber;
100
101 return name;
102 }
103
104 void Animation::addFrame(ImagePtr image, uint32_t duration) {
105 FrameInfo info;
106 info.index = m_frames.size();
107 info.duration = duration;
108 info.image = image;
109 m_frames.push_back(info);
110
111 std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.end());
112 if (i == m_framemap.begin()) {
113 m_framemap[0] = info;
114 m_animation_endtime = duration;
115 } else {
116 --i;
117 uint32_t frametime = i->first + i->second.duration;
118 m_framemap[frametime] = info;
119 m_animation_endtime = frametime + duration;
120 }
121
122 }
123
124 int32_t Animation::getFrameIndex(uint32_t timestamp) {
125 int32_t val = -1;
126 if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
127 std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
128 --i;
129 val = i->second.index;
130 }
131 return val;
132 }
133
134 bool Animation::isValidIndex(int32_t index) const{
135 int32_t size = m_frames.size();
136 return size > 0 && index >= 0 && index < size;
137 }
138
139 ImagePtr Animation::getFrame(int32_t index) {
140 ImagePtr image;
141 if (isValidIndex(index)) {
142 image = m_frames[index].image;
143 if (image->getState() == IResource::RES_NOT_LOADED) {
144 image->load();
145 }
146 }
147 return image;
148 }
149
150 ImagePtr Animation::getFrameByTimestamp(uint32_t timestamp) {
151 ImagePtr val;
152 if ((static_cast<int32_t>(timestamp) <= m_animation_endtime) && (m_animation_endtime > 0)) {
153 std::map<uint32_t, FrameInfo>::const_iterator i(m_framemap.upper_bound(timestamp));
154 --i;
155 val = i->second.image;
156 }
157 if(val && val->getState() == IResource::RES_NOT_LOADED) {
158 val->load();
159 }
160 return val;
161 }
162
163 std::vector<ImagePtr> Animation::getFrames() {
164 std::vector<ImagePtr> frames;
165 for (std::vector<FrameInfo>::iterator it = m_frames.begin(); it != m_frames.end(); ++it) {
166 frames.push_back((*it).image);
167 }
168 return frames;
169 }
170
171 int32_t Animation::getFrameDuration(int32_t index) const{
172 if (isValidIndex(index)) {
173 return m_frames[index].duration;
174 }
175 return -1;
176 }
177
178 uint32_t Animation::getFrameCount() const {
179 return m_frames.size();
180 }
181
182 void Animation::setDirection(uint32_t direction) {
183 m_direction = direction % 360;
184 }
185 }