forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnim2D.cpp
More file actions
865 lines (650 loc) · 27.9 KB
/
Anim2D.cpp
File metadata and controls
865 lines (650 loc) · 27.9 KB
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** 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 3 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, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: Anim2D.cpp ///////////////////////////////////////////////////////////////////////////////
// Author: Colin Day, July 2002
// Desc: A collection of 2D images to make animation
///////////////////////////////////////////////////////////////////////////////////////////////////
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#define DEFINE_ANIM_2D_MODE_NAMES
#include "Common/RandomValue.h"
#include "Common/Xfer.h"
#include "GameClient/Anim2D.h"
#include "GameClient/Display.h"
#include "GameClient/Image.h"
#include "GameLogic/GameLogic.h"
// GLOBAL /////////////////////////////////////////////////////////////////////////////////////////
Anim2DCollection *TheAnim2DCollection = nullptr;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2DTemplate::Anim2DTemplate( AsciiString name )
{
m_name = name;
m_images = nullptr;
m_numFrames = NUM_FRAMES_INVALID;
m_framesBetweenUpdates = 0;
m_animMode = ANIM_2D_LOOP;
m_randomizeStartFrame = FALSE;
m_nextTemplate = nullptr;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2DTemplate::~Anim2DTemplate()
{
delete [] m_images;
}
// ------------------------------------------------------------------------------------------------
/** Field parse table for 2D animation templates */
// ---------i---------------------------------------------------------------------------------------
const FieldParse Anim2DTemplate::s_anim2DFieldParseTable[] =
{
{ "NumberImages", Anim2DTemplate::parseNumImages, nullptr, 0 },
{ "Image", Anim2DTemplate::parseImage, nullptr, 0 },
{ "ImageSequence", Anim2DTemplate::parseImageSequence, nullptr, 0 },
{ "AnimationMode", INI::parseIndexList, Anim2DModeNames, offsetof( Anim2DTemplate, m_animMode ) },
{ "AnimationDelay", INI::parseDurationUnsignedShort, nullptr, offsetof( Anim2DTemplate, m_framesBetweenUpdates ) },
{ "RandomizeStartFrame", INI::parseBool, nullptr, offsetof( Anim2DTemplate, m_randomizeStartFrame ) },
{ nullptr, nullptr, nullptr, 0 }
};
// ------------------------------------------------------------------------------------------------
/** Parse the number of images we will have in this animation and allocate the array for them */
// ------------------------------------------------------------------------------------------------
void Anim2DTemplate::parseNumImages( INI *ini, void *instance, void *store, const void *userData )
{
// parse the integer data from the file
UnsignedInt numFrames;
ini->parseUnsignedInt( ini, instance, &numFrames, userData );
// get the template we are to store into
Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
// animations must have a minimum # of frames
Int minimumFrames = 1;
if( numFrames < minimumFrames )
{
DEBUG_CRASH(( "Anim2DTemplate::parseNumImages - Invalid animation '%s', animations must have '%d' or more frames defined",
animTemplate->getName().str(), minimumFrames ));
throw INI_INVALID_DATA;
}
// allocate the image array
animTemplate->allocateImages( (UnsignedShort)numFrames );
}
// ------------------------------------------------------------------------------------------------
/** Allocate the image array for an animation template and store the number of frames we have */
// ------------------------------------------------------------------------------------------------
void Anim2DTemplate::allocateImages( UnsignedShort numFrames )
{
// store the number of frames
m_numFrames = numFrames;
// allocate an array to hold the image pointers
m_images = NEW const Image *[ m_numFrames ]; // pool[]ify
// set all the images to nullptr;
for( Int i = 0; i < m_numFrames; ++i )
m_images[ i ] = nullptr;
}
// ------------------------------------------------------------------------------------------------
/** Parsing a single image definition for an animation */
// ------------------------------------------------------------------------------------------------
void Anim2DTemplate::parseImage( INI *ini, void *instance, void *store, const void *userData )
{
// parse the image name from the file and store as an image pointer
const Image *image;
ini->parseMappedImage( ini, instance, &image, userData );
// sanity
if( image == nullptr )
{
//We don't care if we're in the builder
//DEBUG_CRASH(( "Anim2DTemplate::parseImage - Image not found" ));
//throw INI_INVALID_DATA;
}
//
// assign the image to the animation template list of images ... note since we've pre-allocated
// the array of images and the index an image is loaded into depends on its order specified
// in INI, we need to get the number of images currently loaded into this animation template
// so that we can put it at the next free image spot ... and then tell the animation
// template we've loaded one more
//
Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
animTemplate->storeImage( image );
}
// ------------------------------------------------------------------------------------------------
/** This will parse the image sequence of an animation. You can use this as a shortcut to
* specifying a series of images for an animation instead of having to specify them all
* individually. Image names will be assumed to start with an appended "000" to the end
* end of the first image name and incremented up to the number of images for the
* animation. NOTE: That the number images *must* have already been specified before
* we can parse this entry so we know how many images to allocate and look for */
// ------------------------------------------------------------------------------------------------
/*static*/ void Anim2DTemplate::parseImageSequence( INI *ini, void *instance,
void *store, const void *userData )
{
// get the animation template
Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
//
// before we can read, allocate, and find all the images for the sequence ... we must
// know how many total images are in this animation. Verify that now
//
if( animTemplate->getNumFrames() == NUM_FRAMES_INVALID )
{
DEBUG_CRASH(( "Anim2DTemplate::parseImageSequence - You must specify the number of animation frames for animation '%s' *BEFORE* specifying the image sequence name",
animTemplate->getName().str() ));
throw INI_INVALID_DATA;
}
//
// the image storage has already been allocated, all we have to do now is count from
// 0 to numImages - 1 and add those images to the template
//
AsciiString imageBaseName = ini->getNextAsciiString();
AsciiString imageName;
const Image *image;
for( Int i = 0; i < animTemplate->getNumFrames(); ++i )
{
// construct this name
imageName.format( "%s%03d", imageBaseName.str(), i );
// search for this image
image = TheMappedImageCollection->findImageByName( imageName );
// sanity
if( image == nullptr )
{
DEBUG_CRASH(( "Anim2DTemplate::parseImageSequence - Image '%s' not found for animation '%s'. Check the number of images specified in INI and also make sure all the actual images exist.",
imageName.str(), animTemplate->getName().str() ));
throw INI_INVALID_DATA;
}
// store the image in the next free frame
animTemplate->storeImage( image );
}
}
// ------------------------------------------------------------------------------------------------
/** Store the image at the next open image slot for the animation */
// ------------------------------------------------------------------------------------------------
void Anim2DTemplate::storeImage( const Image *image )
{
// sanity
if( image == nullptr )
return;
// search through the image list and store at the next free spot
for( Int i = 0; i < m_numFrames; ++i )
{
if( m_images[ i ] == nullptr )
{
m_images[ i ] = image;
return;
}
}
// if we got here we tried to store an image in an array that was too small
DEBUG_CRASH(( "Anim2DTemplate::storeImage - Unable to store image '%s' into animation '%s' because the animation is setup to only support '%d' image frames",
image->getName().str(), getName().str(), m_numFrames ));
throw INI_INVALID_DATA;
}
// ------------------------------------------------------------------------------------------------
/** Return the Image* for the frame number requested */
// ------------------------------------------------------------------------------------------------
const Image* Anim2DTemplate::getFrame( UnsignedShort frameNumber ) const
{
// sanity
DEBUG_ASSERTCRASH( m_images != nullptr,
("Anim2DTemplate::getFrame - Image data is null for animation '%s'",
getName().str()) );
// sanity
if( frameNumber < 0 || frameNumber >= m_numFrames )
{
DEBUG_CRASH(( "Anim2DTemplate::getFrame - Illegal frame number '%d' for animation '%s'",
frameNumber, getName().str() ));
return nullptr;
}
else
{
// return the image frame
return m_images[ frameNumber ];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2D::Anim2D( Anim2DTemplate *animTemplate, Anim2DCollection *collectionSystem )
{
// sanity
DEBUG_ASSERTCRASH( animTemplate != nullptr, ("Anim2D::Anim2D - null template") );
m_currentFrame = 0;
// set the template
m_template = animTemplate;
// initialize members
m_status = ANIM_2D_STATUS_NONE;
m_alpha = 1.0f;
// set the initial frame for the animation based on the type of animation mode or randomize
if( m_template->isRandomizedStartFrame() )
randomizeCurrentFrame();
else
reset();
m_minFrame = 0;
m_maxFrame = m_template->getNumFrames() - 1;
m_framesBetweenUpdates = m_template->getNumFramesBetweenUpdates();
// we register ourselves to the System
m_collectionSystemNext = nullptr;
m_collectionSystemPrev = nullptr;
m_lastUpdateFrame = 0;
// if a system is present, register ourselves with that system
m_collectionSystem = collectionSystem;
if( m_collectionSystem )
m_collectionSystem->registerAnimation( this );
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2D::~Anim2D()
{
// if we were registered with a system, un-register ourselves
if( m_collectionSystem )
m_collectionSystem->unRegisterAnimation( this );
}
// ------------------------------------------------------------------------------------------------
/** Set the current animation frame */
// ------------------------------------------------------------------------------------------------
void Anim2D::setCurrentFrame( UnsignedShort frame )
{
// sanity
DEBUG_ASSERTCRASH( m_template != nullptr, ("Anim2D::reset - No template for animation") );
// sanity
DEBUG_ASSERTCRASH( TheGameLogic != nullptr,
("Anim2D::setCurrentFrame - TheGameLogic must exist to use animation instances (%s)",
m_template->getName().str()) );
// sanity
DEBUG_ASSERTCRASH( frame >= 0 && frame < m_template->getNumFrames(),
("Anim2D::setCurrentFrame - Illegal frame number '%d' in animation",
frame, m_template->getName().str()) );
// set the frame
m_currentFrame = frame;
// record the frame of this update to our current frame
m_lastUpdateFrame = TheGameLogic->getFrame();
}
// ------------------------------------------------------------------------------------------------
/** Randomize the current frame */
// ------------------------------------------------------------------------------------------------
void Anim2D::randomizeCurrentFrame()
{
// sanity
DEBUG_ASSERTCRASH( m_template != nullptr, ("Anim2D::reset - No template for animation") );
// set the current frame to a random frame
setCurrentFrame( GameClientRandomValue( 0, m_template->getNumFrames() - 1 ) );
}
// ------------------------------------------------------------------------------------------------
/** Reset this animation instance to the "start" of the animation */
// ------------------------------------------------------------------------------------------------
void Anim2D::reset()
{
// sanity
DEBUG_ASSERTCRASH( m_template != nullptr, ("Anim2D::reset - No template for animation") );
switch( m_template->getAnimMode() )
{
// --------------------------------------------------------------------------------------------
case ANIM_2D_ONCE:
case ANIM_2D_LOOP:
case ANIM_2D_PING_PONG:
setCurrentFrame( m_minFrame );
break;
// --------------------------------------------------------------------------------------------
case ANIM_2D_ONCE_BACKWARDS:
case ANIM_2D_LOOP_BACKWARDS:
case ANIM_2D_PING_PONG_BACKWARDS:
setCurrentFrame( m_maxFrame );
break;
// --------------------------------------------------------------------------------------------
default:
DEBUG_CRASH(( "Anim2D::reset - Unknown animation mode '%d' for '%s'",
m_template->getAnimMode(), m_template->getName().str() ));
break;
}
}
// ------------------------------------------------------------------------------------------------
/** This is called after we are drawn ... if sufficient time has passed since our last
* frame update we will update our current frame */
// ------------------------------------------------------------------------------------------------
void Anim2D::tryNextFrame()
{
// sanity
DEBUG_ASSERTCRASH( TheGameLogic != nullptr,
("Anim2D::tryNextFrame - TheGameLogic must exist to use animation instances (%s)",
m_template->getName().str()) );
// how many frames have passed since our last update
if( TheGameLogic->getFrame() - m_lastUpdateFrame >= m_framesBetweenUpdates )
{
switch( m_template->getAnimMode() )
{
// ------------------------------------------------------------------------------------------
case ANIM_2D_ONCE:
{
if( m_currentFrame < m_maxFrame )
setCurrentFrame( m_currentFrame + 1 );
else
setStatus( ANIM_2D_STATUS_COMPLETE );
break;
}
// -------------------------------------------------------------------------------------------
case ANIM_2D_ONCE_BACKWARDS:
{
if( m_currentFrame > m_minFrame )
setCurrentFrame( m_currentFrame - 1 );
else
setStatus( ANIM_2D_STATUS_COMPLETE );
break;
}
// -------------------------------------------------------------------------------------------
case ANIM_2D_LOOP:
{
if( m_currentFrame == m_maxFrame )
setCurrentFrame( m_minFrame );
else
setCurrentFrame( m_currentFrame + 1 );
break;
}
// -------------------------------------------------------------------------------------------
case ANIM_2D_LOOP_BACKWARDS:
{
if( m_currentFrame > m_minFrame )
setCurrentFrame( m_currentFrame - 1 );
else
setCurrentFrame( m_maxFrame );
break;
}
// -------------------------------------------------------------------------------------------
case ANIM_2D_PING_PONG:
case ANIM_2D_PING_PONG_BACKWARDS:
{
if( BitIsSet( m_status, ANIM_2D_STATUS_REVERSED ) )
{
//
// decrement frame, unless we're at frame 0 in which case we
// increment and reverse directions
//
if( m_currentFrame == m_minFrame )
{
setCurrentFrame( m_currentFrame + 1 );
clearStatus( ANIM_2D_STATUS_REVERSED );
}
else
{
setCurrentFrame( m_currentFrame - 1 );
}
}
else
{
//
// increment frame, unless we're at the end in which case we decrement
// and reverse directions
//
if( m_currentFrame == m_maxFrame )
{
setCurrentFrame( m_currentFrame - 1 );
setStatus( ANIM_2D_STATUS_REVERSED );
}
else
{
setCurrentFrame( m_currentFrame + 1 );
}
}
break;
}
// -------------------------------------------------------------------------------------------
default:
{
DEBUG_CRASH(( "Anim2D::tryNextFrame - Unknown animation mode '%d' for '%s'",
m_template->getAnimMode(), m_template->getName().str() ));
break;
}
}
}
}
// ------------------------------------------------------------------------------------------------
/** Set status bit */
// ------------------------------------------------------------------------------------------------
void Anim2D::setStatus( UnsignedByte statusBits )
{
// set the bits
BitSet( m_status, statusBits );
}
// ------------------------------------------------------------------------------------------------
/** Clear status bit */
// ------------------------------------------------------------------------------------------------
void Anim2D::clearStatus( UnsignedByte statusBits )
{
// clear bits
BitClear( m_status, statusBits );
}
// ------------------------------------------------------------------------------------------------
/** Return the "natural" width of the image for our current frame */
// ------------------------------------------------------------------------------------------------
UnsignedInt Anim2D::getCurrentFrameWidth() const
{
const Image *currentFrameImage = m_template->getFrame( m_currentFrame );
if( currentFrameImage )
return currentFrameImage->getImageWidth();
return 0;
}
// ------------------------------------------------------------------------------------------------
/** Return the "natural" height of the image for our current frame */
// ------------------------------------------------------------------------------------------------
UnsignedInt Anim2D::getCurrentFrameHeight() const
{
const Image *currentFrameImage = m_template->getFrame( m_currentFrame );
if( currentFrameImage )
return currentFrameImage->getImageHeight();
return 0;
}
// ------------------------------------------------------------------------------------------------
/** Draw an Anim2D using the natural width and height of the image data */
// ------------------------------------------------------------------------------------------------
void Anim2D::draw( Int x, Int y )
{
// get the current image
const Image *image = m_template->getFrame( m_currentFrame );
// sanity
DEBUG_ASSERTCRASH( image != nullptr, ("Anim2D::draw - Image not found for frame '%d' on animation '%s'",
m_currentFrame, m_template->getName().str()) );
// get the natural width and height of this image
const ICoord2D *imageSize = image->getImageSize();
// draw the image
Color color = GameMakeColor( 255, 255, 255, 255 * m_alpha );
TheDisplay->drawImage( image, x, y, x + imageSize->x, y + imageSize->y, color );
//
// see if it's time for us to go to the next frame in the sequence, we do not update
// frame numbers for animation instances that are registered with a system as the
// system will update them during its update phase
//
if( m_collectionSystem == nullptr && BitIsSet( m_status, ANIM_2D_STATUS_FROZEN ) == FALSE )
tryNextFrame();
}
// ------------------------------------------------------------------------------------------------
/** Drawing an Anim2D using a forced width and height */
// ------------------------------------------------------------------------------------------------
void Anim2D::draw( Int x, Int y, Int width, Int height )
{
// get the current image
const Image *image = m_template->getFrame( m_currentFrame );
// sanity
DEBUG_ASSERTCRASH( image != nullptr, ("Anim2D::draw - Image not found for frame '%d' on animation '%s'",
m_currentFrame, m_template->getName().str()) );
// draw image to the display
Color color = GameMakeColor( 255, 255, 255, 255 * m_alpha );
TheDisplay->drawImage( image, x, y, x + width, y + height, color );
//
// see if it's time for us to go to the next frame in the sequence, we do not update
// frame numbers for animation instances that are registered with a system as the
// system will update them during its update phase
//
if( m_collectionSystem == nullptr && BitIsSet( m_status, ANIM_2D_STATUS_FROZEN ) == FALSE )
tryNextFrame();
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method
* Version Info:
* 1: Initial version */
// ------------------------------------------------------------------------------------------------
void Anim2D::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// current frame
xfer->xferUnsignedShort( &m_currentFrame );
// last update frame
xfer->xferUnsignedInt( &m_lastUpdateFrame );
// status
xfer->xferUnsignedByte( &m_status );
// min frame
xfer->xferUnsignedShort( &m_minFrame );
// max frame
xfer->xferUnsignedShort( &m_maxFrame );
// frames between updates
xfer->xferUnsignedInt( &m_framesBetweenUpdates );
// alpha
xfer->xferReal( &m_alpha );
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2DCollection::Anim2DCollection()
{
m_templateList = nullptr;
m_instanceList = nullptr;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Anim2DCollection::~Anim2DCollection()
{
// there should not be any animation instances registered with us since we're being destroyed
DEBUG_ASSERTCRASH( m_instanceList == nullptr, ("Anim2DCollection - instance list is not null") );
// delete all the templates
Anim2DTemplate *nextTemplate;
while( m_templateList )
{
// get next template
nextTemplate = m_templateList->friend_getNextTemplate();
// delete this template
deleteInstance(m_templateList);
// set the head of our list to the next template
m_templateList = nextTemplate;
}
}
// ------------------------------------------------------------------------------------------------
/** Initialize 2D animation collection */
// ------------------------------------------------------------------------------------------------
void Anim2DCollection::init()
{
INI ini;
ini.loadFileDirectory( "Data\\INI\\Animation2D", INI_LOAD_OVERWRITE, nullptr );
}
// ------------------------------------------------------------------------------------------------
/** System update phase */
// ------------------------------------------------------------------------------------------------
void Anim2DCollection::update()
{
Anim2D *anim;
// go through all our animations
for( anim = m_instanceList; anim; anim = anim->m_collectionSystemNext )
{
// try to update the frame
if( BitIsSet( anim->getStatus(), ANIM_2D_STATUS_FROZEN ) == FALSE )
anim->tryNextFrame();
}
}
// ------------------------------------------------------------------------------------------------
/** Search the template list for a template with a matching name */
// ------------------------------------------------------------------------------------------------
Anim2DTemplate *Anim2DCollection::findTemplate( const AsciiString& name )
{
// search the list
for( Anim2DTemplate *animTemplate = m_templateList;
animTemplate;
animTemplate = animTemplate->friend_getNextTemplate() )
{
if( animTemplate->getName() == name )
return animTemplate;
}
return nullptr; // template not found
}
//-------------------------------------------------------------------------------------------------
Anim2DTemplate* Anim2DCollection::getNextTemplate( Anim2DTemplate *animTemplate ) const
{
if( animTemplate )
{
return animTemplate->friend_getNextTemplate();
}
return nullptr;
}
// ------------------------------------------------------------------------------------------------
/** Allocate a new template, assign name, and link to our internal list */
// ------------------------------------------------------------------------------------------------
Anim2DTemplate *Anim2DCollection::newTemplate( const AsciiString& name )
{
// allocate a new template
Anim2DTemplate *animTemplate = newInstance(Anim2DTemplate)( name );
// link to our template list
animTemplate->friend_setNextTemplate( m_templateList );
m_templateList = animTemplate;
// return the new template
return animTemplate;
}
// ------------------------------------------------------------------------------------------------
/** Register animation instance with us. When an animation instance is registered it can
* be updated even when it's not drawn */
// ------------------------------------------------------------------------------------------------
void Anim2DCollection::registerAnimation( Anim2D *anim )
{
// sanity
if( anim == nullptr )
return;
// sanity
DEBUG_ASSERTCRASH( anim->m_collectionSystemNext == nullptr &&
anim->m_collectionSystemPrev == nullptr,
("Registering animation instance, instance '%s' is already in a system",
anim->getAnimTemplate()->getName().str()) );
// tie to our list
anim->m_collectionSystemPrev = nullptr;
anim->m_collectionSystemNext = m_instanceList;
if( m_instanceList )
m_instanceList->m_collectionSystemPrev = anim;
m_instanceList = anim;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void Anim2DCollection::unRegisterAnimation( Anim2D *anim )
{
// sanity
if( anim == nullptr )
return;
// if animation is not registered with us do nothing
if( anim->m_collectionSystem != this )
return;
// unlink from our instance list
if( anim->m_collectionSystemNext )
anim->m_collectionSystemNext->m_collectionSystemPrev = anim->m_collectionSystemPrev;
if( anim->m_collectionSystemPrev )
anim->m_collectionSystemPrev->m_collectionSystemNext = anim->m_collectionSystemNext;
else
m_instanceList = anim->m_collectionSystemNext;
}