Anim2D.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Anim2D.cpp ///////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, July 2002
  25. // Desc: A collection of 2D images to make animation
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_ANIM_2D_MODE_NAMES
  30. #include "Common/RandomValue.h"
  31. #include "Common/Xfer.h"
  32. #include "GameClient/Anim2D.h"
  33. #include "GameClient/Display.h"
  34. #include "GameClient/Image.h"
  35. #include "GameLogic/GameLogic.h"
  36. #ifdef _INTERNAL
  37. // for occasional debugging...
  38. //#pragma optimize("", off)
  39. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  40. #endif
  41. // GLOBAL /////////////////////////////////////////////////////////////////////////////////////////
  42. Anim2DCollection *TheAnim2DCollection = NULL;
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////
  46. // ------------------------------------------------------------------------------------------------
  47. // ------------------------------------------------------------------------------------------------
  48. Anim2DTemplate::Anim2DTemplate( AsciiString name )
  49. {
  50. m_name = name;
  51. m_images = NULL;
  52. m_numFrames = NUM_FRAMES_INVALID;
  53. m_framesBetweenUpdates = 0;
  54. m_animMode = ANIM_2D_LOOP;
  55. m_randomizeStartFrame = FALSE;
  56. m_nextTemplate = NULL;
  57. } // end Anim2DTemplate
  58. // ------------------------------------------------------------------------------------------------
  59. // ------------------------------------------------------------------------------------------------
  60. Anim2DTemplate::~Anim2DTemplate( void )
  61. {
  62. // delete the images
  63. if( m_images )
  64. delete [] m_images;
  65. } // end ~Anim2DTemplate
  66. // ------------------------------------------------------------------------------------------------
  67. /** Field parse table for 2D animation templates */
  68. // ---------i---------------------------------------------------------------------------------------
  69. const FieldParse Anim2DTemplate::s_anim2DFieldParseTable[] =
  70. {
  71. { "NumberImages", Anim2DTemplate::parseNumImages, NULL, 0 },
  72. { "Image", Anim2DTemplate::parseImage, NULL, 0 },
  73. { "ImageSequence", Anim2DTemplate::parseImageSequence, NULL, 0 },
  74. { "AnimationMode", INI::parseIndexList, Anim2DModeNames, offsetof( Anim2DTemplate, m_animMode ) },
  75. { "AnimationDelay", INI::parseDurationUnsignedShort, NULL, offsetof( Anim2DTemplate, m_framesBetweenUpdates ) },
  76. { "RandomizeStartFrame", INI::parseBool, NULL, offsetof( Anim2DTemplate, m_randomizeStartFrame ) },
  77. { NULL, NULL, NULL, 0 }
  78. };
  79. // ------------------------------------------------------------------------------------------------
  80. /** Parse the number of images we will have in this animation and allocate the array for them */
  81. // ------------------------------------------------------------------------------------------------
  82. void Anim2DTemplate::parseNumImages( INI *ini, void *instance, void *store, const void *userData )
  83. {
  84. // parse the integer data from the file
  85. UnsignedInt numFrames;
  86. ini->parseUnsignedInt( ini, instance, &numFrames, userData );
  87. // get the template we are to store into
  88. Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
  89. // animations must have a minimum # of frames
  90. Int minimumFrames = 1;
  91. if( numFrames < minimumFrames )
  92. {
  93. DEBUG_CRASH(( "Anim2DTemplate::parseNumImages - Invalid animation '%s', animations must have '%d' or more frames defined\n",
  94. animTemplate->getName().str(), minimumFrames ));
  95. throw INI_INVALID_DATA;
  96. } // end if
  97. // allocate the image array
  98. animTemplate->allocateImages( (UnsignedShort)numFrames );
  99. } // end parseNumImages
  100. // ------------------------------------------------------------------------------------------------
  101. /** Allocate the image array for an animation template and store the number of frames we have */
  102. // ------------------------------------------------------------------------------------------------
  103. void Anim2DTemplate::allocateImages( UnsignedShort numFrames )
  104. {
  105. // store the number of frames
  106. m_numFrames = numFrames;
  107. // allocate an array to hold the image pointers
  108. m_images = NEW const Image *[ m_numFrames ]; // pool[]ify
  109. // set all the images to NULL;
  110. for( Int i = 0; i < m_numFrames; ++i )
  111. m_images[ i ] = NULL;
  112. } // end allocateImages
  113. // ------------------------------------------------------------------------------------------------
  114. /** Parsing a single image definition for an animation */
  115. // ------------------------------------------------------------------------------------------------
  116. void Anim2DTemplate::parseImage( INI *ini, void *instance, void *store, const void *userData )
  117. {
  118. // parse the image name from the file and store as an image pointer
  119. const Image *image;
  120. ini->parseMappedImage( ini, instance, &image, userData );
  121. // sanity
  122. if( image == NULL )
  123. {
  124. //We don't care if we're in the builder
  125. //DEBUG_CRASH(( "Anim2DTemplate::parseImage - Image not found\n" ));
  126. //throw INI_INVALID_DATA;
  127. } // end if
  128. //
  129. // assign the image to the animation template list of images ... note since we've pre-allocated
  130. // the array of images and the index an image is loaded into depends on its order specified
  131. // in INI, we need to get the number of images currently loaded into this animation template
  132. // so that we can put it at the next free image spot ... and then tell the animation
  133. // template we've loaded one more
  134. //
  135. Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
  136. animTemplate->storeImage( image );
  137. } // end parseImage
  138. // ------------------------------------------------------------------------------------------------
  139. /** This will parse the image sequence of an animation. You can use this as a shortcut to
  140. * specifying a series of images for an animation instead of having to specify them all
  141. * individually. Image names will be assumed to start with an appended "000" to the end
  142. * end of the first image name and incremented up to the number of images for the
  143. * animation. NOTE: That the number images *must* have already been specified before
  144. * we can parse this entry so we know how many images to allocate and look for */
  145. // ------------------------------------------------------------------------------------------------
  146. /*static*/ void Anim2DTemplate::parseImageSequence( INI *ini, void *instance,
  147. void *store, const void *userData )
  148. {
  149. // get the animation template
  150. Anim2DTemplate *animTemplate = (Anim2DTemplate *)instance;
  151. //
  152. // before we can read, allocate, and find all the images for the sequence ... we must
  153. // know how many total images are in this animation. Verify that now
  154. //
  155. if( animTemplate->getNumFrames() == NUM_FRAMES_INVALID )
  156. {
  157. DEBUG_CRASH(( "Anim2DTemplate::parseImageSequence - You must specify the number of animation frames for animation '%s' *BEFORE* specifying the image sequence name\n",
  158. animTemplate->getName().str() ));
  159. throw INI_INVALID_DATA;
  160. } // end if
  161. //
  162. // the image storage has already been allocated, all we have to do now is count from
  163. // 0 to numImages - 1 and add those images to the template
  164. //
  165. AsciiString imageBaseName = ini->getNextAsciiString();
  166. AsciiString imageName;
  167. const Image *image;
  168. for( Int i = 0; i < animTemplate->getNumFrames(); ++i )
  169. {
  170. // construct this name
  171. imageName.format( "%s%03d", imageBaseName.str(), i );
  172. // search for this image
  173. image = TheMappedImageCollection->findImageByName( imageName );
  174. // sanity
  175. if( image == NULL )
  176. {
  177. 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.\n",
  178. imageName.str(), animTemplate->getName().str() ));
  179. throw INI_INVALID_DATA;
  180. } // end if
  181. // store the image in the next free frame
  182. animTemplate->storeImage( image );
  183. } // end if
  184. } // end parseImageSequence
  185. // ------------------------------------------------------------------------------------------------
  186. /** Store the image at the next open image slot for the animation */
  187. // ------------------------------------------------------------------------------------------------
  188. void Anim2DTemplate::storeImage( const Image *image )
  189. {
  190. // sanity
  191. if( image == NULL )
  192. return;
  193. // search through the image list and store at the next free spot
  194. for( Int i = 0; i < m_numFrames; ++i )
  195. {
  196. if( m_images[ i ] == NULL )
  197. {
  198. m_images[ i ] = image;
  199. return;
  200. } // end if
  201. } // end for i
  202. // if we got here we tried to store an image in an array that was too small
  203. DEBUG_CRASH(( "Anim2DTemplate::storeImage - Unable to store image '%s' into animation '%s' because the animation is setup to only support '%d' image frames\n",
  204. image->getName().str(), getName().str(), m_numFrames ));
  205. throw INI_INVALID_DATA;
  206. } // end storeImage
  207. // ------------------------------------------------------------------------------------------------
  208. /** Return the Image* for the frame number requested */
  209. // ------------------------------------------------------------------------------------------------
  210. const Image* Anim2DTemplate::getFrame( UnsignedShort frameNumber ) const
  211. {
  212. // sanity
  213. DEBUG_ASSERTCRASH( m_images != NULL,
  214. ("Anim2DTemplate::getFrame - Image data is NULL for animation '%s'\n",
  215. getName().str()) );
  216. // sanity
  217. if( frameNumber < 0 || frameNumber >= m_numFrames )
  218. {
  219. DEBUG_CRASH(( "Anim2DTemplate::getFrame - Illegal frame number '%d' for animation '%s'\n",
  220. frameNumber, getName().str() ));
  221. return NULL;
  222. } // end if
  223. else
  224. {
  225. // return the image frame
  226. return m_images[ frameNumber ];
  227. } // end else
  228. } // end getFrame
  229. ///////////////////////////////////////////////////////////////////////////////////////////////////
  230. ///////////////////////////////////////////////////////////////////////////////////////////////////
  231. ///////////////////////////////////////////////////////////////////////////////////////////////////
  232. // ------------------------------------------------------------------------------------------------
  233. // ------------------------------------------------------------------------------------------------
  234. Anim2D::Anim2D( Anim2DTemplate *animTemplate, Anim2DCollection *collectionSystem )
  235. {
  236. // sanity
  237. DEBUG_ASSERTCRASH( animTemplate != NULL, ("Anim2D::Anim2D - NULL template\n") );
  238. //Added By Sadullah Nader
  239. //Initialization
  240. m_currentFrame = 0;
  241. //
  242. // set the template
  243. m_template = animTemplate;
  244. // initialize members
  245. m_status = ANIM_2D_STATUS_NONE;
  246. m_alpha = 1.0f;
  247. // set the initial frame for the animation based on the type of animation mode or randomize
  248. if( m_template->isRandomizedStartFrame() )
  249. randomizeCurrentFrame();
  250. else
  251. reset();
  252. m_minFrame = 0;
  253. m_maxFrame = m_template->getNumFrames() - 1;
  254. m_framesBetweenUpdates = m_template->getNumFramesBetweenUpdates();
  255. //added by Sadullah Nader
  256. // initializing pointers to NULL, and clearing Frame counters before
  257. // we register ourselves to the System
  258. m_collectionSystemNext = NULL;
  259. m_collectionSystemPrev = NULL;
  260. m_lastUpdateFrame = 0;
  261. // if a system is present, register ourselves with that system
  262. m_collectionSystem = collectionSystem;
  263. if( m_collectionSystem )
  264. m_collectionSystem->registerAnimation( this );
  265. } // end Anim2D
  266. // ------------------------------------------------------------------------------------------------
  267. // ------------------------------------------------------------------------------------------------
  268. Anim2D::~Anim2D( void )
  269. {
  270. // if we were registered with a system, un-register ourselves
  271. if( m_collectionSystem )
  272. m_collectionSystem->unRegisterAnimation( this );
  273. } // end ~Anim2D
  274. // ------------------------------------------------------------------------------------------------
  275. /** Set the current animation frame */
  276. // ------------------------------------------------------------------------------------------------
  277. void Anim2D::setCurrentFrame( UnsignedShort frame )
  278. {
  279. // sanity
  280. DEBUG_ASSERTCRASH( m_template != NULL, ("Anim2D::reset - No template for animation\n") );
  281. // sanity
  282. DEBUG_ASSERTCRASH( TheGameLogic != NULL,
  283. ("Anim2D::setCurrentFrame - TheGameLogic must exist to use animation instances (%s)\n",
  284. m_template->getName().str()) );
  285. // sanity
  286. DEBUG_ASSERTCRASH( frame >= 0 && frame < m_template->getNumFrames(),
  287. ("Anim2D::setCurrentFrame - Illegal frame number '%d' in animation\n",
  288. frame, m_template->getName().str()) );
  289. // set the frame
  290. m_currentFrame = frame;
  291. // record the frame of this update to our current frame
  292. m_lastUpdateFrame = TheGameLogic->getFrame();
  293. } // end setCurrentFrame
  294. // ------------------------------------------------------------------------------------------------
  295. /** Randomize the current frame */
  296. // ------------------------------------------------------------------------------------------------
  297. void Anim2D::randomizeCurrentFrame( void )
  298. {
  299. // sanity
  300. DEBUG_ASSERTCRASH( m_template != NULL, ("Anim2D::reset - No template for animation\n") );
  301. // set the current frame to a random frame
  302. setCurrentFrame( GameClientRandomValue( 0, m_template->getNumFrames() - 1 ) );
  303. } // end randomizeCurrentFrame
  304. // ------------------------------------------------------------------------------------------------
  305. /** Reset this animation instance to the "start" of the animation */
  306. // ------------------------------------------------------------------------------------------------
  307. void Anim2D::reset( void )
  308. {
  309. // sanity
  310. DEBUG_ASSERTCRASH( m_template != NULL, ("Anim2D::reset - No template for animation\n") );
  311. switch( m_template->getAnimMode() )
  312. {
  313. // --------------------------------------------------------------------------------------------
  314. case ANIM_2D_ONCE:
  315. case ANIM_2D_LOOP:
  316. case ANIM_2D_PING_PONG:
  317. setCurrentFrame( m_minFrame );
  318. break;
  319. // --------------------------------------------------------------------------------------------
  320. case ANIM_2D_ONCE_BACKWARDS:
  321. case ANIM_2D_LOOP_BACKWARDS:
  322. case ANIM_2D_PING_PONG_BACKWARDS:
  323. setCurrentFrame( m_maxFrame );
  324. break;
  325. // --------------------------------------------------------------------------------------------
  326. default:
  327. DEBUG_CRASH(( "Anim2D::reset - Unknown animation mode '%d' for '%s'\n",
  328. m_template->getAnimMode(), m_template->getName().str() ));
  329. break;
  330. } // end switch, animation mode
  331. } // end reset
  332. // ------------------------------------------------------------------------------------------------
  333. /** This is called after we are drawn ... if sufficient time has passed since our last
  334. * frame update we will update our current frame */
  335. // ------------------------------------------------------------------------------------------------
  336. void Anim2D::tryNextFrame( void )
  337. {
  338. // sanity
  339. DEBUG_ASSERTCRASH( TheGameLogic != NULL,
  340. ("Anim2D::tryNextFrame - TheGameLogic must exist to use animation instances (%s)\n",
  341. m_template->getName().str()) );
  342. // how many frames have passed since our last update
  343. if( TheGameLogic->getFrame() - m_lastUpdateFrame >= m_framesBetweenUpdates )
  344. {
  345. switch( m_template->getAnimMode() )
  346. {
  347. // ------------------------------------------------------------------------------------------
  348. case ANIM_2D_ONCE:
  349. {
  350. if( m_currentFrame < m_maxFrame )
  351. setCurrentFrame( m_currentFrame + 1 );
  352. else
  353. setStatus( ANIM_2D_STATUS_COMPLETE );
  354. break;
  355. } // end once
  356. // -------------------------------------------------------------------------------------------
  357. case ANIM_2D_ONCE_BACKWARDS:
  358. {
  359. if( m_currentFrame > m_minFrame )
  360. setCurrentFrame( m_currentFrame - 1 );
  361. else
  362. setStatus( ANIM_2D_STATUS_COMPLETE );
  363. break;
  364. } // end once backwards
  365. // -------------------------------------------------------------------------------------------
  366. case ANIM_2D_LOOP:
  367. {
  368. if( m_currentFrame == m_maxFrame )
  369. setCurrentFrame( m_minFrame );
  370. else
  371. setCurrentFrame( m_currentFrame + 1 );
  372. break;
  373. } // end loop
  374. // -------------------------------------------------------------------------------------------
  375. case ANIM_2D_LOOP_BACKWARDS:
  376. {
  377. if( m_currentFrame > m_minFrame )
  378. setCurrentFrame( m_currentFrame - 1 );
  379. else
  380. setCurrentFrame( m_maxFrame );
  381. break;
  382. } // end loop backwards
  383. // -------------------------------------------------------------------------------------------
  384. case ANIM_2D_PING_PONG:
  385. case ANIM_2D_PING_PONG_BACKWARDS:
  386. {
  387. if( BitTest( m_status, ANIM_2D_STATUS_REVERSED ) )
  388. {
  389. //
  390. // decrement frame, unless we're at frame 0 in which case we
  391. // increment and reverse directions
  392. //
  393. if( m_currentFrame == m_minFrame )
  394. {
  395. setCurrentFrame( m_currentFrame + 1 );
  396. clearStatus( ANIM_2D_STATUS_REVERSED );
  397. } // end if
  398. else
  399. {
  400. setCurrentFrame( m_currentFrame - 1 );
  401. } // end else
  402. } // end if
  403. else
  404. {
  405. //
  406. // increment frame, unless we're at the end in which case we decrement
  407. // and reverse directions
  408. //
  409. if( m_currentFrame == m_maxFrame )
  410. {
  411. setCurrentFrame( m_currentFrame - 1 );
  412. setStatus( ANIM_2D_STATUS_REVERSED );
  413. } // end if
  414. else
  415. {
  416. setCurrentFrame( m_currentFrame + 1 );
  417. } // end else
  418. } // end else
  419. break;
  420. } // end ping pong / ping pong backwards
  421. // -------------------------------------------------------------------------------------------
  422. default:
  423. {
  424. DEBUG_CRASH(( "Anim2D::tryNextFrame - Unknown animation mode '%d' for '%s'\n",
  425. m_template->getAnimMode(), m_template->getName().str() ));
  426. break;
  427. } // end default
  428. } // end switch
  429. } // end if
  430. } // end tryNextFrame
  431. // ------------------------------------------------------------------------------------------------
  432. /** Set status bit */
  433. // ------------------------------------------------------------------------------------------------
  434. void Anim2D::setStatus( UnsignedByte statusBits )
  435. {
  436. // set the bits
  437. BitSet( m_status, statusBits );
  438. } // end setStatus
  439. // ------------------------------------------------------------------------------------------------
  440. /** Clear status bit */
  441. // ------------------------------------------------------------------------------------------------
  442. void Anim2D::clearStatus( UnsignedByte statusBits )
  443. {
  444. // clear bits
  445. BitClear( m_status, statusBits );
  446. } // end clearStatus
  447. // ------------------------------------------------------------------------------------------------
  448. /** Return the "natural" width of the image for our current frame */
  449. // ------------------------------------------------------------------------------------------------
  450. UnsignedInt Anim2D::getCurrentFrameWidth( void ) const
  451. {
  452. const Image *currentFrameImage = m_template->getFrame( m_currentFrame );
  453. if( currentFrameImage )
  454. return currentFrameImage->getImageWidth();
  455. return 0;
  456. } // end getCurrentFrameWidth
  457. // ------------------------------------------------------------------------------------------------
  458. /** Return the "natural" height of the image for our current frame */
  459. // ------------------------------------------------------------------------------------------------
  460. UnsignedInt Anim2D::getCurrentFrameHeight( void ) const
  461. {
  462. const Image *currentFrameImage = m_template->getFrame( m_currentFrame );
  463. if( currentFrameImage )
  464. return currentFrameImage->getImageHeight();
  465. return 0;
  466. } // end getCurrentFrameHeight
  467. // ------------------------------------------------------------------------------------------------
  468. /** Draw an Anim2D using the natural width and height of the image data */
  469. // ------------------------------------------------------------------------------------------------
  470. void Anim2D::draw( Int x, Int y )
  471. {
  472. // get the current image
  473. const Image *image = m_template->getFrame( m_currentFrame );
  474. // sanity
  475. DEBUG_ASSERTCRASH( image != NULL, ("Anim2D::draw - Image not found for frame '%d' on animation '%s'\n",
  476. m_currentFrame, m_template->getName().str()) );
  477. // get the natural width and height of this image
  478. const ICoord2D *imageSize = image->getImageSize();
  479. // draw the image
  480. Color color = GameMakeColor( 255, 255, 255, 255 * m_alpha );
  481. TheDisplay->drawImage( image, x, y, x + imageSize->x, y + imageSize->y, color );
  482. //
  483. // see if it's time for us to go to the next frame in the sequence, we do not update
  484. // frame numbers for animation instances that are registered with a system as the
  485. // system will update them during its update phase
  486. //
  487. if( m_collectionSystem == NULL && BitTest( m_status, ANIM_2D_STATUS_FROZEN ) == FALSE )
  488. tryNextFrame();
  489. } // end draw
  490. // ------------------------------------------------------------------------------------------------
  491. /** Drawing an Anim2D using a forced width and height */
  492. // ------------------------------------------------------------------------------------------------
  493. void Anim2D::draw( Int x, Int y, Int width, Int height )
  494. {
  495. // get the current image
  496. const Image *image = m_template->getFrame( m_currentFrame );
  497. // sanity
  498. DEBUG_ASSERTCRASH( image != NULL, ("Anim2D::draw - Image not found for frame '%d' on animation '%s'\n",
  499. m_currentFrame, m_template->getName().str()) );
  500. // draw image to the display
  501. Color color = GameMakeColor( 255, 255, 255, 255 * m_alpha );
  502. TheDisplay->drawImage( image, x, y, x + width, y + height, color );
  503. //
  504. // see if it's time for us to go to the next frame in the sequence, we do not update
  505. // frame numbers for animation instances that are registered with a system as the
  506. // system will update them during its update phase
  507. //
  508. if( m_collectionSystem == NULL && BitTest( m_status, ANIM_2D_STATUS_FROZEN ) == FALSE )
  509. tryNextFrame();
  510. } // end draw
  511. // ------------------------------------------------------------------------------------------------
  512. /** Xfer Method
  513. * Version Info:
  514. * 1: Initial version */
  515. // ------------------------------------------------------------------------------------------------
  516. void Anim2D::xfer( Xfer *xfer )
  517. {
  518. // version
  519. XferVersion currentVersion = 1;
  520. XferVersion version = currentVersion;
  521. xfer->xferVersion( &version, currentVersion );
  522. // current frame
  523. xfer->xferUnsignedShort( &m_currentFrame );
  524. // last update frame
  525. xfer->xferUnsignedInt( &m_lastUpdateFrame );
  526. // status
  527. xfer->xferUnsignedByte( &m_status );
  528. // min frame
  529. xfer->xferUnsignedShort( &m_minFrame );
  530. // max frame
  531. xfer->xferUnsignedShort( &m_maxFrame );
  532. // frames between updates
  533. xfer->xferUnsignedInt( &m_framesBetweenUpdates );
  534. // alpha
  535. xfer->xferReal( &m_alpha );
  536. } // end xfer
  537. ///////////////////////////////////////////////////////////////////////////////////////////////////
  538. ///////////////////////////////////////////////////////////////////////////////////////////////////
  539. ///////////////////////////////////////////////////////////////////////////////////////////////////
  540. // ------------------------------------------------------------------------------------------------
  541. // ------------------------------------------------------------------------------------------------
  542. Anim2DCollection::Anim2DCollection( void )
  543. {
  544. m_templateList = NULL;
  545. m_instanceList = NULL;
  546. } // end Anim2DCollection
  547. // ------------------------------------------------------------------------------------------------
  548. // ------------------------------------------------------------------------------------------------
  549. Anim2DCollection::~Anim2DCollection( void )
  550. {
  551. // there should not be any animation instances registered with us since we're being destroyed
  552. DEBUG_ASSERTCRASH( m_instanceList == NULL, ("Anim2DCollection - instance list is not NULL\n") );
  553. // delete all the templates
  554. Anim2DTemplate *nextTemplate;
  555. while( m_templateList )
  556. {
  557. // get next template
  558. nextTemplate = m_templateList->friend_getNextTemplate();
  559. // delete this template
  560. m_templateList->deleteInstance();
  561. // set the head of our list to the next template
  562. m_templateList = nextTemplate;
  563. } // end while
  564. } // end ~Anim2DCollection
  565. // ------------------------------------------------------------------------------------------------
  566. /** Initialize 2D animation collection */
  567. // ------------------------------------------------------------------------------------------------
  568. void Anim2DCollection::init( void )
  569. {
  570. INI ini;
  571. ini.load( "Data\\INI\\Animation2D.ini", INI_LOAD_OVERWRITE, NULL );
  572. } // end init
  573. // ------------------------------------------------------------------------------------------------
  574. /** System update phase */
  575. // ------------------------------------------------------------------------------------------------
  576. void Anim2DCollection::update( void )
  577. {
  578. Anim2D *anim;
  579. // go through all our animations
  580. for( anim = m_instanceList; anim; anim = anim->m_collectionSystemNext )
  581. {
  582. // try to update the frame
  583. if( BitTest( anim->getStatus(), ANIM_2D_STATUS_FROZEN ) == FALSE )
  584. anim->tryNextFrame();
  585. } // end for, anim
  586. } // end update
  587. // ------------------------------------------------------------------------------------------------
  588. /** Search the template list for a template with a matching name */
  589. // ------------------------------------------------------------------------------------------------
  590. Anim2DTemplate *Anim2DCollection::findTemplate( const AsciiString& name )
  591. {
  592. // search the list
  593. for( Anim2DTemplate *animTemplate = m_templateList;
  594. animTemplate;
  595. animTemplate = animTemplate->friend_getNextTemplate() )
  596. {
  597. if( animTemplate->getName() == name )
  598. return animTemplate;
  599. } // end for
  600. return NULL; // template not found
  601. } // end findTemplate
  602. //-------------------------------------------------------------------------------------------------
  603. Anim2DTemplate* Anim2DCollection::getNextTemplate( Anim2DTemplate *animTemplate ) const
  604. {
  605. if( animTemplate )
  606. {
  607. return animTemplate->friend_getNextTemplate();
  608. }
  609. return NULL;
  610. }
  611. // ------------------------------------------------------------------------------------------------
  612. /** Allocate a new template, assign name, and link to our internal list */
  613. // ------------------------------------------------------------------------------------------------
  614. Anim2DTemplate *Anim2DCollection::newTemplate( const AsciiString& name )
  615. {
  616. // allocate a new template
  617. Anim2DTemplate *animTemplate = newInstance(Anim2DTemplate)( name );
  618. // link to our template list
  619. animTemplate->friend_setNextTemplate( m_templateList );
  620. m_templateList = animTemplate;
  621. // return the new template
  622. return animTemplate;
  623. } // end newTemplate
  624. // ------------------------------------------------------------------------------------------------
  625. /** Register animation instance with us. When an animation instance is registered it can
  626. * be updated even when it's not drawn */
  627. // ------------------------------------------------------------------------------------------------
  628. void Anim2DCollection::registerAnimation( Anim2D *anim )
  629. {
  630. // sanity
  631. if( anim == NULL )
  632. return;
  633. // sanity
  634. DEBUG_ASSERTCRASH( anim->m_collectionSystemNext == NULL &&
  635. anim->m_collectionSystemPrev == NULL,
  636. ("Registering animation instance, instance '%s' is already in a system\n",
  637. anim->getAnimTemplate()->getName().str()) );
  638. // tie to our list
  639. anim->m_collectionSystemPrev = NULL;
  640. anim->m_collectionSystemNext = m_instanceList;
  641. if( m_instanceList )
  642. m_instanceList->m_collectionSystemPrev = anim;
  643. m_instanceList = anim;
  644. } // end registerAnimation
  645. // ------------------------------------------------------------------------------------------------
  646. // ------------------------------------------------------------------------------------------------
  647. void Anim2DCollection::unRegisterAnimation( Anim2D *anim )
  648. {
  649. // sanity
  650. if( anim == NULL )
  651. return;
  652. // if animation is not registered with us do nothing
  653. if( anim->m_collectionSystem != this )
  654. return;
  655. // unlink from our instnace list
  656. if( anim->m_collectionSystemNext )
  657. anim->m_collectionSystemNext->m_collectionSystemPrev = anim->m_collectionSystemPrev;
  658. if( anim->m_collectionSystemPrev )
  659. anim->m_collectionSystemPrev->m_collectionSystemNext = anim->m_collectionSystemNext;
  660. else
  661. m_instanceList = anim->m_collectionSystemNext;
  662. } // end unRegisterAnimation