2010-07-31 09:53:02 +00:00
|
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
|
*
|
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-08-06 13:13:25 +00:00
|
|
|
|
/*
|
2010-07-31 09:53:02 +00:00
|
|
|
|
* This code is based on Broken Sword 2.5 engine
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
|
|
|
|
|
*
|
|
|
|
|
* Licensed under GNU GPL v2
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-07-30 09:02:39 +00:00
|
|
|
|
#include "sword25/gfx/animationresource.h"
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-07-30 09:02:39 +00:00
|
|
|
|
#include "sword25/kernel/kernel.h"
|
2011-01-28 16:13:12 +00:00
|
|
|
|
#include "sword25/kernel/resmanager.h" // for PRECACHE_RESOURCES
|
2010-07-30 09:02:39 +00:00
|
|
|
|
#include "sword25/package/packagemanager.h"
|
|
|
|
|
#include "sword25/gfx/bitmapresource.h"
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-05 12:48:19 +00:00
|
|
|
|
namespace Sword25 {
|
|
|
|
|
|
2011-06-05 22:28:18 +00:00
|
|
|
|
enum {
|
|
|
|
|
DEFAULT_FPS = 10,
|
|
|
|
|
MIN_FPS = 1,
|
|
|
|
|
MAX_FPS = 200
|
|
|
|
|
};
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
AnimationResource::AnimationResource(const Common::String &filename) :
|
|
|
|
|
Resource(filename, Resource::TYPE_ANIMATION),
|
|
|
|
|
Common::XMLParser(),
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_valid(false) {
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Get a pointer to the package manager
|
2010-10-19 21:03:33 +00:00
|
|
|
|
_pPackage = Kernel::getInstance()->getPackage();
|
2011-01-23 15:01:24 +00:00
|
|
|
|
assert(_pPackage);
|
2010-08-23 00:32:45 +00:00
|
|
|
|
|
|
|
|
|
// Switch to the folder the specified Xml fiile is in
|
2010-09-25 19:48:26 +00:00
|
|
|
|
Common::String oldDirectory = _pPackage->getCurrentDirectory();
|
2010-09-02 17:11:45 +00:00
|
|
|
|
if (getFileName().contains('/')) {
|
|
|
|
|
Common::String dir = Common::String(getFileName().c_str(), strrchr(getFileName().c_str(), '/'));
|
2010-09-25 19:48:26 +00:00
|
|
|
|
_pPackage->changeDirectory(dir);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Load the contents of the file
|
2010-09-02 12:14:04 +00:00
|
|
|
|
uint fileSize;
|
2010-09-25 19:48:26 +00:00
|
|
|
|
char *xmlData = _pPackage->getXmlFile(getFileName(), &fileSize);
|
2010-08-23 00:32:45 +00:00
|
|
|
|
if (!xmlData) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Could not read \"%s\".", getFileName().c_str());
|
2011-06-19 22:59:48 +00:00
|
|
|
|
return;
|
2010-08-23 00:32:45 +00:00
|
|
|
|
}
|
2010-08-06 13:13:25 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Parse the contents
|
|
|
|
|
if (!loadBuffer((const byte *)xmlData, fileSize))
|
|
|
|
|
return;
|
2010-08-06 13:13:25 +00:00
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_valid = parse();
|
2010-08-23 00:32:45 +00:00
|
|
|
|
close();
|
|
|
|
|
free(xmlData);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Switch back to the previous folder
|
2010-09-25 19:48:26 +00:00
|
|
|
|
_pPackage->changeDirectory(oldDirectory);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Give an error message if there weren't any frames specified
|
2010-09-02 17:11:45 +00:00
|
|
|
|
if (_frames.empty()) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("\"%s\" does not have any frames.", getFileName().c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Pre-cache all the frames
|
2010-10-12 19:38:40 +00:00
|
|
|
|
if (!precacheAllFrames()) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Could not precache all frames of \"%s\".", getFileName().c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Post processing to compute animation features
|
2010-10-12 19:38:40 +00:00
|
|
|
|
if (!computeFeatures()) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Could not determine the features of \"%s\".", getFileName().c_str());
|
2010-08-06 13:13:25 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_valid = true;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
bool AnimationResource::parseBooleanKey(Common::String s, bool &result) {
|
|
|
|
|
s.toLowercase();
|
|
|
|
|
if (!strcmp(s.c_str(), "true"))
|
|
|
|
|
result = true;
|
|
|
|
|
else if (!strcmp(s.c_str(), "false"))
|
|
|
|
|
result = false;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AnimationResource::parserCallback_animation(ParserNode *node) {
|
2010-10-15 12:19:13 +00:00
|
|
|
|
if (!parseIntegerKey(node->values["fps"], 1, &_FPS) || (_FPS < MIN_FPS) || (_FPS > MAX_FPS)) {
|
2011-06-05 22:28:18 +00:00
|
|
|
|
return parserError(Common::String::format("Illegal or missing fps attribute in <animation> tag in \"%s\". Assuming default (\"%d\").",
|
|
|
|
|
getFileName().c_str(), DEFAULT_FPS));
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Loop type value
|
|
|
|
|
const char *loopTypeString = node->values["type"].c_str();
|
2011-06-19 22:59:48 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
if (strcmp(loopTypeString, "oneshot") == 0) {
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_animationType = Animation::AT_ONESHOT;
|
2010-08-23 00:32:45 +00:00
|
|
|
|
} else if (strcmp(loopTypeString, "loop") == 0) {
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_animationType = Animation::AT_LOOP;
|
2010-08-23 00:32:45 +00:00
|
|
|
|
} else if (strcmp(loopTypeString, "jojo") == 0) {
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_animationType = Animation::AT_JOJO;
|
2010-08-23 00:32:45 +00:00
|
|
|
|
} else {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
warning("Illegal type value (\"%s\") in <animation> tag in \"%s\". Assuming default (\"loop\").",
|
2010-09-02 17:11:45 +00:00
|
|
|
|
loopTypeString, getFileName().c_str());
|
|
|
|
|
_animationType = Animation::AT_LOOP;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
// Calculate the milliseconds required per frame
|
|
|
|
|
// FIXME: Double check variable naming. Based on the constant, it may be microseconds
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_millisPerFrame = 1000000 / _FPS;
|
2010-08-23 00:32:45 +00:00
|
|
|
|
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
bool AnimationResource::parserCallback_frame(ParserNode *node) {
|
|
|
|
|
Frame frame;
|
|
|
|
|
|
|
|
|
|
const char *fileString = node->values["file"].c_str();
|
|
|
|
|
if (!fileString) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("<frame> tag without file attribute occurred in \"%s\".", getFileName().c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.fileName = _pPackage->getAbsolutePath(fileString);
|
|
|
|
|
if (frame.fileName.empty()) {
|
2011-06-19 22:59:48 +00:00
|
|
|
|
error("Could not create absolute path for file specified in <frame> tag in \"%s\": \"%s\".",
|
2010-09-02 17:11:45 +00:00
|
|
|
|
getFileName().c_str(), fileString);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
const char *actionString = node->values["action"].c_str();
|
|
|
|
|
if (actionString)
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.action = actionString;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
const char *hotspotxString = node->values["hotspotx"].c_str();
|
|
|
|
|
const char *hotspotyString = node->values["hotspoty"].c_str();
|
|
|
|
|
if ((!hotspotxString && hotspotyString) ||
|
|
|
|
|
(hotspotxString && !hotspotyString))
|
2011-01-23 14:49:50 +00:00
|
|
|
|
warning("%s attribute occurred without %s attribute in <frame> tag in \"%s\". Assuming default (\"0\").",
|
2010-08-23 00:32:45 +00:00
|
|
|
|
hotspotxString ? "hotspotx" : "hotspoty",
|
|
|
|
|
!hotspotyString ? "hotspoty" : "hotspotx",
|
2010-09-02 17:11:45 +00:00
|
|
|
|
getFileName().c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.hotspotX = 0;
|
|
|
|
|
if (hotspotxString && !parseIntegerKey(hotspotxString, 1, &frame.hotspotX))
|
2011-01-23 15:07:46 +00:00
|
|
|
|
warning("Illegal hotspotx value (\"%s\") in frame tag in \"%s\". Assuming default (\"%d\").",
|
2010-10-12 19:38:40 +00:00
|
|
|
|
hotspotxString, getFileName().c_str(), frame.hotspotX);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.hotspotY = 0;
|
|
|
|
|
if (hotspotyString && !parseIntegerKey(hotspotyString, 1, &frame.hotspotY))
|
2011-01-23 15:07:46 +00:00
|
|
|
|
warning("Illegal hotspoty value (\"%s\") in frame tag in \"%s\". Assuming default (\"%d\").",
|
2010-10-12 19:38:40 +00:00
|
|
|
|
hotspotyString, getFileName().c_str(), frame.hotspotY);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
Common::String flipVString = node->values["flipv"];
|
|
|
|
|
if (!flipVString.empty()) {
|
2010-10-12 19:38:40 +00:00
|
|
|
|
if (!parseBooleanKey(flipVString, frame.flipV)) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
warning("Illegal flipv value (\"%s\") in <frame> tag in \"%s\". Assuming default (\"false\").",
|
2010-09-02 17:11:45 +00:00
|
|
|
|
flipVString.c_str(), getFileName().c_str());
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.flipV = false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
2010-08-06 13:13:25 +00:00
|
|
|
|
} else
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.flipV = false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-08-23 00:32:45 +00:00
|
|
|
|
Common::String flipHString = node->values["fliph"];
|
|
|
|
|
if (!flipHString.empty()) {
|
2010-10-12 19:38:40 +00:00
|
|
|
|
if (!parseBooleanKey(flipVString, frame.flipV)) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
warning("Illegal fliph value (\"%s\") in <frame> tag in \"%s\". Assuming default (\"false\").",
|
2010-09-02 17:11:45 +00:00
|
|
|
|
flipHString.c_str(), getFileName().c_str());
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.flipH = false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
2010-08-06 13:13:25 +00:00
|
|
|
|
} else
|
2010-10-12 19:38:40 +00:00
|
|
|
|
frame.flipH = false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_frames.push_back(frame);
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 10:52:24 +00:00
|
|
|
|
AnimationResource::~AnimationResource() {
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 19:38:40 +00:00
|
|
|
|
bool AnimationResource::precacheAllFrames() const {
|
2010-09-02 17:11:45 +00:00
|
|
|
|
Common::Array<Frame>::const_iterator iter = _frames.begin();
|
|
|
|
|
for (; iter != _frames.end(); ++iter) {
|
2011-01-28 16:13:12 +00:00
|
|
|
|
#ifdef PRECACHE_RESOURCES
|
2010-10-19 21:03:33 +00:00
|
|
|
|
if (!Kernel::getInstance()->getResourceManager()->precacheResource((*iter).fileName)) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Could not precache \"%s\".", (*iter).fileName.c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-28 16:13:12 +00:00
|
|
|
|
#else
|
|
|
|
|
Kernel::getInstance()->getResourceManager()->requestResource((*iter).fileName);
|
|
|
|
|
#endif
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 19:38:40 +00:00
|
|
|
|
bool AnimationResource::computeFeatures() {
|
2011-01-23 15:01:24 +00:00
|
|
|
|
assert(_frames.size());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
// Alle Features werden als vorhanden angenommen
|
2010-09-02 17:11:45 +00:00
|
|
|
|
_scalingAllowed = true;
|
|
|
|
|
_alphaAllowed = true;
|
|
|
|
|
_colorModulationAllowed = true;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
|
|
|
|
// Alle Frame durchgehen und alle Features deaktivieren, die auch nur von einem Frame nicht unterst<73>tzt werden.
|
2010-10-12 19:38:40 +00:00
|
|
|
|
Common::Array<Frame>::const_iterator iter = _frames.begin();
|
|
|
|
|
for (; iter != _frames.end(); ++iter) {
|
2010-08-18 10:52:24 +00:00
|
|
|
|
BitmapResource *pBitmap;
|
2010-10-19 21:03:33 +00:00
|
|
|
|
if (!(pBitmap = static_cast<BitmapResource *>(Kernel::getInstance()->getResourceManager()->requestResource((*iter).fileName)))) {
|
2011-01-23 14:49:50 +00:00
|
|
|
|
error("Could not request \"%s\".", (*iter).fileName.c_str());
|
2010-07-29 19:53:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
if (!pBitmap->isScalingAllowed())
|
|
|
|
|
_scalingAllowed = false;
|
|
|
|
|
if (!pBitmap->isAlphaAllowed())
|
|
|
|
|
_alphaAllowed = false;
|
|
|
|
|
if (!pBitmap->isColorModulationAllowed())
|
|
|
|
|
_colorModulationAllowed = false;
|
2010-07-29 19:53:02 +00:00
|
|
|
|
|
2010-09-02 17:11:45 +00:00
|
|
|
|
pBitmap->release();
|
2010-07-29 19:53:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-08-05 12:48:19 +00:00
|
|
|
|
|
|
|
|
|
} // End of namespace Sword25
|