![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cOscillator.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "cOscillator.h" 00026 #include "time.h" 00027 #include "constants.h" 00028 00029 namespace n2l 00030 { 00031 const tHertz cOscillator::DefaultFrequency = 1.0f; 00032 00033 00034 /* ************************************************************************* */ 00035 cOscillator::cOscillator() : 00036 m_nextCycle(0), 00037 m_frequency(0), 00038 m_overrunBehaviour(OverrunBehaviour_Skip), 00039 m_warnOnOverrun(false), 00040 m_autoTrigger(AutoTrigger_TriggerOnRequest), 00041 m_timeStep(0) 00042 { 00043 setFrequency(DefaultFrequency); 00044 } 00045 00046 00047 /* ************************************************************************* */ 00048 cOscillator::cOscillator( const tHertz i_frequency, 00049 const tOverrunBehaviour i_ob ) : 00050 m_nextCycle(0), 00051 m_frequency(0), 00052 m_overrunBehaviour(i_ob), 00053 m_warnOnOverrun(false), 00054 m_autoTrigger(AutoTrigger_TriggerOnRequest), 00055 m_timeStep(0) 00056 { 00057 setFrequency(i_frequency); 00058 } 00059 00060 00061 /* ************************************************************************* */ 00062 cOscillator::~cOscillator() 00063 { 00064 } 00065 00066 00067 /* ************************************************************************* */ 00068 const tUint cOscillator::getTimeStepForFreq( const tHertz i_freq ) const 00069 { 00070 if (i_freq<=0) throw cOutOfBoundsException("cOscillator::getTimeStepForFreq","Negative or Zero Frequency"); 00071 return tUint((1000.0/i_freq)+0.5); 00072 } 00073 00074 00075 /* ************************************************************************* */ 00076 const bool cOscillator::isTime() 00077 { 00078 if (!m_frequency) return false; 00079 const tUint Ticks = getTicks(); 00080 if ( (m_warnOnOverrun) && (Ticks>=(m_nextCycle+m_timeStep)) ) 00081 throw cOverdueException(); 00082 if ( (AutoTrigger_TriggerOnRequest==m_autoTrigger)&&(Ticks>=m_nextCycle) ) { 00083 triggered(); 00084 return true; 00085 } else return (Ticks>=m_nextCycle); 00086 } 00087 00088 00089 /* ************************************************************************* */ 00090 void cOscillator::wait() 00091 { 00092 tSint waitTicks = timeLeft(); 00093 if (waitTicks>tSint(PlatformTimerGranularity)) 00094 n2l::delay(waitTicks-(waitTicks%PlatformTimerGranularity)); 00095 while (!isTime()); 00096 } 00097 00098 00099 /* ************************************************************************* */ 00100 const tSint cOscillator::timeLeft() const 00101 { 00102 return tSint(m_nextCycle)-tSint(getTicks()); 00103 } 00104 00105 00106 /* ************************************************************************* */ 00107 void cOscillator::setFrequency( const tHertz i_frequency ) 00108 { 00109 m_frequency = i_frequency; 00110 if (!m_frequency) { 00111 m_timeStep = 0; 00112 return; 00113 } 00114 m_timeStep = getTimeStepForFreq( i_frequency ); 00115 sync(); 00116 } 00117 00118 00119 /* ************************************************************************* */ 00120 void cOscillator::setOverrunBehaviour( const tOverrunBehaviour i_ob ) 00121 { 00122 m_overrunBehaviour = i_ob; 00123 } 00124 00125 00126 /* ************************************************************************* */ 00127 void cOscillator::setOverrunWarningFlag( const tBool i_flag ) 00128 { 00129 m_warnOnOverrun = i_flag; 00130 } 00131 00132 00133 /* ************************************************************************* */ 00134 void cOscillator::setAutoTrigger( const tAutoTrigger i_autoTrigger ) 00135 { 00136 m_autoTrigger = i_autoTrigger; 00137 } 00138 00139 00140 /* ************************************************************************* */ 00141 void cOscillator::sync() 00142 { 00143 m_nextCycle = getTicks()+m_timeStep; 00144 } 00145 00146 00147 /* ************************************************************************* */ 00148 void cOscillator::triggered() 00149 { 00150 if (OverrunBehaviour_Skip==m_overrunBehaviour) { 00151 const tUint Ticks = getTicks(); 00152 while (Ticks>=m_nextCycle) m_nextCycle += m_timeStep; 00153 } else m_nextCycle += m_timeStep; 00154 } 00155 00156 00157 00158 } // namespace n2l |