# Visualization Library

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

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

- Main Page
- 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.com                                               */
    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 // mingw and msvc
    00037 #if defined(_WIN32)
    00038   #include <windows.h>
    00039 // linux and mac
    00040 #elif defined(__GNUG__)
    00041   #include <sys/time.h> // gettimeofday()
    00042   #include <unistd.h> // usleep()
    00043 #endif
    00044
    00045 using namespace vl;
    00046
    00047 namespace
    00048 {
    00049   double init_start_time()
    00050   {
    00051     #if defined(_WIN32)
    00052       // Win32
    00053       LARGE_INTEGER Frequency;
    00054       LARGE_INTEGER PerformanceCount;
    00055       BOOL has_timer = QueryPerformanceFrequency( &Frequency );
    00056       if (has_timer)
    00057       {
    00058         QueryPerformanceCounter( &PerformanceCount );
    00059         return (double)PerformanceCount.QuadPart/Frequency.QuadPart;
    00060       }
    00061       else
    00062       {
    00063         return GetTickCount() / 1000.0;
    00064       }
    00065     #elif defined(__GNUG__)
    00066       struct timeval tv;
    00067       gettimeofday( &tv, NULL );
    00068       return tv.tv_sec + tv.tv_usec * 0.000001;
    00069     #endif
    00070   }
    00071   double mStartTime =  init_start_time();
    00072 }
    00073
    00074 //-----------------------------------------------------------------------------
    00075 // Time
    00076 //-----------------------------------------------------------------------------
    00077 Time::Time()
    00078 {
    00079   VL_DEBUG_SET_OBJECT_NAME()
    00080
    00081   for(int i=0; i<VL_MAX_TIMERS; ++i)
    00082     mStart[i] = -1;
    00083
    00084   #if defined(_WIN32)
    00085     SYSTEMTIME local_time;
    00086     GetLocalTime(&local_time);
    00087     mYear = local_time.wYear;
    00088     mMonth = local_time.wMonth;
    00089     mDayOfWeek = local_time.wDayOfWeek;
    00090     mDayOfMonth = local_time.wDay;
    00091     mHour = local_time.wHour;
    00092     mMinute = local_time.wMinute;
    00093     mSecond = local_time.wSecond;
    00094     mMicrosecond = local_time.wMilliseconds * 1000;
    00095   #elif defined(__GNUG__)
    00096     time_t secs;
    00097     ::time(&secs);
    00098     tm* date = localtime( &secs );
    00099
    00100     mYear = date->tm_year + 1900;
    00101     mMonth = date->tm_mon;
    00102     mDayOfWeek = date->tm_wday;
    00103     mDayOfMonth = date->tm_mday;
    00104     mHour = date->tm_hour;
    00105     mMinute = date->tm_min;
    00106     mSecond = date->tm_sec;
    00107
    00108     struct timeval tv;
    00109     gettimeofday( &tv, NULL );
    00110     mMicrosecond = tv.tv_usec;
    00111   #endif
    00112 }
    00113 //-----------------------------------------------------------------------------
    00116 Real Time::currentTime()
    00117 {
    00118   #if defined(_WIN32)
    00119     // Win32
    00120     LARGE_INTEGER Frequency;
    00121     LARGE_INTEGER PerformanceCount;
    00122     BOOL has_timer = QueryPerformanceFrequency( &Frequency );
    00123     if (has_timer)
    00124     {
    00125       QueryPerformanceCounter( &PerformanceCount );
    00126       return (Real)PerformanceCount.QuadPart/Frequency.QuadPart  - mStartTime;
    00127     }
    00128     else
    00129     {
    00130       return (Real)(GetTickCount() / 1000.0 - mStartTime);
    00131     }
    00132   #elif defined(__GNUG__)
    00133     struct timeval tv;
    00134     gettimeofday( &tv, NULL );
    00135     return (Real)(tv.tv_sec + tv.tv_usec * 0.000001 - mStartTime);
    00136   #endif
    00137 }
    00138 //-----------------------------------------------------------------------------
    00139 void Time::sleep(unsigned int milliseconds)
    00140 {
    00141   #if defined(_WIN32)
    00142     Sleep(milliseconds);
    00143   #elif defined(__GNUG__)
    00144     usleep(milliseconds*1000);
    00145   #endif
    00146 }
    00147 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:22.
Permission is granted to use this page to write and publish articles regarding Visualization Library.