iTickable.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ITICKABLE_H_
  23. #define _ITICKABLE_H_
  24. #include "core/util/tVector.h"
  25. /// This interface allows you to let any object be ticked. You use it like so:
  26. /// @code
  27. /// class FooClass : public SimObject, public virtual ITickable
  28. /// {
  29. /// // You still mark SimObject as Parent
  30. /// typdef SimObject Parent;
  31. /// private:
  32. /// ...
  33. ///
  34. /// protected:
  35. /// // These three methods are the interface for ITickable
  36. /// virtual void interpolateTick( F32 delta );
  37. /// virtual void processTick();
  38. /// virtual void advanceTime( F32 timeDelta );
  39. ///
  40. /// public:
  41. /// ...
  42. /// };
  43. /// @endcode
  44. /// Please note the three methods you must implement to use ITickable, but don't
  45. /// worry. If you forget, the compiler will tell you so. Also note that the
  46. /// typedef for Parent should NOT BE SET to ITickable, the compiler will <i>probably</i>
  47. /// also tell you if you forget that. Last, but assuridly not least is that you note
  48. /// the way that the inheretance is done: public <b>virtual</b> ITickable
  49. /// It is very important that you keep the virtual keyword in there, otherwise
  50. /// proper behavior is not guarenteed. You have been warned.
  51. ///
  52. /// The point of a tickable object is that the object gets ticks at a fixed rate
  53. /// which is one tick every 32ms. This means, also, that if an object doesn't get
  54. /// updated for 64ms, that the next update it will get two-ticks. Basically it
  55. /// comes down to this. You are assured to get one tick per 32ms of time passing
  56. /// provided that isProcessingTicks returns true when ITickable calls it.
  57. ///
  58. /// isProcessingTicks is a virtual method and you can (should you want to)
  59. /// override it and put some extended functionality to decide if you want to
  60. /// recieve tick-notification or not.
  61. ///
  62. /// The other half of this is that you get time-notification from advanceTime.
  63. /// advanceTime lets you know when time passes regardless of the return value
  64. /// of isProcessingTicks. The object WILL get the advanceTime call every single
  65. /// update. The argument passed to advanceTime is the time since the last call
  66. /// to advanceTime. Updates are not based on the 32ms tick time. Updates are
  67. /// dependant on framerate. So you may get 200 advanceTime calls in a second, or you
  68. /// may only get 20. There is no way of assuring consistant calls of advanceTime
  69. /// like there is with processTick. Both are useful for different things, and
  70. /// it is important to understand the differences between them.
  71. ///
  72. /// Interpolation is the last part of the ITickable interface. It is called
  73. /// every update, as long as isProcessingTicks evaluates to true on the object.
  74. /// This is used to interpolate between 32ms ticks. The argument passed to
  75. /// interpolateTick is the time since the last call to processTick. You can see
  76. /// in the code for ITickable::advanceTime that before a tick occurs it calls
  77. /// interpolateTick(0) on every object. This is to tell objects which do interpolate
  78. /// between ticks to reset their interpolation because they are about to get a
  79. /// new tick.
  80. ///
  81. /// This is an extremely powerful interface when used properly. An example of a class
  82. /// that properly uses this interface is GuiTickCtrl. The documentation for that
  83. /// class describes why it was created and why it was important that it use
  84. /// a consistant update frequency for its effects.
  85. /// @see GuiTickCtrl
  86. ///
  87. /// @todo Support processBefore/After and move the GameBase processing over to use ITickable
  88. class ITickable
  89. {
  90. private:
  91. static U32 smLastTick; ///< Time of the last tick that occurred
  92. static U32 smLastTime; ///< Last time value at which advanceTime was called
  93. static U32 smLastDelta; ///< Last delta value for advanceTime
  94. static U32 smTickShift; ///< Shift value to control how often Ticks occur
  95. static U32 smTickMs; ///< Number of milliseconds per tick, 32 in this case
  96. static F32 smTickSec; ///< Fraction of a second per tick
  97. static U32 smTickMask;
  98. // This just makes life easy
  99. typedef Vector<ITickable *>::iterator ProcessListIterator;
  100. /// Returns a reference to the list of all ITickable objects.
  101. static Vector<ITickable *>& getProcessList();
  102. bool mProcessTick; ///< Set to true if this object wants tick processing
  103. protected:
  104. /// This method is called every frame and lets the control interpolate between
  105. /// ticks so you can smooth things as long as isProcessingTicks returns true
  106. /// when it is called on the object
  107. virtual void interpolateTick( F32 delta ) = 0;
  108. /// This method is called once every 32ms if isProcessingTicks returns true
  109. /// when called on the object
  110. virtual void processTick() = 0;
  111. /// This method is called once every frame regardless of the return value of
  112. /// isProcessingTicks and informs the object of the passage of time.
  113. /// @param timeDelta Time increment in seconds.
  114. virtual void advanceTime( F32 timeDelta ) = 0;
  115. public:
  116. /// Constructor
  117. /// This will add the object to the process list
  118. ITickable();
  119. /// Destructor
  120. /// Remove this object from the process list
  121. virtual ~ITickable();
  122. /// Is this object wanting to receive tick notifications
  123. /// @returns True if object wants tick notifications
  124. bool isProcessingTicks() const { return mProcessTick; };
  125. /// Sets this object as either tick processing or not
  126. /// @param tick True if this object should process ticks
  127. virtual void setProcessTicks( bool tick = true );
  128. /// Initialise the ITickable system.
  129. static void init( const U32 tickShift );
  130. /// Gets the Tick bit-shift.
  131. static U32 getTickShift() { return smTickShift; }
  132. /// Gets the Tick (ms)
  133. static U32 getTickMs() { return smTickMs; }
  134. /// Gets the Tick (seconds)
  135. static F32 getTickSec() { return smTickSec; }
  136. /// Gets the Tick mask.
  137. static U32 getTickMask() { return smTickMask; }
  138. //------------------------------------------------------------------------------
  139. /// This is called in clientProcess to advance the time for all ITickable
  140. /// objects.
  141. /// @param timeDelta Time increment in milliseconds.
  142. /// @returns True if any ticks were sent
  143. /// @see clientProcess
  144. static bool advanceTime( U32 timeDelta );
  145. };
  146. //------------------------------------------------------------------------------
  147. inline void ITickable::setProcessTicks( bool tick /* = true */ )
  148. {
  149. mProcessTick = tick;
  150. }
  151. #endif