Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00040 #include "GHOST_TimerManager.h" 00041 00042 #include <algorithm> 00043 00044 #include "GHOST_TimerTask.h" 00045 00046 00047 GHOST_TimerManager::GHOST_TimerManager() 00048 { 00049 } 00050 00051 00052 GHOST_TimerManager::~GHOST_TimerManager() 00053 { 00054 disposeTimers(); 00055 } 00056 00057 00058 GHOST_TUns32 GHOST_TimerManager::getNumTimers() 00059 { 00060 return (GHOST_TUns32)m_timers.size(); 00061 } 00062 00063 00064 bool GHOST_TimerManager::getTimerFound(GHOST_TimerTask* timer) 00065 { 00066 TTimerVector::const_iterator iter = std::find(m_timers.begin(), m_timers.end(), timer); 00067 return iter != m_timers.end(); 00068 } 00069 00070 00071 GHOST_TSuccess GHOST_TimerManager::addTimer(GHOST_TimerTask* timer) 00072 { 00073 GHOST_TSuccess success; 00074 if (!getTimerFound(timer)) { 00075 // Add the timer task 00076 m_timers.push_back(timer); 00077 success = GHOST_kSuccess; 00078 } 00079 else { 00080 success = GHOST_kFailure; 00081 } 00082 return success; 00083 } 00084 00085 00086 GHOST_TSuccess GHOST_TimerManager::removeTimer(GHOST_TimerTask* timer) 00087 { 00088 GHOST_TSuccess success; 00089 TTimerVector::iterator iter = std::find(m_timers.begin(), m_timers.end(), timer); 00090 if (iter != m_timers.end()) { 00091 // Remove the timer task 00092 m_timers.erase(iter); 00093 delete timer; 00094 timer = 0; 00095 success = GHOST_kSuccess; 00096 } 00097 else { 00098 success = GHOST_kFailure; 00099 } 00100 return success; 00101 } 00102 00103 GHOST_TUns64 GHOST_TimerManager::nextFireTime() 00104 { 00105 GHOST_TUns64 smallest = GHOST_kFireTimeNever; 00106 TTimerVector::iterator iter; 00107 00108 for (iter = m_timers.begin(); iter != m_timers.end(); iter++) { 00109 GHOST_TUns64 next = (*iter)->getNext(); 00110 00111 if (next<smallest) 00112 smallest = next; 00113 } 00114 00115 return smallest; 00116 } 00117 00118 bool GHOST_TimerManager::fireTimers(GHOST_TUns64 time) 00119 { 00120 TTimerVector::iterator iter; 00121 bool anyProcessed = false; 00122 00123 for (iter = m_timers.begin(); iter != m_timers.end(); iter++) { 00124 if (fireTimer(time, *iter)) 00125 anyProcessed = true; 00126 } 00127 00128 return anyProcessed; 00129 } 00130 00131 00132 bool GHOST_TimerManager::fireTimer(GHOST_TUns64 time, GHOST_TimerTask* task) 00133 { 00134 GHOST_TUns64 next = task->getNext(); 00135 00136 // Check if the timer should be fired 00137 if (time > next) { 00138 // Fire the timer 00139 GHOST_TimerProcPtr timerProc = task->getTimerProc(); 00140 GHOST_TUns64 start = task->getStart(); 00141 timerProc(task, time - start); 00142 00143 // Update the time at which we will fire it again 00144 GHOST_TUns64 interval = task->getInterval(); 00145 GHOST_TUns64 numCalls = (next - start) / interval; 00146 numCalls++; 00147 next = start + numCalls * interval; 00148 task->setNext(next); 00149 00150 return true; 00151 } else { 00152 return false; 00153 } 00154 } 00155 00156 00157 void GHOST_TimerManager::disposeTimers() 00158 { 00159 while (m_timers.size() > 0) { 00160 delete m_timers[0]; 00161 m_timers.erase(m_timers.begin()); 00162 } 00163 }