home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / gamesys / State.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  4KB  |  158 lines

  1. #ifndef __SYS_STATE_H__
  2. #define __SYS_STATE_H__
  3.  
  4. typedef enum {
  5.     SRESULT_OK,                // Call was made successfully
  6.     SRESULT_ERROR,            // An unrecoverable error occurred
  7.     SRESULT_DONE,            // Done with current state, move to next
  8.     SRESULT_DONE_WAIT,        // Done with current state, wait a frame then move to next
  9.     SRESULT_WAIT,            // Wait a frame and re-run current state
  10.     SRESULT_IDLE,            // State thread is currently idle (ie. no states)
  11.     SRESULT_SETSTAGE,        // Sets the current stage of the current state and reruns the state
  12.                             // NOTE: this has to be the last result becuase the stage is added to
  13.                             //         the result.
  14.     SRESULT_SETDELAY = SRESULT_SETSTAGE + 20
  15. } stateResult_t;
  16.  
  17. #define MAX_STATE_CALLS        50
  18.  
  19. #define SRESULT_STAGE(x)    ((stateResult_t)((int)SRESULT_SETSTAGE + (int)(x)))
  20. #define SRESULT_DELAY(x)    ((stateResult_t)((int)SRESULT_SETDELAY + (int)(x)))
  21.  
  22. struct stateParms_t {
  23.     int        blendFrames;
  24.     int        time;
  25.     int        stage;
  26.  
  27.     void    Save( idSaveGame *saveFile ) const;
  28.     void    Restore( idRestoreGame *saveFile );
  29. };
  30.  
  31. typedef stateResult_t ( idClass::*stateCallback_t )( const stateParms_t& parms );
  32.  
  33. template< class Type >
  34. struct rvStateFunc {
  35.     const char*            name;
  36.     stateCallback_t        function;
  37. };
  38.  
  39. /*
  40. ================
  41. CLASS_STATES_PROTOTYPE
  42.  
  43. This macro must be included in the definition of any subclass of idClass that
  44. wishes to have its own custom states.  Its prototypes variables used in the process
  45. of managing states.
  46. ================
  47. */
  48. #define CLASS_STATES_PROTOTYPE(nameofclass)                        \
  49. protected:                                                        \
  50.     static    rvStateFunc<nameofclass>        stateCallbacks[]
  51.  
  52. /*
  53. ================
  54. CLASS_STATES_DECLARATION
  55.  
  56. This macro must be included in the code to properly initialize variables
  57. used in state processing for a idClass dervied class
  58. ================
  59. */
  60. #define CLASS_STATES_DECLARATION(nameofclass)                \
  61. rvStateFunc<nameofclass> nameofclass::stateCallbacks[] = {
  62.     
  63. /*
  64. ================
  65. STATE
  66.  
  67. This macro declares a single state.  It must be surrounded by the CLASS_STATES_DECLARATION 
  68. and END_CLASS_STATES macros.
  69. ================
  70. */
  71. #define STATE(statename,function)            { statename, (stateCallback_t)( &function ) },
  72.  
  73. /*
  74. ================
  75. END_CLASS_STATES
  76.  
  77. Terminates a state block
  78. ================
  79. */
  80. #define END_CLASS_STATES                    { NULL, NULL } };
  81.  
  82. struct stateCall_t {
  83.     const rvStateFunc<idClass>*    state;
  84.     idLinkList<stateCall_t>        node;
  85.     int                            flags;
  86.     int                            delay;
  87.     stateParms_t                parms;
  88.  
  89.     void                        Save( idSaveGame *saveFile ) const;
  90.     void                        Restore( idRestoreGame *saveFile, const idClass* owner );
  91. };
  92.  
  93. class idClass;
  94.  
  95. const int SFLAG_ONCLEAR            = BIT(0);            // Executes, even if the state queue is cleared
  96. const int SFLAG_ONCLEARONLY        = BIT(1);            // Executes only if the state queue is cleared
  97.  
  98. class rvStateThread {
  99. public:
  100.  
  101.     rvStateThread ( void );
  102.     ~rvStateThread ( void );
  103.     
  104.     void            SetName            ( const char* name );
  105.     void            SetOwner        ( idClass* owner );
  106.     
  107.     bool            Interrupt        ( void );
  108.  
  109.     stateResult_t    InterruptState    ( const char* state, int blendFrames = 0, int delay = 0, int flags = 0 );    
  110.     stateResult_t    PostState        ( const char* state, int blendFrames = 0, int delay = 0, int flags = 0 );
  111.     stateResult_t    SetState        ( const char* state, int blendFrames = 0, int delay = 0, int flags = 0 );
  112.     stateCall_t*    GetState        ( void ) const;
  113.     bool            CurrentStateIs    ( const char* name ) const;
  114.     
  115.     stateResult_t    Execute            ( void );
  116.     
  117.     void            Clear            ( bool ignoreStateCalls = false );
  118.     
  119.     bool            IsIdle            ( void ) const;
  120.     bool            IsExecuting        ( void ) const;
  121.  
  122.     void            Save( idSaveGame *saveFile ) const;
  123.     void            Restore( idRestoreGame *saveFile, idClass* owner );
  124.     
  125. protected:
  126.  
  127.     struct flags {
  128.         bool        stateCleared        :1;        // State list was cleared 
  129.         bool        stateInterrupted    :1;        // State list was interrupted
  130.         bool        executing            :1;        // Execute is currently processing states
  131.     } fl;
  132.  
  133.     idStr                        name;
  134.     idClass*                    owner;
  135.     idLinkList<stateCall_t>        states;
  136.     idLinkList<stateCall_t>        interrupted;
  137.     stateCall_t*                insertAfter;
  138.     stateResult_t                lastResult;
  139. };
  140.  
  141. ID_INLINE void rvStateThread::SetName ( const char* _name ) {
  142.     name = _name;
  143. }
  144.  
  145. ID_INLINE stateCall_t* rvStateThread::GetState ( void ) const {
  146.     return states.Next();
  147. }
  148.  
  149. ID_INLINE bool rvStateThread::IsIdle ( void ) const {
  150.     return !states.Next() && !interrupted.Next();
  151. }
  152.  
  153. ID_INLINE bool rvStateThread::IsExecuting ( void ) const {
  154.     return fl.executing;
  155. }
  156.  
  157. #endif // __SYS_STATE_H__
  158.