Visualization Library 2.1.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Time.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlCore/Time.hpp>
33 #include <ctime>
34 #include <cmath>
35 
36 #if defined(VL_PLATFORM_LINUX) || defined(VL_PLATFORM_MACOSX)
37  #include <sys/time.h> // gettimeofday()
38  #include <unistd.h> // usleep()
39 #endif
40 
41 using namespace vl;
42 
43 //-----------------------------------------------------------------------------
44 // Time
45 //-----------------------------------------------------------------------------
47 {
48  VL_DEBUG_SET_OBJECT_NAME()
49 
50  for(int i=0; i<VL_MAX_TIMERS; ++i)
51  mStart[i] = -1;
52 
53  #if defined(VL_PLATFORM_WINDOWS)
54  SYSTEMTIME local_time;
55  GetLocalTime(&local_time);
56  mYear = local_time.wYear;
57  mMonth = local_time.wMonth;
58  mDayOfWeek = local_time.wDayOfWeek;
59  mDayOfMonth = local_time.wDay;
60  mHour = local_time.wHour;
61  mMinute = local_time.wMinute;
62  mSecond = local_time.wSecond;
63  mMicrosecond = local_time.wMilliseconds * 1000;
64  #elif defined(__GNUG__)
65  time_t secs;
66  ::time(&secs);
67  tm* date = localtime( &secs );
68 
69  mYear = date->tm_year + 1900;
70  mMonth = date->tm_mon;
71  mDayOfWeek = date->tm_wday;
72  mDayOfMonth = date->tm_mday;
73  mHour = date->tm_hour;
74  mMinute = date->tm_min;
75  mSecond = date->tm_sec;
76 
77  struct timeval tv;
78  gettimeofday( &tv, NULL );
79  mMicrosecond = tv.tv_usec;
80  #endif
81 }
82 //-----------------------------------------------------------------------------
83 namespace vl
84 {
85  unsigned long long gStartTime = 0;
86 
88  {
89  #if defined(VL_PLATFORM_WINDOWS)
90  LARGE_INTEGER Frequency;
91  LARGE_INTEGER PerformanceCount;
92  BOOL has_timer = QueryPerformanceFrequency( &Frequency );
93  if (has_timer)
94  {
95  // DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 1);
96  QueryPerformanceCounter( &PerformanceCount );
97  // ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
98  gStartTime = PerformanceCount.QuadPart;
99  }
100  else
101  {
102  gStartTime = GetTickCount();
103  }
104  #elif defined(__GNUG__)
105  struct timeval tv;
106  gettimeofday( &tv, NULL );
107  gStartTime = (unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec;
108  #endif
109  }
110 }
111 //-----------------------------------------------------------------------------
115 {
116  if (gStartTime == 0)
117  initStartTime();
118 
120 
121  #if defined(VL_PLATFORM_WINDOWS)
122  // Win32
123  LARGE_INTEGER Frequency;
124  LARGE_INTEGER PerformanceCount;
125  BOOL has_timer = QueryPerformanceFrequency( &Frequency );
126  if (has_timer)
127  {
128  // DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 1);
129  QueryPerformanceCounter( &PerformanceCount );
130  // ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
131  return (real)(PerformanceCount.QuadPart-gStartTime)/Frequency.QuadPart;
132  }
133  else
134  {
135  return (GetTickCount()-gStartTime) / 1000.0f;
136  }
137  #elif defined(__GNUG__)
138  struct timeval tv;
139  gettimeofday( &tv, NULL );
140  return ((unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec - gStartTime) * 0.000001f;
141  #endif
142 }
143 //-----------------------------------------------------------------------------
144 void Time::sleep(unsigned int milliseconds)
145 {
146  #if defined(VL_PLATFORM_WINDOWS)
147  Sleep(milliseconds);
148  #elif defined(__GNUG__)
149  usleep(milliseconds*1000);
150  #endif
151 }
152 //-----------------------------------------------------------------------------
int mHour
Definition: Time.hpp:89
int mMicrosecond
Definition: Time.hpp:92
int mSecond
Definition: Time.hpp:91
int mDayOfWeek
Definition: Time.hpp:87
int mYear
Definition: Time.hpp:85
int mMonth
Definition: Time.hpp:86
int mMinute
Definition: Time.hpp:90
Visualization Library main namespace.
unsigned long long gStartTime
Definition: Time.cpp:85
Time()
Definition: Time.cpp:46
real mStart[VL_MAX_TIMERS]
Definition: Time.hpp:94
#define NULL
Definition: OpenGLDefs.hpp:81
int mDayOfMonth
Definition: Time.hpp:88
static real currentTime()
Seconds passed from an arbitrary origin QueryPerformanceFrequency should be called only once in the a...
Definition: Time.cpp:114
static void sleep(unsigned int milliseconds)
Definition: Time.cpp:144
#define VL_CHECK(expr)
Definition: checks.hpp:73
void initStartTime()
Definition: Time.cpp:87