00001 #include "vuHWTimer.h"
00002
00003 #if defined(WIN32)
00004
00005 vuHWTimer::vuHWTimer()
00006 {
00007 LARGE_INTEGER freq;
00008
00009 if (QueryPerformanceFrequency(&freq))
00010 {
00011 m_IsSupported = true;
00012 m_FloatFreq = (float)freq.QuadPart;
00013 m_DoubleFreq = (double)freq.QuadPart;
00014 }
00015 else
00016 {
00017 m_IsSupported = false;
00018 m_FloatFreq = 1.0f;
00019 m_DoubleFreq = 1.0;
00020 }
00021 }
00022
00023 vuHWTimer::vuHWTimer(const vuHWTimer& t)
00024 {
00025 m_LastCount = t.m_LastCount;
00026 m_FloatFreq = t.m_FloatFreq;
00027 m_DoubleFreq = t.m_DoubleFreq;
00028 m_IsSupported = t.m_IsSupported;
00029 }
00030
00031 vuHWTimer::~vuHWTimer()
00032 {
00033 }
00034
00035 vuHWTimer& vuHWTimer::operator=(const vuHWTimer& rhs)
00036 {
00037 if (this != &rhs)
00038 {
00039 m_LastCount = rhs.m_LastCount;
00040 m_FloatFreq = rhs.m_FloatFreq;
00041 m_DoubleFreq = rhs.m_DoubleFreq;
00042 m_IsSupported = rhs.m_IsSupported;
00043 }
00044 return *this;
00045 }
00046
00047 bool vuHWTimer::isSupported(void) const
00048 {
00049 return m_IsSupported;
00050 }
00051
00052 float vuHWTimer::getElapsedTimeFloat(void)
00053 {
00054 LARGE_INTEGER count;
00055 float time;
00056
00057 QueryPerformanceCounter(&count);
00058 time = (float)(count.QuadPart - m_LastCount.QuadPart)/m_FloatFreq;
00059 m_LastCount = count;
00060
00061 return time;
00062 }
00063
00064 double vuHWTimer::getElapsedTimeDouble(void)
00065 {
00066 LARGE_INTEGER count;
00067 double time;
00068
00069 QueryPerformanceCounter(&count);
00070 time = (double)(count.QuadPart - m_LastCount.QuadPart)/m_DoubleFreq;
00071 m_LastCount = count;
00072
00073 return time;
00074 }
00075
00076 void vuHWTimer::sleepFloat(float s)
00077 {
00078 LARGE_INTEGER count;
00079 float time = -1.0f;
00080
00081 if (s < 0.0f)
00082 return;
00083
00084 while(time < s)
00085 {
00086 QueryPerformanceCounter(&count);
00087 time = (float)(count.QuadPart - m_LastCount.QuadPart)/m_FloatFreq;
00088 }
00089 m_LastCount = count;
00090
00091 return;
00092 }
00093
00094 void vuHWTimer::sleepDouble(double s)
00095 {
00096 LARGE_INTEGER count;
00097 double time = -1.0f;
00098
00099 if (s < 0.0f)
00100 return;
00101
00102 while(time < s)
00103 {
00104 QueryPerformanceCounter(&count);
00105 time = (double)(count.QuadPart - m_LastCount.QuadPart)/m_DoubleFreq;
00106 }
00107 m_LastCount = count;
00108
00109 return;
00110 }
00111
00112 #else
00113
00114 #define TIMEVALDIFFFLOAT(t1, t2) (((((float)(t1.tv_sec-t2.tv_sec))*1000000.0f) + ((float)(t1.tv_usec-t2.tv_usec)))/1000000.0f)
00115 #define TIMEVALDIFFDOUBLE(t1, t2) \
00116 (((((double)(t1.tv_sec-t2.tv_sec))*1000000.0) + \
00117 ((double)(t1.tv_usec-t2.tv_usec)))/1000000.0)
00118
00119 vuHWTimer::vuHWTimer()
00120 {
00121 struct timeval tv;
00122
00123 m_IsSupported = (gettimeofday(&tv, 0) == 0);
00124 }
00125
00126 vuHWTimer::vuHWTimer(const vuHWTimer& t)
00127 {
00128 m_LastCount.tv_sec = t.m_LastCount.tv_sec;
00129 m_LastCount.tv_usec = t.m_LastCount.tv_usec;
00130 m_IsSupported = t.m_IsSupported;
00131 }
00132
00133 vuHWTimer::~vuHWTimer()
00134 {
00135 }
00136
00137 vuHWTimer& vuHWTimer::operator=(const vuHWTimer& rhs)
00138 {
00139 if (this != &rhs)
00140 {
00141 m_LastCount.tv_sec = rhs.m_LastCount.tv_sec;
00142 m_LastCount.tv_usec = rhs.m_LastCount.tv_usec;
00143 m_IsSupported = rhs.m_IsSupported;
00144 }
00145 return *this;
00146 }
00147
00148 bool vuHWTimer::isSupported(void) const
00149 {
00150 return m_IsSupported;
00151 }
00152
00153 float vuHWTimer::getElapsedTimeFloat(void)
00154 {
00155 struct timeval count;
00156 float time;
00157
00158 gettimeofday(&count, 0);
00159 time = TIMEVALDIFFFLOAT(count, m_LastCount);
00160 m_LastCount.tv_sec = count.tv_sec;
00161 m_LastCount.tv_usec = count.tv_usec;
00162
00163 return time;
00164 }
00165
00166 double vuHWTimer::getElapsedTimeDouble(void)
00167 {
00168 struct timeval count;
00169 double time;
00170
00171 gettimeofday(&count, 0);
00172 time = TIMEVALDIFFDOUBLE(count, m_LastCount);
00173 m_LastCount.tv_sec = count.tv_sec;
00174 m_LastCount.tv_usec = count.tv_usec;
00175
00176 return time;
00177 }
00178
00179 void vuHWTimer::sleepFloat(float s)
00180 {
00181 struct timeval count;
00182 float time = -1.0f;
00183
00184 if (s < 0.0f)
00185 return;
00186
00187 while(time < s)
00188 {
00189 gettimeofday(&count, 0);
00190 time = TIMEVALDIFFFLOAT(count, m_LastCount);
00191 }
00192 m_LastCount.tv_sec = count.tv_sec;
00193 m_LastCount.tv_usec = count.tv_usec;
00194
00195 return;
00196 }
00197
00198 void vuHWTimer::sleepDouble(double s)
00199 {
00200 struct timeval count;
00201 double time = -1.0f;
00202
00203 if (s < 0.0f)
00204 return;
00205
00206 while(time < s)
00207 {
00208 gettimeofday(&count, 0);
00209 time = TIMEVALDIFFDOUBLE(count, m_LastCount);
00210 }
00211 m_LastCount.tv_sec = count.tv_sec;
00212 m_LastCount.tv_usec = count.tv_usec;
00213
00214 return;
00215 }
00216
00217 #endif