00001 /*************************************************************************** 00002 * * 00003 * (c) Art Tevs, MPI Informatik Saarbruecken * 00004 * mailto: <tevs@mpi-sb.mpg.de> * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 2 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 ***************************************************************************/ 00012 00013 //---------------------------------------------------------------------------------- 00014 // Includes 00015 //---------------------------------------------------------------------------------- 00016 #include "TimeSource.h" 00017 #include "Log.h" 00018 00019 #include <time.h> 00020 #if NR_PLATFORM != NR_PLATFORM_WIN32 00021 # include <sys/time.h> 00022 #endif 00023 00024 namespace nrEngine{ 00025 00026 //------------------------------------------------------------------------ 00027 TimeSource::TimeSource() : _currentTime(0), _syncTime(0) 00028 { 00029 reset(); 00030 } 00031 00032 //------------------------------------------------------------------------ 00033 TimeSource::~TimeSource() 00034 { 00035 } 00036 00037 //------------------------------------------------------------------------ 00038 float64 TimeSource::getSystemTime() 00039 { 00040 // do not use rdtsc for time calculation, just get time 00041 // by system calls 00042 timeval now; 00043 gettimeofday(&now, NULL); 00044 00045 // some precalculation 00046 float64 a = static_cast<float64>(now.tv_sec) * static_cast<float64>(1000.0); 00047 float64 b = static_cast<float64>(now.tv_usec)/ static_cast<float64>(1000.0); 00048 00049 // clculate the time in seconds 00050 return static_cast<float64>(0.001) * (a+b); 00051 00052 } 00053 00054 //------------------------------------------------------------------------ 00055 float64 TimeSource::getTime() 00056 { 00057 // retrive time information 00058 timeval now; 00059 gettimeofday(&now, NULL); 00060 00061 // some precalculation 00062 float64 a = static_cast<float64>(now.tv_sec - _startTime.tv_sec) * static_cast<float64>(1000.0); 00063 float64 b = static_cast<float64>(now.tv_usec- _startTime.tv_usec)/ static_cast<float64>(1000.0); 00064 00065 // clculate the time in seconds 00066 _currentTime = static_cast<float64>(0.001) * (a+b) + _resetTime + _syncTime; 00067 00068 // return it back 00069 return _currentTime; 00070 } 00071 00072 //------------------------------------------------------------------------ 00073 void TimeSource::reset(float64 startValue) 00074 { 00075 gettimeofday(&_startTime, NULL); 00076 _resetTime = startValue; 00077 } 00078 00079 //------------------------------------------------------------------------ 00080 void TimeSource::sync() 00081 { 00082 _syncTime = _currentTime; 00083 reset(); 00084 } 00085 00086 //------------------------------------------------------------------------ 00087 void TimeSource::notifyNextFrame() 00088 { 00089 } 00090 00091 }; // end namespace 00092