Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef TIMEDEVENT_H
  7. #define TIMEDEVENT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. // This class triggers events at a specified rate. Just call NextEvent() and do an event until it
  12. // returns false. For example, if you want to spawn particles 10 times per second, do this:
  13. // pTimer->SetRate(10);
  14. // float tempDelta = fTimeDelta;
  15. // while(pTimer->NextEvent(tempDelta))
  16. // spawn a particle
  17. class TimedEvent
  18. {
  19. public:
  20. TimedEvent()
  21. {
  22. m_TimeBetweenEvents = -1;
  23. m_fNextEvent = 0;
  24. }
  25. // Rate is in events per second (ie: rate of 15 will trigger 15 events per second).
  26. inline void Init(float rate)
  27. {
  28. m_TimeBetweenEvents = 1.0f / rate;
  29. m_fNextEvent = 0;
  30. }
  31. inline void ResetRate(float rate)
  32. {
  33. m_TimeBetweenEvents = 1.0f / rate;
  34. }
  35. inline bool NextEvent(float &curDelta)
  36. {
  37. // If this goes off, you didn't call Init().
  38. Assert( m_TimeBetweenEvents != -1 );
  39. if(curDelta >= m_fNextEvent)
  40. {
  41. curDelta -= m_fNextEvent;
  42. m_fNextEvent = m_TimeBetweenEvents;
  43. return true;
  44. }
  45. else
  46. {
  47. m_fNextEvent -= curDelta;
  48. return false;
  49. }
  50. }
  51. private:
  52. float m_TimeBetweenEvents;
  53. float m_fNextEvent; // When the next event should be triggered.
  54. };
  55. #endif // TIMEDEVENT_H