30 #include "ext/tinyxml/fife_tinyxml.h"
31 #include "util/log/logger.h"
32 #include "model/model.h"
33 #include "model/metamodel/object.h"
34 #include "model/metamodel/action.h"
35 #include "vfs/fife_boost_filesystem.h"
37 #include "vfs/raw/rawdata.h"
38 #include "view/visual.h"
39 #include "video/imagemanager.h"
41 #include "objectloader.h"
42 #include "animationloader.h"
45 static Logger _log(LM_NATIVE_LOADERS);
47 ObjectLoader::ObjectLoader(Model* model, VFS* vfs, ImageManager* imageManager,
const AnimationLoaderPtr& animationLoader)
48 : m_model(model), m_vfs(vfs), m_imageManager(imageManager) {
49 assert(m_model && m_vfs && m_imageManager);
51 if (animationLoader) {
52 m_animationLoader = animationLoader;
55 m_animationLoader.reset(
new AnimationLoader(m_vfs, m_imageManager));
59 ObjectLoader::~ObjectLoader() {
63 void ObjectLoader::setAnimationLoader(
const AnimationLoaderPtr& animationLoader) {
64 assert(animationLoader);
66 m_animationLoader = animationLoader;
69 bool ObjectLoader::isLoadable(
const std::string& filename)
const {
70 bfs::path objectPath(filename);
75 RawData* data = m_vfs->open(objectPath.string());
78 if (data->getDataLength() != 0) {
79 objectFile.
Parse(data->readString(data->getDataLength()).c_str());
81 if (objectFile.
Error()) {
82 std::ostringstream oss;
83 oss <<
" Failed to load"
84 << objectPath.string()
86 <<
" [" << __LINE__ <<
"]"
88 FL_ERR(_log, oss.str());
94 std::ostringstream oss;
95 oss <<
" Failed to load"
96 << objectPath.string()
98 <<
" [" << __LINE__ <<
"]"
100 FL_ERR(_log, oss.str());
110 std::ostringstream oss;
111 oss <<
" Failed to load"
112 << objectPath.string()
114 <<
" [" << __LINE__ <<
"]"
116 FL_ERR(_log, oss.str());
122 std::ostringstream oss;
123 oss <<
" Failed to load"
124 << objectPath.string()
126 <<
" [" << __LINE__ <<
"]"
128 FL_ERR(_log, oss.str());
140 if (root && root->ValueStr() ==
"object") {
148 void ObjectLoader::load(
const std::string& filename) {
149 bfs::path objectPath(filename);
154 RawData* data = m_vfs->open(objectPath.string());
157 if (data->getDataLength() != 0) {
158 objectFile.
Parse(data->readString(data->getDataLength()).c_str());
160 if (objectFile.
Error()) {
171 std::ostringstream oss;
172 oss <<
" Failed to load"
173 << objectPath.string()
175 <<
" [" << __LINE__ <<
"]"
177 FL_ERR(_log, oss.str());
189 if (root && root->ValueStr() ==
"object") {
190 const std::string* objectId = root->
Attribute(std::string(
"id"));
191 const std::string* namespaceId = root->
Attribute(std::string(
"namespace"));
194 if (objectId && namespaceId) {
195 const std::string* parentId = root->
Attribute(std::string(
"parent"));
198 Object* parent = m_model->getObject(*parentId, *namespaceId);
201 obj = m_model->createObject(*objectId, *namespaceId, parent);
211 if (m_model->getObject(*objectId, *namespaceId) == NULL) {
213 obj = m_model->createObject(*objectId, *namespaceId);
215 catch (NameClash &e) {
216 FL_ERR(_log, e.what());
226 obj->setFilename(objectPath.string());
227 ObjectVisual::create(obj);
231 obj->setBlocking(isBlocking!=0);
235 obj->setStatic(isStatic!=0);
237 const std::string* pather = root->
Attribute(std::string(
"pather"));
240 obj->setPather(m_model->getPather(*pather));
243 obj->setPather(m_model->getPather(
"RoutePather"));
248 const std::string* sourceId = imageElement->Attribute(std::string(
"source"));
251 bfs::path imagePath(filename);
256 imagePath = bfs::path(*sourceId);
260 if(!m_imageManager->exists(imagePath.string())) {
261 imagePtr = m_imageManager->create(imagePath.string());
264 imagePtr = m_imageManager->getPtr(imagePath.string());
269 int success = imageElement->QueryIntAttribute(
"x_offset", &xOffset);
271 if (success == TIXML_SUCCESS) {
272 imagePtr->setXShift(xOffset);
276 success = imageElement->QueryIntAttribute(
"y_offset", &yOffset);
278 if (success == TIXML_SUCCESS) {
279 imagePtr->setYShift(yOffset);
283 success = imageElement->QueryIntAttribute(
"direction", &direction);
285 if (success == TIXML_SUCCESS) {
286 ObjectVisual* objVisual = obj->getVisual<ObjectVisual>();
289 objVisual->addStaticImage(direction, static_cast<int32_t>(imagePtr->getHandle()));
297 const std::string* actionId = actionElement->Attribute(std::string(
"id"));
300 Action* action = obj->createAction(*actionId);
301 ActionVisual::create(action);
304 const std::string* sourceId = animElement->Attribute(std::string(
"atlas"));
306 bfs::path atlasPath(filename);
311 atlasPath = bfs::path(*sourceId);
314 ImagePtr atlasImgPtr;
316 if(!m_imageManager->exists(atlasPath.string())) {
317 atlasImgPtr = m_imageManager->create(atlasPath.string());
320 atlasImgPtr = m_imageManager->getPtr(atlasPath.string());
330 animElement->QueryValueAttribute(
"width", &frameWidth);
331 animElement->QueryValueAttribute(
"height", &frameHeight);
332 animElement->QueryValueAttribute(
"frames", &animFrames);
333 animElement->QueryValueAttribute(
"delay", &animDelay);
334 animElement->QueryValueAttribute(
"x_offset", &animXoffset);
335 animElement->QueryValueAttribute(
"y_offset", &animYoffset);
340 AnimationPtr animation(
new Animation);
343 dirElement->QueryIntAttribute(
"dir", &dir);
348 success = dirElement->QueryValueAttribute(
"frames", &frames);
349 if(success != TIXML_SUCCESS) {
354 success = dirElement->QueryValueAttribute(
"delay", &delay);
355 if(success != TIXML_SUCCESS) {
360 success = dirElement->QueryValueAttribute(
"x_offset", &xoffset);
361 if(success != TIXML_SUCCESS) {
362 xoffset = animXoffset;
366 success = dirElement->QueryValueAttribute(
"y_offset", &yoffset);
367 if(success != TIXML_SUCCESS) {
368 yoffset = animYoffset;
372 success = dirElement->QueryValueAttribute(
"action", &action_frame);
373 if(success == TIXML_SUCCESS) {
374 animation->setActionFrame(action_frame);
377 for (
int iframe = 0; iframe < frames; ++iframe) {
378 static char tmpBuf[64];
379 sprintf(tmpBuf,
"%03d:%04d", dir, iframe);
381 std::string frameId = *objectId +
":" + *actionId +
":" + std::string(tmpBuf);
384 if (!m_imageManager->exists(frameId)) {
385 framePtr = m_imageManager->create(frameId);
387 frameWidth * iframe, frameHeight * nDir, frameWidth, frameHeight
389 framePtr->useSharedImage(atlasImgPtr, region);
390 framePtr->setXShift(xoffset);
391 framePtr->setYShift(yoffset);
394 framePtr = m_imageManager->getPtr(frameId);
396 animation->addFrame(framePtr, delay);
399 ActionVisual* actionVisual = action->getVisual<ActionVisual>();
401 actionVisual->addAnimation(dir, animation);
402 action->setDuration(animation->getDuration());
408 sourceId = animElement->Attribute(std::string(
"source"));
410 bfs::path animPath(filename);
415 animPath = bfs::path(*sourceId);
418 AnimationPtr animation;
419 if (m_animationLoader && m_animationLoader->isLoadable(animPath.string())) {
420 animation = m_animationLoader->load(animPath.string());
424 int success = animElement->QueryIntAttribute(
"direction", &direction);
426 if (action && animation) {
427 ActionVisual* actionVisual = action->getVisual<ActionVisual>();
430 actionVisual->addAnimation(direction, animation);
431 action->setDuration(animation->getDuration());