1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/***************************************************************************
* Copyright (C) 2005-2019 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
// 3rd party library includes
#include <tinyxml.h>
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "vfs/fife_boost_filesystem.h"
#include "vfs/vfs.h"
#include "vfs/raw/rawdata.h"
#include "video/imagemanager.h"
#include "video/animationmanager.h"
#include "video/image.h"
#include "video/animation.h"
#include "util/base/exception.h"
#include "util/log/logger.h"
#include "util/resource/resource.h"
#include "util/resource/resourcemanager.h"
#include "animationloader.h"
namespace FIFE {
/** Logger to use for this source file.
* @relates Logger
*/
static Logger _log(LM_NATIVE_LOADERS);
AnimationLoader::AnimationLoader(VFS* vfs, ImageManager* imageManager, AnimationManager* animationManager)
: m_vfs(vfs), m_imageManager(imageManager), m_animationManager(animationManager) {
}
bool AnimationLoader::isLoadable(const std::string& filename) {
bfs::path animPath(filename);
std::string animationFilename = animPath.string();
TiXmlDocument animFile;
try {
RawData* data = m_vfs->open(animationFilename);
if (data) {
if (data->getDataLength() != 0) {
// TODO - this could be expanded to do more checks
animFile.Parse(data->readString(data->getDataLength()).c_str());
if (animFile.Error()) {
return false;
}
}
// done with data delete resource
delete data;
data = 0;
}
}
catch (NotFound&) {
return false;
}
// if we get here then loading the file went well
TiXmlElement* root = animFile.RootElement();
if (root && root->ValueStr() == "assets") {
if (root->FirstChildElement("animation")) {
return true;
}
}
return false;
}
AnimationPtr AnimationLoader::load(const std::string& filename) {
bfs::path animPath(filename);
std::string animationFilename = animPath.string();
TiXmlDocument doc;
AnimationPtr animation;
try {
RawData* data = m_vfs->open(animationFilename);
if (data) {
if (data->getDataLength() != 0) {
doc.Parse(data->readString(data->getDataLength()).c_str());
if (doc.Error()) {
return animation;
}
// done with data delete resource
delete data;
data = 0;
}
}
}
catch (NotFound& e) {
FL_ERR(_log, e.what());
// TODO - should we abort here
// or rethrow the exception
// or just keep going
return animation;
}
// if we get here then everything loaded properly
// so we can just parse out the contents
TiXmlElement* root = doc.RootElement();
if (root && root->ValueStr() == "assets") {
animation = loadAnimation(filename, root->FirstChildElement("animation"));
}
return animation;
}
std::vector<AnimationPtr> AnimationLoader::loadMultiple(const std::string& filename) {
bfs::path animPath(filename);
std::string animationFilename = animPath.string();
TiXmlDocument doc;
std::vector<AnimationPtr> animationVector;
try {
RawData* data = m_vfs->open(animationFilename);
if (data) {
if (data->getDataLength() != 0) {
doc.Parse(data->readString(data->getDataLength()).c_str());
if (doc.Error()) {
return animationVector;
}
// done with data delete resource
delete data;
data = 0;
}
}
}
catch (NotFound& e) {
FL_ERR(_log, e.what());
// TODO - should we abort here
// or rethrow the exception
// or just keep going
return animationVector;
}
// if we get here then everything loaded properly
// so we can just parse out the contents
TiXmlElement* root = doc.RootElement();
if (root && root->ValueStr() == "assets") {
for (TiXmlElement* animationElem = root->FirstChildElement("animation"); animationElem; animationElem = animationElem->NextSiblingElement("animation")) {
AnimationPtr animation = loadAnimation(filename, animationElem);
if (animation) {
animationVector.push_back(animation);
}
}
}
return animationVector;
}
AnimationPtr AnimationLoader::loadAnimation(const std::string& filename, TiXmlElement* animationElem) {
AnimationPtr animation;
if (!animationElem) {
return animation;
}
bfs::path animPath(filename);
std::string animationFilename = animPath.string();
bool alreadyLoaded = false;
// first try to use the id, if no id exists it use the filename as fallback
const std::string* animationId = animationElem->Attribute(std::string("id"));
if (animationId) {
if (!m_animationManager->exists(*animationId)) {
animation = m_animationManager->create(*animationId);
} else {
animation = m_animationManager->getPtr(*animationId);
alreadyLoaded = animation->getFrameCount() != 0;
}
} else {
if (HasParentPath(animPath)) {
animPath= GetParentPath(animPath) / animationFilename;
} else {
animPath = bfs::path(animationFilename);
}
if (!m_animationManager->exists(animPath.string())) {
animation = m_animationManager->create(animPath.string());
} else {
animation = m_animationManager->getPtr(animPath.string());
alreadyLoaded = animation->getFrameCount() != 0;
}
}
if (alreadyLoaded) {
return animation;
}
int direction = 0;
int actionFrame = -1;
int animDelay = 0;
int animXoffset = 0;
int animYoffset = 0;
int success = animationElem->QueryValueAttribute("direction", &direction);
if (success == TIXML_SUCCESS) {
animation->setDirection(direction);
}
success = animationElem->QueryValueAttribute("action_frame", &actionFrame);
if (success == TIXML_SUCCESS) {
animation->setActionFrame(actionFrame);
}
animationElem->QueryValueAttribute("delay", &animDelay);
animationElem->QueryValueAttribute("x_offset", &animXoffset);
animationElem->QueryValueAttribute("y_offset", &animYoffset);
for (TiXmlElement* frameElement = animationElem->FirstChildElement("frame"); frameElement; frameElement = frameElement->NextSiblingElement("frame")) {
const std::string* sourceId = frameElement->Attribute(std::string("source"));
if (sourceId) {
bfs::path framePath(filename);
if (HasParentPath(framePath)) {
framePath = GetParentPath(framePath) / *sourceId;
if (!bfs::exists(framePath)) {
framePath = bfs::path(*sourceId);
}
} else {
framePath = bfs::path(*sourceId);
}
ImagePtr imagePtr;
if (!m_imageManager->exists(framePath.string())) {
imagePtr = m_imageManager->create(framePath.string());
} else {
imagePtr = m_imageManager->getPtr(framePath.string());
}
if (imagePtr) {
int frameXoffset = 0;
success = frameElement->QueryValueAttribute("x_offset", &frameXoffset);
if (success == TIXML_SUCCESS) {
imagePtr->setXShift(frameXoffset);
} else {
imagePtr->setXShift(animXoffset);
}
int frameYoffset = 0;
success = frameElement->QueryValueAttribute("y_offset", &frameYoffset);
if (success == TIXML_SUCCESS) {
imagePtr->setYShift(frameYoffset);
} else {
imagePtr->setYShift(animYoffset);
}
int frameDelay = 0;
success = frameElement->QueryValueAttribute("delay", &frameDelay);
if (success == TIXML_SUCCESS) {
animation->addFrame(imagePtr, frameDelay);
} else {
animation->addFrame(imagePtr, animDelay);
}
}
}
}
return animation;
}
}
|