BVB Source Codes

CRYENGINE Show TimeOfDayScheduler.cpp Source code

Return Download CRYENGINE: download TimeOfDayScheduler.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   TimeOfDayScheduler.cpp
  5. //  Version:     v1.00
  6. //  Created:     27/02/2007 by AlexL
  7. //  Compilers:   Visual Studio.NET
  8. //  Description: TimeOfDayScheduler
  9. // -------------------------------------------------------------------------
  10. //  History:
  11. //
  12. ////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include "StdAfx.h"
  15. #include <Cry3DEngine/ITimeOfDay.h>
  16. #include "TimeOfDayScheduler.h"
  17.  
  18. CTimeOfDayScheduler::CTimeOfDayScheduler()
  19. {
  20.         m_nextId = 0;
  21.         m_lastTime = 0.0f;
  22.         m_bForceUpdate = true;
  23. }
  24.  
  25. CTimeOfDayScheduler::~CTimeOfDayScheduler()
  26. {
  27. }
  28.  
  29. void CTimeOfDayScheduler::Reset()
  30. {
  31.         stl::free_container(m_entries);
  32.         m_nextId = 0;
  33.         m_lastTime = gEnv->p3DEngine->GetTimeOfDay()->GetTime();
  34.         m_bForceUpdate = true;
  35. }
  36.  
  37. CTimeOfDayScheduler::TimeOfDayTimerId CTimeOfDayScheduler::AddTimer(float time, CTimeOfDayScheduler::TimeOfDayTimerCallback callback, void* pUserData)
  38. {
  39.         assert(callback != 0);
  40.         if (callback == 0)
  41.                 return InvalidTimerId;
  42.         SEntry entryForTime(0, time, 0, 0);
  43.         TEntries::iterator iter = std::lower_bound(m_entries.begin(), m_entries.end(), entryForTime);
  44.         m_entries.insert(iter, SEntry(m_nextId, time, callback, pUserData));
  45.         return m_nextId++;
  46. }
  47.  
  48. void* CTimeOfDayScheduler::RemoveTimer(TimeOfDayTimerId id)
  49. {
  50.         TEntries::iterator iter = std::find(m_entries.begin(), m_entries.end(), id);
  51.         if (iter == m_entries.end())
  52.                 return 0;
  53.         void* pUserData = iter->pUserData;
  54.         m_entries.erase(iter);
  55.         return pUserData;
  56. }
  57.  
  58. void CTimeOfDayScheduler::Update()
  59. {
  60.         static const float MIN_DT = 1.0f / 100.0f;
  61.  
  62.         float curTime = gEnv->p3DEngine->GetTimeOfDay()->GetTime();
  63.         float lastTime = m_lastTime;
  64.         const float dt = curTime - lastTime;
  65.  
  66.         // only evaluate if at least some time passed
  67.         if (m_bForceUpdate == false && fabs_tpl(dt) < MIN_DT)
  68.                 return;
  69.         m_bForceUpdate = false;
  70.  
  71.         // execute all entries between lastTime and curTime
  72.         // if curTime < lastTime, we wrapped around and have to process two intervals
  73.         // [lastTime, 24.0] and [0, curTime]
  74.         TEntries processingEntries; // no need to make member var, as allocation/execution currently is NOT too often
  75.         SEntry entryForTime(0, lastTime, 0, 0);
  76.         TEntries::iterator iter = std::lower_bound(m_entries.begin(), m_entries.end(), entryForTime);
  77.         TEntries::iterator iterEnd = m_entries.end();
  78.         const bool bWrap = lastTime > curTime;
  79.         const float maxTime = bWrap ? 24.0f : curTime;
  80.  
  81.         //CryLogAlways("CTOD: lastTime=%f curTime=%f", lastTime, curTime);
  82.  
  83.         // process interval [lastTime, curTime] or in case of wrap [lastTime, 24.0]
  84.         while (iter != iterEnd)
  85.         {
  86.                 const SEntry& entry = *iter;
  87.                 assert(entry.time >= lastTime);
  88.                 if (entry.time > maxTime)
  89.                         break;
  90.                 // CryLogAlways("Adding: %d time=%f", entry.id, entry.time);
  91.                 processingEntries.push_back(entry);
  92.                 ++iter;
  93.         }
  94.  
  95.         if (bWrap) // process interval [0, curTime]
  96.         {
  97.                 iter = m_entries.begin();
  98.                 while (iter != iterEnd)
  99.                 {
  100.                         const SEntry& entry = *iter;
  101.                         if (entry.time > curTime)
  102.                                 break;
  103.                         // CryLogAlways("Adding[wrap]: %d time=%f", entry.id, entry.time);
  104.                         processingEntries.push_back(entry);
  105.                         ++iter;
  106.                 }
  107.         }
  108.  
  109.         iter = processingEntries.begin();
  110.         iterEnd = processingEntries.end();
  111.         while (iter != iterEnd)
  112.         {
  113.                 const SEntry& entry = *iter;
  114.                 entry.callback(entry.id, entry.pUserData, curTime);
  115.                 ++iter;
  116.         }
  117.  
  118.         m_lastTime = curTime;
  119. }
  120.  
downloadTimeOfDayScheduler.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top