# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/Time.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.org                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlCore/Time.hpp>
    00033 #include <ctime>
    00034 #include <cmath>
    00035
    00036 #if defined(VL_PLATFORM_LINUX) || defined(VL_PLATFORM_MACOSX)
    00037   #include <sys/time.h> // gettimeofday()
    00038   #include <unistd.h>   // usleep()
    00039 #endif
    00040
    00041 using namespace vl;
    00042
    00043 //-----------------------------------------------------------------------------
    00044 // Time
    00045 //-----------------------------------------------------------------------------
    00046 Time::Time()
    00047 {
    00048   VL_DEBUG_SET_OBJECT_NAME()
    00049
    00050   for(int i=0; i<VL_MAX_TIMERS; ++i)
    00051     mStart[i] = -1;
    00052
    00053   #if defined(VL_PLATFORM_WINDOWS)
    00054     SYSTEMTIME local_time;
    00055     GetLocalTime(&local_time);
    00056     mYear = local_time.wYear;
    00057     mMonth = local_time.wMonth;
    00058     mDayOfWeek = local_time.wDayOfWeek;
    00059     mDayOfMonth = local_time.wDay;
    00060     mHour = local_time.wHour;
    00061     mMinute = local_time.wMinute;
    00062     mSecond = local_time.wSecond;
    00063     mMicrosecond = local_time.wMilliseconds * 1000;
    00064   #elif defined(__GNUG__)
    00065     time_t secs;
    00066     ::time(&secs);
    00067     tm* date = localtime( &secs );
    00068
    00069     mYear = date->tm_year + 1900;
    00070     mMonth = date->tm_mon;
    00071     mDayOfWeek = date->tm_wday;
    00072     mDayOfMonth = date->tm_mday;
    00073     mHour = date->tm_hour;
    00074     mMinute = date->tm_min;
    00075     mSecond = date->tm_sec;
    00076
    00077     struct timeval tv;
    00078     gettimeofday( &tv, NULL );
    00079     mMicrosecond = tv.tv_usec;
    00080   #endif
    00081 }
    00082 //-----------------------------------------------------------------------------
    00083 namespace vl
    00084 {
    00085   unsigned long long gStartTime = 0;
    00086
    00087   void initStartTime()
    00088   {
    00089     #if defined(VL_PLATFORM_WINDOWS)
    00090       LARGE_INTEGER Frequency;
    00091       LARGE_INTEGER PerformanceCount;
    00092       BOOL has_timer = QueryPerformanceFrequency( &Frequency );
    00093       if (has_timer)
    00094       {
    00095         // DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 1);
    00096         QueryPerformanceCounter( &PerformanceCount );
    00097         // ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
    00098         gStartTime = PerformanceCount.QuadPart;
    00099       }
    00100       else
    00101       {
    00102         gStartTime = GetTickCount();
    00103       }
    00104     #elif defined(__GNUG__)
    00105       struct timeval tv;
    00106       gettimeofday( &tv, NULL );
    00107       gStartTime = (unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec;
    00108     #endif
    00109   }
    00110 }
    00111 //-----------------------------------------------------------------------------
    00114 real Time::currentTime()
    00115 {
    00116   if (gStartTime == 0)
    00117     initStartTime();
    00118
    00119   VL_CHECK(gStartTime);
    00120
    00121   #if defined(VL_PLATFORM_WINDOWS)
    00122     // Win32
    00123     LARGE_INTEGER Frequency;
    00124     LARGE_INTEGER PerformanceCount;
    00125     BOOL has_timer = QueryPerformanceFrequency( &Frequency );
    00126     if (has_timer)
    00127     {
    00128       // DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 1);
    00129       QueryPerformanceCounter( &PerformanceCount );
    00130       // ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
    00131       return (real)(PerformanceCount.QuadPart-gStartTime)/Frequency.QuadPart;
    00132     }
    00133     else
    00134     {
    00135       return (GetTickCount()-gStartTime) / 1000.0f;
    00136     }
    00137   #elif defined(__GNUG__)
    00138     struct timeval tv;
    00139     gettimeofday( &tv, NULL );
    00140     return ((unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec - gStartTime) * 0.000001f;
    00141   #endif
    00142 }
    00143 //-----------------------------------------------------------------------------
    00144 void Time::sleep(unsigned int milliseconds)
    00145 {
    00146   #if defined(VL_PLATFORM_WINDOWS)
    00147     Sleep(milliseconds);
    00148   #elif defined(__GNUG__)
    00149     usleep(milliseconds*1000);
    00150   #endif
    00151 }
    00152 //-----------------------------------------------------------------------------

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:31.
Permission is granted to use this page to write and publish articles regarding Visualization Library.