FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
animationloader.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "ext/tinyxml/fife_tinyxml.h"
32 #include "vfs/vfs.h"
33 #include "vfs/raw/rawdata.h"
34 #include "video/imagemanager.h"
35 #include "video/image.h"
36 #include "video/animation.h"
37 #include "util/base/exception.h"
38 #include "util/log/logger.h"
39 #include "util/resource/resource.h"
41 
42 #include "animationloader.h"
43 
44 namespace FIFE {
48  static Logger _log(LM_NATIVE_LOADERS);
49 
51  : m_vfs(vfs), m_imageManager(imageManager) {
52 
53  }
54 
55  bool AnimationLoader::isLoadable(const std::string& filename) {
56  bfs::path animPath(filename);
57 
58  std::string animationFilename = animPath.string();
59 
60  try {
61  RawData* data = m_vfs->open(animationFilename);
62 
63  if (data) {
64  if (data->getDataLength() != 0) {
65  // TODO - this could be expanded to do more checks
66  TiXmlDocument doc;
67  doc.Parse(data->readString(data->getDataLength()).c_str());
68 
69  if (doc.Error()) {
70  return false;
71  }
72  }
73 
74  // done with data delete resource
75  delete data;
76  data = 0;
77  }
78  }
79  catch (NotFound&) {
80  return false;
81  }
82 
83  return true;
84  }
85 
86  AnimationPtr AnimationLoader::load(const std::string& filename) {
87  bfs::path animPath(filename);
88 
89  std::string animationFilename = animPath.string();
90 
91  TiXmlDocument doc;
92 
93  AnimationPtr animation;
94 
95  try {
96  RawData* data = m_vfs->open(animationFilename);
97 
98  if (data) {
99  if (data->getDataLength() != 0) {
100  doc.Parse(data->readString(data->getDataLength()).c_str());
101 
102  if (doc.Error()) {
103  return animation;
104  }
105 
106  // done with data delete resource
107  delete data;
108  data = 0;
109  }
110  }
111  }
112  catch (NotFound& e) {
113  FL_ERR(_log, e.what());
114 
115  // TODO - should we abort here
116  // or rethrow the exception
117  // or just keep going
118 
119  return animation;
120  }
121 
122  // if we get here then everything loaded properly
123  // so we can just parse out the contents
124  TiXmlElement* root = doc.RootElement();
125 
126  if (root) {
127  animation.reset(new Animation());
128 
129  int animDelay = 0;
130  root->QueryValueAttribute("delay", &animDelay);
131 
132  int animXoffset = 0;
133  int animYoffset = 0;
134  int action = -1;
135  root->QueryValueAttribute("x_offset", &animXoffset);
136  root->QueryValueAttribute("y_offset", &animYoffset);
137  root->QueryValueAttribute("action", &action);
138 
139  for (TiXmlElement* frameElement = root->FirstChildElement("frame"); frameElement; frameElement = frameElement->NextSiblingElement("frame")) {
140  if (animation) {
141  animation->setActionFrame(action);
142 
143  const std::string* sourceId = frameElement->Attribute(std::string("source"));
144 
145  if (sourceId) {
146  bfs::path framePath(filename);
147 
148  if (HasParentPath(framePath)) {
149  framePath = GetParentPath(framePath) / *sourceId;
150  } else {
151  framePath = bfs::path(*sourceId);
152  }
153 
154  ImagePtr imagePtr;
155  if(!m_imageManager->exists(framePath.string())) {
156  imagePtr = m_imageManager->create(framePath.string());
157  }
158  else {
159  imagePtr = m_imageManager->getPtr(framePath.string());
160  }
161 
162  if (imagePtr) {
163  int frameXoffset = 0;
164  int frameYoffset = 0;
165 
166  int success = root->QueryValueAttribute("x_offset", &frameXoffset);
167 
168  if (success == TIXML_SUCCESS) {
169  imagePtr->setXShift(frameXoffset);
170  }
171  else {
172  imagePtr->setXShift(animXoffset);
173  }
174 
175  success = root->QueryValueAttribute("y_offset", &frameYoffset);
176 
177  if (success == TIXML_SUCCESS) {
178  imagePtr->setYShift(frameYoffset);
179  }
180  else {
181  imagePtr->setYShift(animYoffset);
182  }
183 
184  int frameDelay = 0;
185  success = root->QueryValueAttribute("delay", &frameDelay);
186 
187  if (success == TIXML_SUCCESS) {
188  animation->addFrame(imagePtr, frameDelay);
189  }
190  else {
191  animation->addFrame(imagePtr, animDelay);
192  }
193  }
194  }
195  }
196  }
197  }
198 
199  return animation;
200  }
201 }
virtual ImagePtr create(IResourceLoader *loader=0)
Creates a blank Image but does not load it immediately.
virtual bool exists(const std::string &name)
Checks to see if an Image exists.
uint32_t getDataLength() const
get the complete datalength
Definition: rawdata.cpp:75
void setXShift(int32_t xshift)
Definition: image.h:114
ImageManager.
Definition: imagemanager.h:54
void reset(T *ptr=0)
reset this pointer to a null shared pointer this can be used to lower the reference count of the shar...
Definition: sharedptr.h:164
RawData * open(const std::string &path)
Open a file.
Definition: vfs.cpp:157
Animation.
Definition: animation.h:56
static Logger _log(LM_AUDIO)
void setActionFrame(int32_t num)
Sets the action frame.
Definition: animation.h:105
virtual ImagePtr getPtr(const std::string &name)
AnimationLoader(VFS *vfs, ImageManager *imageManager)
#define FL_ERR(logger, msg)
Definition: logger.h:73
bool HasParentPath(const bfs::path &path)
Helper function to determine if a path object has a parent path.
bfs::path GetParentPath(const bfs::path &path)
Helper function to retrieve a parent path object from a path object.
virtual AnimationPtr load(const std::string &filename)
std::string readString(size_t len)
read a string with len bytes, not assuming a terminating 0 Appends a null terminator character to the...
Definition: rawdata.cpp:128
void addFrame(ImagePtr image, uint32_t duration)
Adds new frame into animation Frames must be added starting from first frame.
Definition: animation.cpp:52
the main VFS (virtual file system) class
Definition: vfs.h:58
void setYShift(int32_t yshift)
Definition: image.h:120
ImageManager * m_imageManager
virtual bool isLoadable(const std::string &filename)
Used to access diffrent kinds of data.
Definition: rawdata.h:48