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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/**************************************************************************
* Copyright (C) 2005-2011 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
// 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 "ext/tinyxml/fife_tinyxml.h"
#include "util/log/logger.h"
#include "model/model.h"
#include "model/metamodel/object.h"
#include "model/metamodel/action.h"
#include "vfs/fife_boost_filesystem.h"
#include "vfs/vfs.h"
#include "vfs/raw/rawdata.h"
#include "view/visual.h"
#include "video/imagemanager.h"
#include "objectloader.h"
#include "animationloader.h"
namespace FIFE {
static Logger _log(LM_NATIVE_LOADERS);
ObjectLoader::ObjectLoader(Model* model, VFS* vfs, ImageManager* imageManager, const AnimationLoaderPtr& animationLoader)
: m_model(model), m_vfs(vfs), m_imageManager(imageManager) {
assert(m_model && m_vfs && m_imageManager);
if (animationLoader) {
m_animationLoader = animationLoader;
}
else {
m_animationLoader.reset(new AnimationLoader(m_vfs, m_imageManager));
}
}
ObjectLoader::~ObjectLoader() {
}
void ObjectLoader::setAnimationLoader(const AnimationLoaderPtr& animationLoader) {
assert(animationLoader);
m_animationLoader = animationLoader;
}
bool ObjectLoader::isLoadable(const std::string& filename) const {
bfs::path objectPath(filename);
TiXmlDocument objectFile;
try {
RawData* data = m_vfs->open(objectPath.string());
if (data) {
if (data->getDataLength() != 0) {
objectFile.Parse(data->readString(data->getDataLength()).c_str());
if (objectFile.Error()) {
std::ostringstream oss;
oss << " Failed to load"
<< objectPath.string()
<< " : " << __FILE__
<< " [" << __LINE__ << "]"
<< std::endl;
FL_ERR(_log, oss.str());
return false;
}
}
else {
std::ostringstream oss;
oss << " Failed to load"
<< objectPath.string()
<< " : " << __FILE__
<< " [" << __LINE__ << "]"
<< std::endl;
FL_ERR(_log, oss.str());
return false;
}
// done with data delete resource
delete data;
data = 0;
}
else {
std::ostringstream oss;
oss << " Failed to load"
<< objectPath.string()
<< " : " << __FILE__
<< " [" << __LINE__ << "]"
<< std::endl;
FL_ERR(_log, oss.str());
return false;
}
}
catch (NotFound&) {
std::ostringstream oss;
oss << " Failed to load"
<< objectPath.string()
<< " : " << __FILE__
<< " [" << __LINE__ << "]"
<< std::endl;
FL_ERR(_log, oss.str());
// TODO - should we abort here
// or rethrow the exception
// or just keep going
return false;
}
// if we get here then loading the file went well
TiXmlElement* root = objectFile.RootElement();
if (root && root->ValueStr() == "object") {
return true;
}
else {
return false;
}
}
void ObjectLoader::load(const std::string& filename) {
bfs::path objectPath(filename);
TiXmlDocument objectFile;
try {
RawData* data = m_vfs->open(objectPath.string());
if (data) {
if (data->getDataLength() != 0) {
objectFile.Parse(data->readString(data->getDataLength()).c_str());
if (objectFile.Error()) {
return;
}
}
// done with data delete resource
delete data;
data = 0;
}
}
catch (NotFound&) {
std::ostringstream oss;
oss << " Failed to load"
<< objectPath.string()
<< " : " << __FILE__
<< " [" << __LINE__ << "]"
<< std::endl;
FL_ERR(_log, oss.str());
// TODO - should we abort here
// or rethrow the exception
// or just keep going
return;
}
// if we get here then loading the file went well
TiXmlElement* root = objectFile.RootElement();
if (root && root->ValueStr() == "object") {
const std::string* objectId = root->Attribute(std::string("id"));
const std::string* namespaceId = root->Attribute(std::string("namespace"));
Object* obj = NULL;
if (objectId && namespaceId) {
const std::string* parentId = root->Attribute(std::string("parent"));
if (parentId) {
Object* parent = m_model->getObject(*parentId, *namespaceId);
if (parent) {
try {
obj = m_model->createObject(*objectId, *namespaceId, parent);
}
catch (NameClash&) {
// TODO - handle exception
assert(false);
}
}
}
else {
// this will make sure the object has not already been loaded
if (m_model->getObject(*objectId, *namespaceId) == NULL) {
try {
obj = m_model->createObject(*objectId, *namespaceId);
}
catch (NameClash &e) {
FL_ERR(_log, e.what());
// TODO - handle exception
assert(false);
}
}
}
}
if (obj) {
obj->setFilename(objectPath.string());
ObjectVisual::create(obj);
int isBlocking = 0;
root->QueryIntAttribute("blocking", &isBlocking);
obj->setBlocking(isBlocking!=0);
int isStatic = 0;
root->QueryIntAttribute("static", &isStatic);
obj->setStatic(isStatic!=0);
const std::string* pather = root->Attribute(std::string("pather"));
if (pather) {
obj->setPather(m_model->getPather(*pather));
}
else {
obj->setPather(m_model->getPather("RoutePather"));
}
// loop over all image tags
for (TiXmlElement* imageElement = root->FirstChildElement("image"); imageElement; imageElement = imageElement->NextSiblingElement("image")) {
const std::string* sourceId = imageElement->Attribute(std::string("source"));
if (sourceId) {
bfs::path imagePath(filename);
if (HasParentPath(imagePath)) {
imagePath = GetParentPath(imagePath) / *sourceId;
} else {
imagePath = bfs::path(*sourceId);
}
ImagePtr imagePtr;
if(!m_imageManager->exists(imagePath.string())) {
imagePtr = m_imageManager->create(imagePath.string());
}
else {
imagePtr = m_imageManager->getPtr(imagePath.string());
}
if (imagePtr) {
int xOffset = 0;
int success = imageElement->QueryIntAttribute("x_offset", &xOffset);
if (success == TIXML_SUCCESS) {
imagePtr->setXShift(xOffset);
}
int yOffset = 0;
success = imageElement->QueryIntAttribute("y_offset", &yOffset);
if (success == TIXML_SUCCESS) {
imagePtr->setYShift(yOffset);
}
int direction = 0;
success = imageElement->QueryIntAttribute("direction", &direction);
if (success == TIXML_SUCCESS) {
ObjectVisual* objVisual = obj->getVisual<ObjectVisual>();
if (objVisual) {
objVisual->addStaticImage(direction, static_cast<int32_t>(imagePtr->getHandle()));
}
}
}
}
}
for (TiXmlElement* actionElement = root->FirstChildElement("action"); actionElement; actionElement = actionElement->NextSiblingElement("action")) {
const std::string* actionId = actionElement->Attribute(std::string("id"));
if (actionId) {
Action* action = obj->createAction(*actionId);
ActionVisual::create(action);
for (TiXmlElement* animElement = actionElement->FirstChildElement("animation"); animElement; animElement = animElement->NextSiblingElement("animation")) {
const std::string* sourceId = animElement->Attribute(std::string("atlas"));
if(sourceId) {
bfs::path atlasPath(filename);
if (HasParentPath(atlasPath)) {
atlasPath = GetParentPath(atlasPath) / *sourceId;
} else {
atlasPath = bfs::path(*sourceId);
}
ImagePtr atlasImgPtr;
// we need to load this since its shared image
if(!m_imageManager->exists(atlasPath.string())) {
atlasImgPtr = m_imageManager->create(atlasPath.string());
}
else {
atlasImgPtr = m_imageManager->getPtr(atlasPath.string());
}
int animFrames = 0;
int animDelay = 0;
int animXoffset = 0;
int animYoffset = 0;
int frameWidth = 0;
int frameHeight = 0;
animElement->QueryValueAttribute("width", &frameWidth);
animElement->QueryValueAttribute("height", &frameHeight);
animElement->QueryValueAttribute("frames", &animFrames);
animElement->QueryValueAttribute("delay", &animDelay);
animElement->QueryValueAttribute("x_offset", &animXoffset);
animElement->QueryValueAttribute("y_offset", &animYoffset);
int nDir = 0;
for (TiXmlElement* dirElement = animElement->FirstChildElement("direction");
dirElement; dirElement = dirElement->NextSiblingElement("direction")) {
AnimationPtr animation(new Animation);
int dir;
dirElement->QueryIntAttribute("dir", &dir);
int frames;
int success;
success = dirElement->QueryValueAttribute("frames", &frames);
if(success != TIXML_SUCCESS) {
frames = animFrames;
}
int delay;
success = dirElement->QueryValueAttribute("delay", &delay);
if(success != TIXML_SUCCESS) {
delay = animDelay;
}
int xoffset;
success = dirElement->QueryValueAttribute("x_offset", &xoffset);
if(success != TIXML_SUCCESS) {
xoffset = animXoffset;
}
int yoffset;
success = dirElement->QueryValueAttribute("y_offset", &yoffset);
if(success != TIXML_SUCCESS) {
yoffset = animYoffset;
}
int action_frame;
success = dirElement->QueryValueAttribute("action", &action_frame);
if(success == TIXML_SUCCESS) {
animation->setActionFrame(action_frame);
}
for (int iframe = 0; iframe < frames; ++iframe) {
static char tmpBuf[64];
sprintf(tmpBuf, "%03d:%04d", dir, iframe);
std::string frameId = *objectId + ":" + *actionId + ":" + std::string(tmpBuf);
ImagePtr framePtr;
if (!m_imageManager->exists(frameId)) {
framePtr = m_imageManager->create(frameId);
Rect region(
frameWidth * iframe, frameHeight * nDir, frameWidth, frameHeight
);
framePtr->useSharedImage(atlasImgPtr, region);
framePtr->setXShift(xoffset);
framePtr->setYShift(yoffset);
}
else {
framePtr = m_imageManager->getPtr(frameId);
}
animation->addFrame(framePtr, delay);
}
ActionVisual* actionVisual = action->getVisual<ActionVisual>();
if(actionVisual) {
actionVisual->addAnimation(dir, animation);
action->setDuration(animation->getDuration());
}
++nDir;
}
} else {
sourceId = animElement->Attribute(std::string("source"));
if (sourceId) {
bfs::path animPath(filename);
if (HasParentPath(animPath)) {
animPath = GetParentPath(animPath) / *sourceId;
} else {
animPath = bfs::path(*sourceId);
}
AnimationPtr animation;
if (m_animationLoader && m_animationLoader->isLoadable(animPath.string())) {
animation = m_animationLoader->load(animPath.string());
}
int direction = 0;
int success = animElement->QueryIntAttribute("direction", &direction);
if (action && animation) {
ActionVisual* actionVisual = action->getVisual<ActionVisual>();
if (actionVisual) {
actionVisual->addAnimation(direction, animation);
action->setDuration(animation->getDuration());
}
}
}
}
}
}
}
}
}
}
}
|