1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
* This code is based on Broken Sword 2.5 engine
29
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
31
* Licensed under GNU GPL v2
35
#include "sword25/kernel/kernel.h"
36
#include "sword25/kernel/resource.h"
37
#include "sword25/kernel/outputpersistenceblock.h"
38
#include "sword25/kernel/inputpersistenceblock.h"
40
#include "sword25/gfx/animationresource.h"
41
#include "sword25/gfx/animationtemplate.h"
42
#include "sword25/gfx/animationtemplateregistry.h"
46
uint AnimationTemplate::create(const Common::String &sourceAnimation) {
47
AnimationTemplate *animationTemplatePtr = new AnimationTemplate(sourceAnimation);
49
if (animationTemplatePtr->isValid()) {
50
return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
52
delete animationTemplatePtr;
57
uint AnimationTemplate::create(const AnimationTemplate &other) {
58
AnimationTemplate *animationTemplatePtr = new AnimationTemplate(other);
60
if (animationTemplatePtr->isValid()) {
61
return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
63
delete animationTemplatePtr;
68
uint AnimationTemplate::create(InputPersistenceBlock &reader, uint handle) {
69
AnimationTemplate *animationTemplatePtr = new AnimationTemplate(reader, handle);
71
if (animationTemplatePtr->isValid()) {
72
return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
74
delete animationTemplatePtr;
79
AnimationTemplate::AnimationTemplate(const Common::String &sourceAnimation) {
80
// Objekt registrieren.
81
AnimationTemplateRegistry::instance().registerObject(this);
85
// Die Animations-Resource wird f�r die gesamte Lebensdauer des Objektes gelockt
86
_sourceAnimationPtr = requestSourceAnimation(sourceAnimation);
88
// Erfolg signalisieren
89
_valid = (_sourceAnimationPtr != 0);
92
AnimationTemplate::AnimationTemplate(const AnimationTemplate &other) : AnimationDescription() {
93
// Objekt registrieren.
94
AnimationTemplateRegistry::instance().registerObject(this);
98
// Die Animations-Resource wird f�r die gesamte Lebensdauer des Objektes gelockt.
99
if (!other._sourceAnimationPtr)
101
_sourceAnimationPtr = requestSourceAnimation(other._sourceAnimationPtr->getFileName());
103
// Alle Member kopieren.
104
_animationType = other._animationType;
106
_millisPerFrame = other._millisPerFrame;
107
_scalingAllowed = other._scalingAllowed;
108
_alphaAllowed = other._alphaAllowed;
109
_colorModulationAllowed = other._colorModulationAllowed;
110
_frames = other._frames;
111
_sourceAnimationPtr = other._sourceAnimationPtr;
112
_valid = other._valid;
114
_valid &= (_sourceAnimationPtr != 0);
117
AnimationTemplate::AnimationTemplate(InputPersistenceBlock &reader, uint handle) {
118
// Objekt registrieren.
119
AnimationTemplateRegistry::instance().registerObject(this, handle);
122
_valid = unpersist(reader);
125
AnimationResource *AnimationTemplate::requestSourceAnimation(const Common::String &sourceAnimation) const {
126
ResourceManager *RMPtr = Kernel::getInstance()->getResourceManager();
127
Resource *resourcePtr;
128
if (NULL == (resourcePtr = RMPtr->requestResource(sourceAnimation)) || resourcePtr->getType() != Resource::TYPE_ANIMATION) {
129
error("The resource \"%s\" could not be requested or is has an invalid type. The animation template can't be created.", sourceAnimation.c_str());
132
return static_cast<AnimationResource *>(resourcePtr);
135
AnimationTemplate::~AnimationTemplate() {
136
// Animations-Resource freigeben
137
if (_sourceAnimationPtr) {
138
_sourceAnimationPtr->release();
141
// Objekt deregistrieren
142
AnimationTemplateRegistry::instance().deregisterObject(this);
145
void AnimationTemplate::addFrame(int index) {
146
if (validateSourceIndex(index)) {
147
_frames.push_back(_sourceAnimationPtr->getFrame(index));
151
void AnimationTemplate::setFrame(int destIndex, int srcIndex) {
152
if (validateDestIndex(destIndex) && validateSourceIndex(srcIndex)) {
153
_frames[destIndex] = _sourceAnimationPtr->getFrame(srcIndex);
157
bool AnimationTemplate::validateSourceIndex(uint index) const {
158
if (index > _sourceAnimationPtr->getFrameCount()) {
159
warning("Tried to insert a frame (\"%d\") that does not exist in the source animation (\"%s\"). Ignoring call.",
160
index, _sourceAnimationPtr->getFileName().c_str());
166
bool AnimationTemplate::validateDestIndex(uint index) const {
167
if (index > _frames.size()) {
168
warning("Tried to change a nonexistent frame (\"%d\") in a template animation. Ignoring call.",
175
void AnimationTemplate::setFPS(int FPS) {
177
_millisPerFrame = 1000000 / _FPS;
180
bool AnimationTemplate::persist(OutputPersistenceBlock &writer) {
183
// Parent persistieren.
184
Result &= AnimationDescription::persist(writer);
186
// Frameanzahl schreiben.
187
writer.write(_frames.size());
189
// Frames einzeln persistieren.
190
Common::Array<const Frame>::const_iterator Iter = _frames.begin();
191
while (Iter != _frames.end()) {
192
writer.write(Iter->hotspotX);
193
writer.write(Iter->hotspotY);
194
writer.write(Iter->flipV);
195
writer.write(Iter->flipH);
196
writer.writeString(Iter->fileName);
197
writer.writeString(Iter->action);
201
// Restliche Member persistieren.
202
writer.writeString(_sourceAnimationPtr->getFileName());
203
writer.write(_valid);
208
bool AnimationTemplate::unpersist(InputPersistenceBlock &reader) {
211
// Parent wieder herstellen.
212
result &= AnimationDescription::unpersist(reader);
214
// Frameanzahl lesen.
216
reader.read(frameCount);
218
// Frames einzeln wieder herstellen.
219
for (uint i = 0; i < frameCount; ++i) {
221
reader.read(frame.hotspotX);
222
reader.read(frame.hotspotY);
223
reader.read(frame.flipV);
224
reader.read(frame.flipH);
225
reader.readString(frame.fileName);
226
reader.readString(frame.action);
228
_frames.push_back(frame);
231
// Die Animations-Resource wird f�r die gesamte Lebensdauer des Objektes gelockt
232
Common::String sourceAnimation;
233
reader.readString(sourceAnimation);
234
_sourceAnimationPtr = requestSourceAnimation(sourceAnimation);
238
return _sourceAnimationPtr && reader.isGood() && result;
241
} // End of namespace Sword25