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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/* 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.
*
* $URL$
* $Id$
*
*/
/*
* This code is based on Broken Sword 2.5 engine
*
* Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
*
* Licensed under GNU GPL v2
*
*/
#include "sword25/gfx/animation.h"
#include "sword25/kernel/kernel.h"
#include "sword25/kernel/resmanager.h"
#include "sword25/kernel/inputpersistenceblock.h"
#include "sword25/kernel/outputpersistenceblock.h"
#include "sword25/kernel/callbackregistry.h"
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/image.h"
#include "sword25/gfx/animationtemplate.h"
#include "sword25/gfx/animationtemplateregistry.h"
#include "sword25/gfx/animationresource.h"
#include "sword25/gfx/bitmapresource.h"
#include "sword25/gfx/graphicengine.h"
namespace Sword25 {
#define BS_LOG_PREFIX "ANIMATION"
Animation::Animation(RenderObjectPtr<RenderObject> parentPtr, const Common::String &fileName) :
TimedRenderObject(parentPtr, RenderObject::TYPE_ANIMATION) {
// Das BS_RenderObject konnte nicht erzeugt werden, daher muss an dieser Stelle abgebrochen werden.
if (!_initSuccess)
return;
initMembers();
// Vom negativen Fall ausgehen.
_initSuccess = false;
initializeAnimationResource(fileName);
// Erfolg signalisieren.
_initSuccess = true;
}
Animation::Animation(RenderObjectPtr<RenderObject> parentPtr, const AnimationTemplate &templ) :
TimedRenderObject(parentPtr, RenderObject::TYPE_ANIMATION) {
// Das BS_RenderObject konnte nicht erzeugt werden, daher muss an dieser Stelle abgebrochen werden.
if (!_initSuccess)
return;
initMembers();
// Vom negativen Fall ausgehen.
_initSuccess = false;
_animationTemplateHandle = AnimationTemplate::create(templ);
// Erfolg signalisieren.
_initSuccess = true;
}
Animation::Animation(InputPersistenceBlock &reader, RenderObjectPtr<RenderObject> parentPtr, uint handle) :
TimedRenderObject(parentPtr, RenderObject::TYPE_ANIMATION, handle) {
// Das BS_RenderObject konnte nicht erzeugt werden, daher muss an dieser Stelle abgebrochen werden.
if (!_initSuccess)
return;
initMembers();
// Objekt vom Stream laden.
_initSuccess = unpersist(reader);
}
void Animation::initializeAnimationResource(const Common::String &fileName) {
// Die Resource wird f�r die gesamte Lebensdauer des Animations-Objektes gelockt.
Resource *resourcePtr = Kernel::GetInstance()->GetResourceManager()->RequestResource(fileName);
if (resourcePtr && resourcePtr->GetType() == Resource::TYPE_ANIMATION)
_animationResourcePtr = static_cast<AnimationResource *>(resourcePtr);
else {
BS_LOG_ERRORLN("The resource \"%s\" could not be requested. The Animation can't be created.", fileName.c_str());
return;
}
// Gr��e und Position der Animation anhand des aktuellen Frames bestimmen.
computeCurrentCharacteristics();
}
void Animation::initMembers() {
_currentFrame = 0;
_currentFrameTime = 0;
_direction = FORWARD;
_running = false;
_finished = false;
_relX = 0;
_relY = 0;
_scaleFactorX = 1.0f;
_scaleFactorY = 1.0f;
_modulationColor = 0xffffffff;
_animationResourcePtr = 0;
_animationTemplateHandle = 0;
_framesLocked = false;
}
Animation::~Animation() {
if (getAnimationDescription()) {
stop();
getAnimationDescription()->unlock();
}
// Delete Callbacks
Common::Array<ANIMATION_CALLBACK_DATA>::iterator it = _deleteCallbacks.begin();
for (; it != _deleteCallbacks.end(); it++)((*it).Callback)((*it).Data);
}
void Animation::play() {
// Wenn die Animation zuvor komplett durchgelaufen ist, wird sie wieder von Anfang abgespielt
if (_finished)
stop();
_running = true;
lockAllFrames();
}
void Animation::pause() {
_running = false;
unlockAllFrames();
}
void Animation::stop() {
_currentFrame = 0;
_currentFrameTime = 0;
_direction = FORWARD;
pause();
}
void Animation::setFrame(uint nr) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
if (nr >= animationDescriptionPtr->getFrameCount()) {
BS_LOG_ERRORLN("Tried to set animation to illegal frame (%d). Value must be between 0 and %d.",
nr, animationDescriptionPtr->getFrameCount());
return;
}
_currentFrame = nr;
_currentFrameTime = 0;
computeCurrentCharacteristics();
forceRefresh();
}
bool Animation::doRender() {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
BS_ASSERT(_currentFrame < animationDescriptionPtr->getFrameCount());
// Bitmap des aktuellen Frames holen
Resource *pResource = Kernel::GetInstance()->GetResourceManager()->RequestResource(animationDescriptionPtr->getFrame(_currentFrame).fileName);
BS_ASSERT(pResource);
BS_ASSERT(pResource->GetType() == Resource::TYPE_BITMAP);
BitmapResource *pBitmapResource = static_cast<BitmapResource *>(pResource);
// Framebufferobjekt holen
GraphicEngine *pGfx = static_cast<GraphicEngine *>(Kernel::GetInstance()->GetService("gfx"));
BS_ASSERT(pGfx);
// Bitmap zeichnen
bool result;
if (isScalingAllowed() && (_width != pBitmapResource->getWidth() || _height != pBitmapResource->getHeight())) {
result = pBitmapResource->blit(_absoluteX, _absoluteY,
(animationDescriptionPtr->getFrame(_currentFrame).flipV ? BitmapResource::FLIP_V : 0) |
(animationDescriptionPtr->getFrame(_currentFrame).flipH ? BitmapResource::FLIP_H : 0),
0, _modulationColor, _width, _height);
} else {
result = pBitmapResource->blit(_absoluteX, _absoluteY,
(animationDescriptionPtr->getFrame(_currentFrame).flipV ? BitmapResource::FLIP_V : 0) |
(animationDescriptionPtr->getFrame(_currentFrame).flipH ? BitmapResource::FLIP_H : 0),
0, _modulationColor, -1, -1);
}
// Resource freigeben
pBitmapResource->release();
return result;
}
void Animation::frameNotification(int timeElapsed) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
BS_ASSERT(timeElapsed >= 0);
// Nur wenn die Animation l�uft wird sie auch weiterbewegt
if (_running) {
// Gesamte vergangene Zeit bestimmen (inkl. Restzeit des aktuellen Frames)
_currentFrameTime += timeElapsed;
// Anzahl an zu �berpringenden Frames bestimmen
int skipFrames = animationDescriptionPtr->getMillisPerFrame() == 0 ? 0 : _currentFrameTime / animationDescriptionPtr->getMillisPerFrame();
// Neue Frame-Restzeit bestimmen
_currentFrameTime -= animationDescriptionPtr->getMillisPerFrame() * skipFrames;
// Neuen Frame bestimmen (je nach aktuellener Abspielrichtung wird addiert oder subtrahiert)
int tmpCurFrame = _currentFrame;
switch (_direction) {
case FORWARD:
tmpCurFrame += skipFrames;
break;
case BACKWARD:
tmpCurFrame -= skipFrames;
break;
default:
BS_ASSERT(0);
}
// �berl�ufe behandeln
if (tmpCurFrame < 0) {
// Loop-Point Callbacks
for (uint i = 0; i < _loopPointCallbacks.size();) {
if ((_loopPointCallbacks[i].Callback)(_loopPointCallbacks[i].Data) == false) {
_loopPointCallbacks.remove_at(i);
} else
i++;
}
// Ein Unterlauf darf nur auftreten, wenn der Animationstyp JOJO ist.
BS_ASSERT(animationDescriptionPtr->getAnimationType() == AT_JOJO);
tmpCurFrame = - tmpCurFrame;
_direction = FORWARD;
} else if (static_cast<uint>(tmpCurFrame) >= animationDescriptionPtr->getFrameCount()) {
// Loop-Point Callbacks
for (uint i = 0; i < _loopPointCallbacks.size();) {
if ((_loopPointCallbacks[i].Callback)(_loopPointCallbacks[i].Data) == false) {
_loopPointCallbacks.remove_at(i);
} else
i++;
}
switch (animationDescriptionPtr->getAnimationType()) {
case AT_ONESHOT:
tmpCurFrame = animationDescriptionPtr->getFrameCount() - 1;
_finished = true;
pause();
break;
case AT_LOOP:
tmpCurFrame = tmpCurFrame % animationDescriptionPtr->getFrameCount();
break;
case AT_JOJO:
tmpCurFrame = animationDescriptionPtr->getFrameCount() - (tmpCurFrame % animationDescriptionPtr->getFrameCount()) - 1;
_direction = BACKWARD;
break;
default:
BS_ASSERT(0);
}
}
if ((int)_currentFrame != tmpCurFrame) {
forceRefresh();
if (animationDescriptionPtr->getFrame(_currentFrame).action != "") {
// Action Callbacks
for (uint i = 0; i < _actionCallbacks.size();) {
if ((_actionCallbacks[i].Callback)(_actionCallbacks[i].Data) == false) {
_actionCallbacks.remove_at(i);
} else
i++;
}
}
}
_currentFrame = static_cast<uint>(tmpCurFrame);
}
// Gr��e und Position der Animation anhand des aktuellen Frames bestimmen
computeCurrentCharacteristics();
BS_ASSERT(_currentFrame < animationDescriptionPtr->getFrameCount());
BS_ASSERT(_currentFrameTime >= 0);
}
void Animation::computeCurrentCharacteristics() {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
const AnimationResource::Frame &curFrame = animationDescriptionPtr->getFrame(_currentFrame);
Resource *pResource = Kernel::GetInstance()->GetResourceManager()->RequestResource(curFrame.fileName);
BS_ASSERT(pResource);
BS_ASSERT(pResource->GetType() == Resource::TYPE_BITMAP);
BitmapResource *pBitmap = static_cast<BitmapResource *>(pResource);
// Gr��e des Bitmaps auf die Animation �bertragen
_width = static_cast<int>(pBitmap->getWidth() * _scaleFactorX);
_height = static_cast<int>(pBitmap->getHeight() * _scaleFactorY);
// Position anhand des Hotspots berechnen und setzen
int posX = _relX + computeXModifier();
int posY = _relY + computeYModifier();
RenderObject::setPos(posX, posY);
pBitmap->release();
}
bool Animation::lockAllFrames() {
if (!_framesLocked) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
for (uint i = 0; i < animationDescriptionPtr->getFrameCount(); ++i) {
if (!Kernel::GetInstance()->GetResourceManager()->RequestResource(animationDescriptionPtr->getFrame(i).fileName)) {
BS_LOG_ERRORLN("Could not lock all animation frames.");
return false;
}
}
_framesLocked = true;
}
return true;
}
bool Animation::unlockAllFrames() {
if (_framesLocked) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
for (uint i = 0; i < animationDescriptionPtr->getFrameCount(); ++i) {
Resource *pResource;
if (!(pResource = Kernel::GetInstance()->GetResourceManager()->RequestResource(animationDescriptionPtr->getFrame(i).fileName))) {
BS_LOG_ERRORLN("Could not unlock all animation frames.");
return false;
}
// Zwei mal freigeben um den Request von LockAllFrames() und den jetzigen Request aufzuheben
pResource->release();
if (pResource->GetLockCount())
pResource->release();
}
_framesLocked = false;
}
return true;
}
Animation::ANIMATION_TYPES Animation::getAnimationType() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->getAnimationType();
}
int Animation::getFPS() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->getFPS();
}
int Animation::getFrameCount() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->getFrameCount();
}
bool Animation::isScalingAllowed() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->isScalingAllowed();
}
bool Animation::isAlphaAllowed() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->isAlphaAllowed();
}
bool Animation::isColorModulationAllowed() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->isColorModulationAllowed();
}
void Animation::setPos(int relX, int relY) {
_relX = relX;
_relY = relY;
computeCurrentCharacteristics();
}
void Animation::setX(int relX) {
_relX = relX;
computeCurrentCharacteristics();
}
void Animation::setY(int relY) {
_relY = relY;
computeCurrentCharacteristics();
}
void Animation::setAlpha(int alpha) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
if (!animationDescriptionPtr->isAlphaAllowed()) {
BS_LOG_WARNINGLN("Tried to set alpha value on an animation that does not support alpha. Call was ignored.");
return;
}
uint newModulationColor = (_modulationColor & 0x00ffffff) | alpha << 24;
if (newModulationColor != _modulationColor) {
_modulationColor = newModulationColor;
forceRefresh();
}
}
void Animation::setModulationColor(uint modulationColor) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
if (!animationDescriptionPtr->isColorModulationAllowed()) {
BS_LOG_WARNINGLN("Tried to set modulation color on an animation that does not support color modulation. Call was ignored");
return;
}
uint newModulationColor = (modulationColor & 0x00ffffff) | (_modulationColor & 0xff000000);
if (newModulationColor != _modulationColor) {
_modulationColor = newModulationColor;
forceRefresh();
}
}
void Animation::setScaleFactor(float scaleFactor) {
setScaleFactorX(scaleFactor);
setScaleFactorY(scaleFactor);
}
void Animation::setScaleFactorX(float scaleFactorX) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
if (!animationDescriptionPtr->isScalingAllowed()) {
BS_LOG_WARNINGLN("Tried to set x scale factor on an animation that does not support scaling. Call was ignored");
return;
}
if (scaleFactorX != _scaleFactorX) {
_scaleFactorX = scaleFactorX;
if (_scaleFactorX <= 0.0f)
_scaleFactorX = 0.001f;
forceRefresh();
computeCurrentCharacteristics();
}
}
void Animation::setScaleFactorY(float scaleFactorY) {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
if (!animationDescriptionPtr->isScalingAllowed()) {
BS_LOG_WARNINGLN("Tried to set y scale factor on an animation that does not support scaling. Call was ignored");
return;
}
if (scaleFactorY != _scaleFactorY) {
_scaleFactorY = scaleFactorY;
if (_scaleFactorY <= 0.0f)
_scaleFactorY = 0.001f;
forceRefresh();
computeCurrentCharacteristics();
}
}
const Common::String &Animation::getCurrentAction() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
return animationDescriptionPtr->getFrame(_currentFrame).action;
}
int Animation::getX() const {
return _relX;
}
int Animation::getY() const {
return _relY;
}
int Animation::getAbsoluteX() const {
return _absoluteX + (_relX - _x);
}
int Animation::getAbsoluteY() const {
return _absoluteY + (_relY - _y);
}
int Animation::computeXModifier() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
const AnimationResource::Frame &curFrame = animationDescriptionPtr->getFrame(_currentFrame);
Resource *pResource = Kernel::GetInstance()->GetResourceManager()->RequestResource(curFrame.fileName);
BS_ASSERT(pResource);
BS_ASSERT(pResource->GetType() == Resource::TYPE_BITMAP);
BitmapResource *pBitmap = static_cast<BitmapResource *>(pResource);
int result = curFrame.flipV ? - static_cast<int>((pBitmap->getWidth() - 1 - curFrame.hotspotX) * _scaleFactorX) :
- static_cast<int>(curFrame.hotspotX * _scaleFactorX);
pBitmap->release();
return result;
}
int Animation::computeYModifier() const {
AnimationDescription *animationDescriptionPtr = getAnimationDescription();
BS_ASSERT(animationDescriptionPtr);
const AnimationResource::Frame &curFrame = animationDescriptionPtr->getFrame(_currentFrame);
Resource *pResource = Kernel::GetInstance()->GetResourceManager()->RequestResource(curFrame.fileName);
BS_ASSERT(pResource);
BS_ASSERT(pResource->GetType() == Resource::TYPE_BITMAP);
BitmapResource *pBitmap = static_cast<BitmapResource *>(pResource);
int result = curFrame.flipH ? - static_cast<int>((pBitmap->getHeight() - 1 - curFrame.hotspotY) * _scaleFactorY) :
- static_cast<int>(curFrame.hotspotY * _scaleFactorY);
pBitmap->release();
return result;
}
void Animation::registerActionCallback(ANIMATION_CALLBACK callback, uint data) {
ANIMATION_CALLBACK_DATA cd;
cd.Callback = callback;
cd.Data = data;
_actionCallbacks.push_back(cd);
}
void Animation::registerLoopPointCallback(ANIMATION_CALLBACK callback, uint data) {
ANIMATION_CALLBACK_DATA cd;
cd.Callback = callback;
cd.Data = data;
_loopPointCallbacks.push_back(cd);
}
void Animation::registerDeleteCallback(ANIMATION_CALLBACK callback, uint data) {
ANIMATION_CALLBACK_DATA cd;
cd.Callback = callback;
cd.Data = data;
_deleteCallbacks.push_back(cd);
}
void Animation::persistCallbackVector(OutputPersistenceBlock &writer, const Common::Array<ANIMATION_CALLBACK_DATA> &vector) {
// Anzahl an Callbacks persistieren.
writer.write(vector.size());
// Alle Callbacks einzeln persistieren.
Common::Array<ANIMATION_CALLBACK_DATA>::const_iterator it = vector.begin();
while (it != vector.end()) {
writer.write(CallbackRegistry::getInstance().resolveCallbackPointer((void (*)(int))it->Callback));
writer.write(it->Data);
++it;
}
}
void Animation::unpersistCallbackVector(InputPersistenceBlock &reader, Common::Array<ANIMATION_CALLBACK_DATA> &vector) {
// Callbackvector leeren.
vector.resize(0);
// Anzahl an Callbacks einlesen.
uint callbackCount;
reader.read(callbackCount);
// Alle Callbacks einzeln wieder herstellen.
for (uint i = 0; i < callbackCount; ++i) {
ANIMATION_CALLBACK_DATA callbackData;
Common::String callbackFunctionName;
reader.read(callbackFunctionName);
callbackData.Callback = reinterpret_cast<ANIMATION_CALLBACK>(CallbackRegistry::getInstance().resolveCallbackFunction(callbackFunctionName));
reader.read(callbackData.Data);
vector.push_back(callbackData);
}
}
bool Animation::persist(OutputPersistenceBlock &writer) {
bool result = true;
result &= RenderObject::persist(writer);
writer.write(_relX);
writer.write(_relY);
writer.write(_scaleFactorX);
writer.write(_scaleFactorY);
writer.write(_modulationColor);
writer.write(_currentFrame);
writer.write(_currentFrameTime);
writer.write(_running);
writer.write(_finished);
writer.write(static_cast<uint>(_direction));
// Je nach Animationstyp entweder das Template oder die Ressource speichern.
if (_animationResourcePtr) {
uint marker = 0;
writer.write(marker);
writer.write(_animationResourcePtr->getFileName());
} else if (_animationTemplateHandle) {
uint marker = 1;
writer.write(marker);
writer.write(_animationTemplateHandle);
} else {
BS_ASSERT(false);
}
//writer.write(_AnimationDescriptionPtr);
writer.write(_framesLocked);
persistCallbackVector(writer, _loopPointCallbacks);
persistCallbackVector(writer, _actionCallbacks);
persistCallbackVector(writer, _deleteCallbacks);
result &= RenderObject::persistChildren(writer);
return result;
}
// -----------------------------------------------------------------------------
bool Animation::unpersist(InputPersistenceBlock &reader) {
bool result = true;
result &= RenderObject::unpersist(reader);
reader.read(_relX);
reader.read(_relY);
reader.read(_scaleFactorX);
reader.read(_scaleFactorY);
reader.read(_modulationColor);
reader.read(_currentFrame);
reader.read(_currentFrameTime);
reader.read(_running);
reader.read(_finished);
uint direction;
reader.read(direction);
_direction = static_cast<Direction>(direction);
// Animationstyp einlesen.
uint marker;
reader.read(marker);
if (marker == 0) {
Common::String resourceFilename;
reader.read(resourceFilename);
initializeAnimationResource(resourceFilename);
} else if (marker == 1) {
reader.read(_animationTemplateHandle);
} else {
BS_ASSERT(false);
}
reader.read(_framesLocked);
if (_framesLocked)
lockAllFrames();
unpersistCallbackVector(reader, _loopPointCallbacks);
unpersistCallbackVector(reader, _actionCallbacks);
unpersistCallbackVector(reader, _deleteCallbacks);
result &= RenderObject::unpersistChildren(reader);
return reader.isGood() && result;
}
// -----------------------------------------------------------------------------
AnimationDescription *Animation::getAnimationDescription() const {
if (_animationResourcePtr)
return _animationResourcePtr;
else
return AnimationTemplateRegistry::getInstance().resolveHandle(_animationTemplateHandle);
}
} // End of namespace Sword25
|