Anim2D.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.h /////////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, July 2002
  25. // Desc: A collection of 2D images to make animation
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __ANIM_2D_H_
  29. #define __ANIM_2D_H_
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Common/Snapshot.h"
  32. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  33. class Image;
  34. // ------------------------------------------------------------------------------------------------
  35. // ------------------------------------------------------------------------------------------------
  36. enum Anim2DMode
  37. {
  38. ANIM_2D_INVALID = 0,
  39. ANIM_2D_ONCE,
  40. ANIM_2D_ONCE_BACKWARDS,
  41. ANIM_2D_LOOP,
  42. ANIM_2D_LOOP_BACKWARDS,
  43. ANIM_2D_PING_PONG,
  44. ANIM_2D_PING_PONG_BACKWARDS,
  45. // dont' forget to add new animation mode names to Anim2DModeNames[] below
  46. ANIM_2D_NUM_MODES // keep this last please
  47. };
  48. #ifdef DEFINE_ANIM_2D_MODE_NAMES
  49. static char *Anim2DModeNames[] =
  50. {
  51. "NONE",
  52. "ONCE",
  53. "ONCE_BACKWARDS",
  54. "LOOP",
  55. "LOOP_BACKWARDS",
  56. "PING_PONG",
  57. "PING_PONG_BACKWARDS",
  58. NULL
  59. };
  60. #endif
  61. // ------------------------------------------------------------------------------------------------
  62. /** A template of a 2D animation */
  63. // ------------------------------------------------------------------------------------------------
  64. class Anim2DTemplate : public MemoryPoolObject
  65. {
  66. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(Anim2DTemplate, "Anim2DTemplate")
  67. public:
  68. Anim2DTemplate( AsciiString name );
  69. //virtual ~Anim2DTemplate( void );
  70. AsciiString getName( void ) const { return m_name; }
  71. const Image *getFrame( UnsignedShort frameNumber ) const;
  72. UnsignedShort getNumFrames( void ) const { return m_numFrames; }
  73. UnsignedShort getNumFramesBetweenUpdates( void ) const { return m_framesBetweenUpdates; }
  74. Anim2DMode getAnimMode( void ) const { return m_animMode; }
  75. Bool isRandomizedStartFrame( void ) const { return m_randomizeStartFrame; }
  76. // list access for use by the Anim2DCollection only
  77. void friend_setNextTemplate( Anim2DTemplate *animTemplate ) { m_nextTemplate = animTemplate; }
  78. Anim2DTemplate *friend_getNextTemplate( void ) const { return m_nextTemplate; };
  79. // INI methods
  80. const FieldParse *getFieldParse( void ) const { return s_anim2DFieldParseTable; }
  81. void storeImage( const Image *image ); ///< store image in next available slot
  82. void allocateImages( UnsignedShort numFrames ); ///< allocate the array of image pointers to use
  83. protected:
  84. static void parseImage( INI *ini, void *instance, void *store, const void *userData );
  85. static void parseNumImages( INI *ini, void *instance, void *store, const void *userData );
  86. static void parseImageSequence( INI *ini, void *instance, void *store, const void *userData );
  87. protected:
  88. enum { NUM_FRAMES_INVALID = 0 }; ///< initialization value for num frames
  89. Anim2DTemplate* m_nextTemplate; ///< next animation in collections animation list
  90. AsciiString m_name; ///< name of this 2D animation
  91. const Image** m_images; ///< array of image pointers that make up this animation
  92. UnsignedShort m_numFrames; ///< total number of frames in this animation
  93. UnsignedShort m_framesBetweenUpdates; ///< frames between frame updates
  94. Anim2DMode m_animMode; ///< the animation mode
  95. Bool m_randomizeStartFrame; ///< randomize animation instance start frames
  96. protected:
  97. static const FieldParse s_anim2DFieldParseTable[]; ///< the parse table for INI definition
  98. };
  99. // ------------------------------------------------------------------------------------------------
  100. // ------------------------------------------------------------------------------------------------
  101. enum Anim2DStatus
  102. {
  103. ANIM_2D_STATUS_NONE = 0x00,
  104. ANIM_2D_STATUS_FROZEN = 0x01,
  105. ANIM_2D_STATUS_REVERSED = 0x02, // used for ping pong direction tracking
  106. ANIM_2D_STATUS_COMPLETE = 0x04, // set when uni-directional things reach their last frame
  107. };
  108. // ------------------------------------------------------------------------------------------------
  109. // ------------------------------------------------------------------------------------------------
  110. class Anim2D : public MemoryPoolObject,
  111. public Snapshot
  112. {
  113. friend class Anim2DCollection;
  114. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( Anim2D, "Anim2D" );
  115. public:
  116. Anim2D( Anim2DTemplate *animTemplate, Anim2DCollection *collectionSystem );
  117. // virtual destructor prototype provided by memory pool object
  118. UnsignedShort getCurrentFrame( void ) const { return m_currentFrame; } ///< get our current frame #
  119. void setCurrentFrame( UnsignedShort frame ); ///< set the current frame #
  120. void randomizeCurrentFrame( void ); ///< randomize the current frame #
  121. void reset( void ); ///< reset the current frame to the "start"
  122. void setStatus( UnsignedByte statusBits ); ///< set status bit(s)
  123. void clearStatus( UnsignedByte statusBits ); ///< clear status bit(s)
  124. UnsignedByte getStatus( void ) const { return m_status; } ///< return status bits(s)
  125. void setAlpha( Real alpha ) { m_alpha = alpha; } ///< set alpha value
  126. Real getAlpha( void ) const { return m_alpha; } ///< return the current alpha value
  127. //Allows you to play a segment of an animation.
  128. void setMinFrame( UnsignedShort frame ) { m_minFrame = frame; }
  129. void setMaxFrame( UnsignedShort frame ) { m_maxFrame = frame; }
  130. // info about the size of the current frame
  131. UnsignedInt getCurrentFrameWidth( void ) const; ///< return natural width of image in the current frame
  132. UnsignedInt getCurrentFrameHeight( void ) const; ///< return natural height of image in the current frame
  133. const Anim2DTemplate *getAnimTemplate( void ) const { return m_template; } ///< return our template
  134. void draw( Int x, Int y ); ///< draw iamge at location using natural width/height
  135. void draw( Int x, Int y, Int width, Int height ); ///< draw image at location using forced width/height
  136. protected:
  137. // snapshot methods
  138. virtual void crc( Xfer *xfer ) { }
  139. virtual void xfer( Xfer *xfer );
  140. virtual void loadPostProcess( void ) { }
  141. void tryNextFrame( void ); ///< we've just drawn ... try to update our frame if necessary
  142. UnsignedShort m_currentFrame; ///< current frame of our animation
  143. UnsignedInt m_lastUpdateFrame; ///< last frame we updated on
  144. Anim2DTemplate *m_template; ///< pointer back to the template that defines this animation
  145. UnsignedByte m_status; ///< status bits (see Anim2DStatus)
  146. UnsignedShort m_minFrame; ///< min animation frame used inclusively.
  147. UnsignedShort m_maxFrame; ///< max animation frame used inclusively.
  148. UnsignedInt m_framesBetweenUpdates; ///< duration between each frame.
  149. Real m_alpha;
  150. Anim2DCollection *m_collectionSystem; ///< system collection (if any) we're registered with
  151. Anim2D *m_collectionSystemNext; ///< system instance tracking list
  152. Anim2D *m_collectionSystemPrev; ///< system instance tracking list
  153. };
  154. // ------------------------------------------------------------------------------------------------
  155. // ------------------------------------------------------------------------------------------------
  156. class Anim2DCollection : public SubsystemInterface
  157. {
  158. public:
  159. Anim2DCollection( void );
  160. virtual ~Anim2DCollection( void );
  161. virtual void init( void ); ///< initialize system
  162. virtual void reset( void ) { }; ///< reset system
  163. virtual void update( void ); ///< update system
  164. Anim2DTemplate *findTemplate( const AsciiString& name ); ///< find animation template
  165. Anim2DTemplate *newTemplate( const AsciiString& name ); ///< allocate a new template to be loaded
  166. void registerAnimation( Anim2D *anim ); ///< register animation with system
  167. void unRegisterAnimation( Anim2D *anim ); ///< un-register animation from system
  168. Anim2DTemplate* getTemplateHead() const { return m_templateList; }
  169. Anim2DTemplate* getNextTemplate( Anim2DTemplate *animTemplate ) const;
  170. protected:
  171. Anim2DTemplate *m_templateList; ///< list of available animation templates
  172. Anim2D *m_instanceList; ///< list of all the anim 2D instance we're tracking
  173. };
  174. // EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
  175. extern Anim2DCollection *TheAnim2DCollection;
  176. #endif // end __ANIM_2D_H_