FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
animationloader.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2011 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"
31 #include "vfs/fife_boost_filesystem.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"
40 #include "util/resource/resourcemanager.h"
41 
42 #include "animationloader.h"
43 
44 namespace FIFE {
45  static Logger _log(LM_NATIVE_LOADERS);
46 
47  AnimationLoader::AnimationLoader(VFS* vfs, ImageManager* imageManager)
48  : m_vfs(vfs), m_imageManager(imageManager) {
49 
50  }
51 
52  bool AnimationLoader::isLoadable(const std::string& filename) {
53  bfs::path animPath(filename);
54 
55  std::string animationFilename = animPath.string();
56 
57  try {
58  RawData* data = m_vfs->open(animationFilename);
59 
60  if (data) {
61  if (data->getDataLength() != 0) {
62  // TODO - this could be expanded to do more checks
63  TiXmlDocument doc;
64  doc.Parse(data->readString(data->getDataLength()).c_str());
65 
66  if (doc.Error()) {
67  return false;
68  }
69  }
70 
71  // done with data delete resource
72  delete data;
73  data = 0;
74  }
75  }
76  catch (NotFound&) {
77  return false;
78  }
79 
80  return true;
81  }
82 
83  AnimationPtr AnimationLoader::load(const std::string& filename) {
84  bfs::path animPath(filename);
85 
86  std::string animationFilename = animPath.string();
87 
88  TiXmlDocument doc;
89 
90  AnimationPtr animation;
91 
92  try {
93  RawData* data = m_vfs->open(animationFilename);
94 
95  if (data) {
96  if (data->getDataLength() != 0) {
97  doc.Parse(data->readString(data->getDataLength()).c_str());
98 
99  if (doc.Error()) {
100  return animation;
101  }
102 
103  // done with data delete resource
104  delete data;
105  data = 0;
106  }
107  }
108  }
109  catch (NotFound& e) {
110  FL_ERR(_log, e.what());
111 
112  // TODO - should we abort here
113  // or rethrow the exception
114  // or just keep going
115 
116  return animation;
117  }
118 
119  // if we get here then everything loaded properly
120  // so we can just parse out the contents
121  TiXmlElement* root = doc.RootElement();
122 
123  if (root) {
124  animation.reset(new Animation());
125 
126  int animDelay = 0;
127  root->QueryValueAttribute("delay", &animDelay);
128 
129  int animXoffset = 0;
130  int animYoffset = 0;
131  int action = -1;
132  root->QueryValueAttribute("x_offset", &animXoffset);
133  root->QueryValueAttribute("y_offset", &animYoffset);
134  root->QueryValueAttribute("action", &action);
135 
136  for (TiXmlElement* frameElement = root->FirstChildElement("frame"); frameElement; frameElement = frameElement->NextSiblingElement("frame")) {
137  if (animation) {
138  animation->setActionFrame(action);
139 
140  const std::string* sourceId = frameElement->Attribute(std::string("source"));
141 
142  if (sourceId) {
143  bfs::path framePath(filename);
144 
145  if (HasParentPath(framePath)) {
146  framePath = GetParentPath(framePath) / *sourceId;
147  } else {
148  framePath = bfs::path(*sourceId);
149  }
150 
151  ImagePtr imagePtr;
152  if(!m_imageManager->exists(framePath.string())) {
153  imagePtr = m_imageManager->create(framePath.string());
154  }
155  else {
156  imagePtr = m_imageManager->getPtr(framePath.string());
157  }
158 
159  if (imagePtr) {
160  int frameXoffset = 0;
161  int frameYoffset = 0;
162 
163  int success = root->QueryValueAttribute("x_offset", &frameXoffset);
164 
165  if (success == TIXML_SUCCESS) {
166  imagePtr->setXShift(frameXoffset);
167  }
168  else {
169  imagePtr->setXShift(animXoffset);
170  }
171 
172  success = root->QueryValueAttribute("y_offset", &frameYoffset);
173 
174  if (success == TIXML_SUCCESS) {
175  imagePtr->setYShift(frameYoffset);
176  }
177  else {
178  imagePtr->setYShift(animYoffset);
179  }
180 
181  int frameDelay = 0;
182  success = root->QueryValueAttribute("delay", &frameDelay);
183 
184  if (success == TIXML_SUCCESS) {
185  animation->addFrame(imagePtr, frameDelay);
186  }
187  else {
188  animation->addFrame(imagePtr, animDelay);
189  }
190  }
191  }
192  }
193  }
194  }
195 
196  return animation;
197  }
198 }