~ubuntu-branches/ubuntu/raring/scummvm/raring

« back to all changes in this revision

Viewing changes to engines/sword25/gfx/animationtemplate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Moritz Muehlenhoff
  • Date: 2011-05-25 19:02:23 UTC
  • mto: (21.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20110525190223-fiqm0oaec714xk31
Tags: upstream-1.3.0
Import upstream version 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ScummVM - Graphic Adventure Engine
 
2
 *
 
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.
 
6
 *
 
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.
 
11
 
 
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.
 
16
 
 
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.
 
20
 *
 
21
 * $URL$
 
22
 * $Id$
 
23
 *
 
24
 */
 
25
 
 
26
/*
 
27
 * This code is based on Broken Sword 2.5 engine
 
28
 *
 
29
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 
30
 *
 
31
 * Licensed under GNU GPL v2
 
32
 *
 
33
 */
 
34
 
 
35
#include "sword25/kernel/kernel.h"
 
36
#include "sword25/kernel/resource.h"
 
37
#include "sword25/kernel/outputpersistenceblock.h"
 
38
#include "sword25/kernel/inputpersistenceblock.h"
 
39
 
 
40
#include "sword25/gfx/animationresource.h"
 
41
#include "sword25/gfx/animationtemplate.h"
 
42
#include "sword25/gfx/animationtemplateregistry.h"
 
43
 
 
44
namespace Sword25 {
 
45
 
 
46
uint AnimationTemplate::create(const Common::String &sourceAnimation) {
 
47
        AnimationTemplate *animationTemplatePtr = new AnimationTemplate(sourceAnimation);
 
48
 
 
49
        if (animationTemplatePtr->isValid()) {
 
50
                return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
 
51
        } else {
 
52
                delete animationTemplatePtr;
 
53
                return 0;
 
54
        }
 
55
}
 
56
 
 
57
uint AnimationTemplate::create(const AnimationTemplate &other) {
 
58
        AnimationTemplate *animationTemplatePtr = new AnimationTemplate(other);
 
59
 
 
60
        if (animationTemplatePtr->isValid()) {
 
61
                return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
 
62
        } else {
 
63
                delete animationTemplatePtr;
 
64
                return 0;
 
65
        }
 
66
}
 
67
 
 
68
uint AnimationTemplate::create(InputPersistenceBlock &reader, uint handle) {
 
69
        AnimationTemplate *animationTemplatePtr = new AnimationTemplate(reader, handle);
 
70
 
 
71
        if (animationTemplatePtr->isValid()) {
 
72
                return AnimationTemplateRegistry::instance().resolvePtr(animationTemplatePtr);
 
73
        } else {
 
74
                delete animationTemplatePtr;
 
75
                return 0;
 
76
        }
 
77
}
 
78
 
 
79
AnimationTemplate::AnimationTemplate(const Common::String &sourceAnimation) {
 
80
        // Objekt registrieren.
 
81
        AnimationTemplateRegistry::instance().registerObject(this);
 
82
 
 
83
        _valid = false;
 
84
 
 
85
        // Die Animations-Resource wird f�r die gesamte Lebensdauer des Objektes gelockt
 
86
        _sourceAnimationPtr = requestSourceAnimation(sourceAnimation);
 
87
 
 
88
        // Erfolg signalisieren
 
89
        _valid = (_sourceAnimationPtr != 0);
 
90
}
 
91
 
 
92
AnimationTemplate::AnimationTemplate(const AnimationTemplate &other) : AnimationDescription() {
 
93
        // Objekt registrieren.
 
94
        AnimationTemplateRegistry::instance().registerObject(this);
 
95
 
 
96
        _valid = false;
 
97
 
 
98
        // Die Animations-Resource wird f�r die gesamte Lebensdauer des Objektes gelockt.
 
99
        if (!other._sourceAnimationPtr)
 
100
                return;
 
101
        _sourceAnimationPtr = requestSourceAnimation(other._sourceAnimationPtr->getFileName());
 
102
 
 
103
        // Alle Member kopieren.
 
104
        _animationType = other._animationType;
 
105
        _FPS = other._FPS;
 
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;
 
113
 
 
114
        _valid &= (_sourceAnimationPtr != 0);
 
115
}
 
116
 
 
117
AnimationTemplate::AnimationTemplate(InputPersistenceBlock &reader, uint handle) {
 
118
        // Objekt registrieren.
 
119
        AnimationTemplateRegistry::instance().registerObject(this, handle);
 
120
 
 
121
        // Objekt laden.
 
122
        _valid = unpersist(reader);
 
123
}
 
124
 
 
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());
 
130
                return 0;
 
131
        }
 
132
        return static_cast<AnimationResource *>(resourcePtr);
 
133
}
 
134
 
 
135
AnimationTemplate::~AnimationTemplate() {
 
136
        // Animations-Resource freigeben
 
137
        if (_sourceAnimationPtr) {
 
138
                _sourceAnimationPtr->release();
 
139
        }
 
140
 
 
141
        // Objekt deregistrieren
 
142
        AnimationTemplateRegistry::instance().deregisterObject(this);
 
143
}
 
144
 
 
145
void AnimationTemplate::addFrame(int index) {
 
146
        if (validateSourceIndex(index)) {
 
147
                _frames.push_back(_sourceAnimationPtr->getFrame(index));
 
148
        }
 
149
}
 
150
 
 
151
void AnimationTemplate::setFrame(int destIndex, int srcIndex) {
 
152
        if (validateDestIndex(destIndex) && validateSourceIndex(srcIndex)) {
 
153
                _frames[destIndex] = _sourceAnimationPtr->getFrame(srcIndex);
 
154
        }
 
155
}
 
156
 
 
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());
 
161
                return false;
 
162
        } else
 
163
                return true;
 
164
}
 
165
 
 
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.",
 
169
                                 index);
 
170
                return false;
 
171
        } else
 
172
                return true;
 
173
}
 
174
 
 
175
void AnimationTemplate::setFPS(int FPS) {
 
176
        _FPS = FPS;
 
177
        _millisPerFrame = 1000000 / _FPS;
 
178
}
 
179
 
 
180
bool AnimationTemplate::persist(OutputPersistenceBlock &writer) {
 
181
        bool Result = true;
 
182
 
 
183
        // Parent persistieren.
 
184
        Result &= AnimationDescription::persist(writer);
 
185
 
 
186
        // Frameanzahl schreiben.
 
187
        writer.write(_frames.size());
 
188
 
 
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);
 
198
                ++Iter;
 
199
        }
 
200
 
 
201
        // Restliche Member persistieren.
 
202
        writer.writeString(_sourceAnimationPtr->getFileName());
 
203
        writer.write(_valid);
 
204
 
 
205
        return Result;
 
206
}
 
207
 
 
208
bool AnimationTemplate::unpersist(InputPersistenceBlock &reader) {
 
209
        bool result = true;
 
210
 
 
211
        // Parent wieder herstellen.
 
212
        result &= AnimationDescription::unpersist(reader);
 
213
 
 
214
        // Frameanzahl lesen.
 
215
        uint frameCount;
 
216
        reader.read(frameCount);
 
217
 
 
218
        // Frames einzeln wieder herstellen.
 
219
        for (uint i = 0; i < frameCount; ++i) {
 
220
                Frame frame;
 
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);
 
227
 
 
228
                _frames.push_back(frame);
 
229
        }
 
230
 
 
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);
 
235
 
 
236
        reader.read(_valid);
 
237
 
 
238
        return _sourceAnimationPtr && reader.isGood() && result;
 
239
}
 
240
 
 
241
} // End of namespace Sword25