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.

151 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "timedeventmgr.h"
  8. #include "tier0/vprof.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. // ------------------------------------------------------------------------------------------ //
  12. // CEventRegister.
  13. // ------------------------------------------------------------------------------------------ //
  14. CEventRegister::CEventRegister()
  15. {
  16. m_bRegistered = false;
  17. m_pEventMgr = NULL;
  18. }
  19. CEventRegister::~CEventRegister()
  20. {
  21. Term();
  22. }
  23. void CEventRegister::Init( CTimedEventMgr *pMgr, IEventRegisterCallback *pCallback )
  24. {
  25. Term();
  26. m_pEventMgr = pMgr;
  27. m_pCallback = pCallback;
  28. }
  29. void CEventRegister::Term()
  30. {
  31. // Unregister.
  32. if ( m_pEventMgr && m_bRegistered )
  33. {
  34. m_pEventMgr->RemoveEvent( this );
  35. }
  36. }
  37. void CEventRegister::SetUpdateInterval( float interval )
  38. {
  39. Assert( m_pEventMgr );
  40. if ( m_pEventMgr )
  41. {
  42. // Register for this event.
  43. m_flUpdateInterval = interval;
  44. m_flNextEventTime = gpGlobals->curtime + m_flUpdateInterval;
  45. m_pEventMgr->RegisterForNextEvent( this );
  46. }
  47. }
  48. void CEventRegister::StopUpdates()
  49. {
  50. if ( m_pEventMgr )
  51. {
  52. // Unregister our next event.
  53. m_pEventMgr->RemoveEvent( this );
  54. }
  55. }
  56. void CEventRegister::Reregister()
  57. {
  58. if ( m_flUpdateInterval > 1e-6 && m_pEventMgr )
  59. {
  60. while ( m_flNextEventTime <= gpGlobals->curtime )
  61. {
  62. m_flNextEventTime += m_flUpdateInterval;
  63. }
  64. m_pEventMgr->RegisterForNextEvent( this );
  65. }
  66. }
  67. // ------------------------------------------------------------------------------------------ //
  68. // CTimedEventMgr.
  69. // ------------------------------------------------------------------------------------------ //
  70. bool TimedEventMgr_LessFunc( CEventRegister* const &a, CEventRegister* const &b )
  71. {
  72. return a->m_flNextEventTime > b->m_flNextEventTime;
  73. }
  74. CTimedEventMgr::CTimedEventMgr()
  75. {
  76. m_Events.SetLessFunc( TimedEventMgr_LessFunc );
  77. }
  78. void CTimedEventMgr::FireEvents()
  79. {
  80. VPROF( "CTimedEventMgr::FireEvents" );
  81. while ( m_Events.Count() )
  82. {
  83. // Fire the top element, then break out.
  84. CEventRegister *pEvent = m_Events.ElementAtHead();
  85. if ( gpGlobals->curtime >= pEvent->m_flNextEventTime )
  86. {
  87. // Reregister for the timed event, then fire the callback for the event.
  88. m_Events.RemoveAtHead();
  89. pEvent->m_bRegistered = false;
  90. pEvent->Reregister();
  91. pEvent->m_pCallback->FireEvent();
  92. }
  93. else
  94. {
  95. break;
  96. }
  97. }
  98. }
  99. void CTimedEventMgr::RegisterForNextEvent( CEventRegister *pEvent )
  100. {
  101. RemoveEvent( pEvent );
  102. m_Events.Insert( pEvent );
  103. pEvent->m_bRegistered = true;
  104. }
  105. void CTimedEventMgr::RemoveEvent( CEventRegister *pEvent )
  106. {
  107. if ( pEvent->m_bRegistered )
  108. {
  109. // Find the event in the list and remove it.
  110. int cnt = m_Events.Count();
  111. for ( int i=0; i < cnt; i++ )
  112. {
  113. if ( m_Events.Element( i ) == pEvent )
  114. {
  115. m_Events.RemoveAt( i );
  116. break;
  117. }
  118. }
  119. }
  120. }