Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/vos/timer.cc

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001-2003 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <tetron@interreality.org>
00022 */
00023 
00024 #if defined(_WIN32) && defined(_MSC_VER)
00025 # include <vos/corelibs/vosconfig-vc7.h>
00026 #else
00027 # include <vos/corelibs/vosconfig.h>
00028 #endif
00029 
00030 #ifdef HAVE_GETTIMEOFDAY
00031 # ifdef HAVE_SYS_TIME_H
00032 #  include <sys/time.h>
00033 # else
00034 #  warning Note: According to autoconf, you have gettimeofday(), but not <sys/time.h>. I wonder where gettimeofday() is defined.
00035 # endif
00036 #else
00037 # ifdef WIN32
00038 #  include <windows.h>
00039 # else
00040 #  error I do not know about time on your system
00041 # endif
00042 #endif
00043 
00044 #include <time.h>
00045 
00046 #if defined(_WIN32) && defined(_MSC_VER)
00047 # ifdef VOS_EXPORTS
00048 #  define VOS_API __declspec(dllexport)
00049 # else
00050 #  define VOS_API __declspec(dllimport)
00051 # endif
00052 #else
00053 # define VOS_API
00054 #endif
00055 
00056 namespace VOS
00057 {
00058 
00059 #ifdef HAVE_GETTIMEOFDAY
00060 VOS_API double getRealTime()
00061 {
00062     double now;
00063 
00064     struct timeval tv;
00065     gettimeofday(&tv, 0);
00066     now = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
00067     
00068     return now;
00069 }
00070 #else
00071 # ifdef WIN32
00072 // The following code to use the Windows high-resolution timer
00073 // was borrowed from the Crystal Space project :-)
00074 // The "LARGE_INTEGER" type is at least 64 bits
00075 VOS_API double getRealTime()
00076 {
00077 #if defined(__CYGWIN32__)
00078 #       define __int64 long long
00079 #endif  
00080 
00081     static __int64 Freq       = 0;
00082     static __int64 FirstCount = 0;
00083     static double startTime   = 0;
00084 
00085   //Freq was set to -1, if the current Hardware does not support
00086   //high resolution timers. We will use GetTickCount instead then.
00087   if (Freq <= 0)
00088   {
00089       FILETIME ft;
00090       LARGE_INTEGER _100nanosecs;
00091 
00092       GetSystemTimeAsFileTime(&ft);
00093 
00094       _100nanosecs.LowPart = ft.dwLowDateTime;
00095       _100nanosecs.HighPart = ft.dwHighDateTime;
00096 
00097     // the ridiculous large figure is the number of 100 ns
00098     // intervals from 1601-01-01 to 1970-01-01 
00099     // (to bring it in line with the unix epoch)
00100 #if defined(__CYGWIN32__)
00101     _100nanosecs.QuadPart -= 116444736000000000ULL;
00102 #else
00103     _100nanosecs.QuadPart -= 116444736000000000;
00104 #endif
00105       startTime = (_100nanosecs.QuadPart / 10000000.0) + (((_100nanosecs.QuadPart / 10) % 1000000) / 1000000.0);
00106       if(Freq < 0) return startTime;
00107   }
00108 
00109   //Freq is 0, the first time this function is being called.
00110   if (Freq == 0)
00111   {
00112       //try to determine the frequency of the high resulution timer
00113       if (!QueryPerformanceFrequency((LARGE_INTEGER*)&Freq))
00114       {
00115           //There is no such timer....
00116           Freq=-1;
00117           return getRealTime();
00118       }
00119       // Start counting from first time this function is called.
00120       QueryPerformanceCounter((LARGE_INTEGER*)&FirstCount);
00121   }
00122 
00123   //retrieve current count
00124   __int64 Count = 0;
00125   QueryPerformanceCounter((LARGE_INTEGER*)&Count);
00126 
00127   return startTime + ((double)(Count-FirstCount))/((double)Freq);
00128 }
00129 #else
00130 #error bad configuration or do not know how to do time on this system
00131 #endif // WIN32
00132 #endif // HAVE_GETTIMEOFDAY_H
00133 
00134 VOS_API double getTimer()
00135 {
00136     static double lasttime = 0.0;
00137     double now = getRealTime();
00138     double delta = now - lasttime;
00139 
00140     if(delta < 0.0) delta = 0.000001;
00141     lasttime += delta;
00142 
00143     return lasttime;
00144 }
00145 
00146 }

Generated on Tue Aug 12 03:55:42 2003 for Interreality Project - VOS by doxygen 1.3.2