Tickable.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _TICKABLE_H_
  23. #define _TICKABLE_H_
  24. #include "collection/vector.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 Tickable
  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 Tickable
  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 Tickable, 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 Tickable, 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 inheritance is done: public <b>virtual</b> Tickable
  49. /// It is very important that you keep the virtual keyword in there, otherwise
  50. /// proper behavior is not guaranteed. 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 Tickable 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. /// receive 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 frame-rate. So you may get 200 advanceTime calls in a second, or you
  68. /// may only get 20. There is no way of assuring consistent 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 Tickable 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.
  76. ///
  77. /// @todo Support processBefore/After and move the GameBase processing over to use Tickable
  78. class Tickable
  79. {
  80. private:
  81. static U32 smLastTick; ///< Time of the last tick that occurred
  82. static U32 smLastTime; ///< Last time value at which advanceTime was called
  83. static U32 smLastDelta; ///< Last delta value for advanceTime
  84. // This just makes life easy
  85. typedef Vector<Tickable *>::iterator ProcessListIterator;
  86. /// Returns a reference to the list of all Tickable objects.
  87. static Vector<Tickable *>& getProcessList();
  88. /// Indicates whether the object is currently processing ticks or not.
  89. bool mProcessTick;
  90. protected:
  91. /// This method is called every frame and lets the control interpolate between
  92. /// ticks so you can smooth things as long as isProcessingTicks returns true
  93. /// when it is called on the object
  94. virtual void interpolateTick( F32 delta ) = 0;
  95. /// This method is called once every 32ms if isProcessingTicks returns true
  96. /// when called on the object
  97. virtual void processTick() = 0;
  98. /// This method is called once every frame regardless of the return value of
  99. /// isProcessingTicks and informs the object of the passage of time
  100. virtual void advanceTime( F32 timeDelta ) = 0;
  101. public:
  102. // Can let everyone look at these if they want to
  103. static const U32 smTickShift; ///< Shift value to control how often Ticks occur
  104. static const U32 smTickMs; ///< Number of milliseconds per tick, 32 in this case
  105. static const F32 smTickSec; ///< Fraction of a second per tick
  106. static const U32 smTickMask;
  107. /// Constructor
  108. /// This will add the object to the process list
  109. Tickable();
  110. /// Destructor
  111. /// Remove this object from the process list
  112. virtual ~Tickable();
  113. /// Is this object wanting to receive tick notifications
  114. /// @returns True if object wants tick notifications
  115. virtual bool isProcessingTicks() const { return mProcessTick; };
  116. /// Sets this object as either tick processing or not
  117. /// @param tick True if this object should process ticks
  118. virtual void setProcessTicks( bool tick = true );
  119. //------------------------------------------------------------------------------
  120. /// This is called in clientProcess to advance the time for all Tickable
  121. /// objects
  122. /// @returns True if any ticks were sent
  123. /// @see clientProcess
  124. static bool advanceTime( U32 timeDelta );
  125. };
  126. #endif