OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "Timer.h"
22 
23 #if defined(_WIN32)
24 
25 #include <cstdio>
26 #include <windows.h>
27 
28  Timer::Timer()
29  {
30  LARGE_INTEGER l;
31  QueryPerformanceFrequency(&l);
32  invFreq = 1.0f / l.QuadPart;
33  lastInterval = 0.0f;
34  frameCount = 0;
35  lastFpsUpdate = 0.0f;
36  reset();
37  fps = 0.0f;
38  lastTime = 0.0f;
39  }
40 
41  void Timer::reset()
42  {
43  LARGE_INTEGER l;
44  QueryPerformanceCounter(&l);
45  resetStamp = (((double)l.QuadPart) * invFreq);
46  }
47 
48  float Timer::getTime()
49  {
50  LARGE_INTEGER l;
51  QueryPerformanceCounter(&l);
52  return (float)(((double)l.QuadPart) * invFreq - resetStamp);
53  }
54 
55  float Timer::getInterval()
56  {
57  float time = getTime();
58  float interval = time - lastInterval;
59  lastInterval = time;
60  return interval;
61  }
62 
63  float Timer::getFPS()
64  {
65  float time = getTime();
66  frameCount++;
67  if (time-lastFpsUpdate > 1.0f)
68  {
69  fps = (float)frameCount / (time-lastFpsUpdate);
70  lastFpsUpdate = time;
71  frameCount = 0;
72  }
73  return fps;
74  }
75 }
76 #else
77 
78 #include <sys/time.h>
79 
80 namespace MaliSDK
81 {
82  Timer::Timer()
83  : startTime()
84  , currentTime()
85  , lastIntervalTime(0.0f)
86  , frameCount(0)
87  , fpsTime(0.0f)
88  , fps(0.0f)
89  {
90  startTime.tv_sec = 0;
91  startTime.tv_usec = 0;
92  currentTime.tv_sec = 0;
93  currentTime.tv_usec = 0;
94 
95  reset();
96  }
97 
98  void Timer::reset()
99  {
100  gettimeofday(&startTime, NULL);
101  lastIntervalTime = 0.0;
102 
103  frameCount = 0;
104  fpsTime = 0.0f;
105  }
106 
107  float Timer::getTime()
108  {
109  gettimeofday(&currentTime, NULL);
110  float seconds = (currentTime.tv_sec - startTime.tv_sec);
111  float milliseconds = (float(currentTime.tv_usec - startTime.tv_usec)) / 1000000.0f;
112  return seconds + milliseconds;
113  }
114 
115  float Timer::getInterval()
116  {
117  float time = getTime();
118  float interval = time - lastIntervalTime;
119  lastIntervalTime = time;
120  return interval;
121  }
122 
123  float Timer::getFPS()
124  {
125  if (getTime() - fpsTime > 1.0f)
126  {
127  fps = static_cast<float>(frameCount) / (getTime() - fpsTime);
128  frameCount = 0;
129  fpsTime = getTime();
130  }
131  ++frameCount;
132  return fps;
133  }
134 }
135 #endif
136 
137 namespace MaliSDK
138 {
139  bool Timer::isTimePassed(float seconds)
140  {
141  float time = getTime();
142  if (time - lastTime > seconds)
143  {
144  lastTime = time;
145  return true;
146  }
147  return false;
148  }
149 }
Timer()
Default Constructor.
Definition: Timer.cpp:84
float getTime()
Returns the time passed since object creation or since reset() was last called.
Definition: Timer.cpp:109
float getFPS()
Returns the FPS (Frames Per Second).
Definition: Timer.cpp:125
float fps
Definition: Timer.h:41
void reset()
Resets the timer to 0.0f.
Definition: Timer.cpp:100
timeval currentTime
Definition: Timer.h:50
float lastTime
Definition: Timer.h:42
GLfloat GLfloat f
Definition: gl2ext.h:2707
timeval startTime
Definition: Timer.h:49
float getInterval()
Returns the time passed since getInterval() was last called.
Definition: Timer.cpp:117
float lastIntervalTime
Definition: Timer.h:51
float fpsTime
Definition: Timer.h:52
precision highp float
Definition: hiz_cull.cs:37
int frameCount
Definition: Timer.h:40
bool isTimePassed(float seconds=1.0f)
Tests if 'seconds' seconds have passed since reset() or this method was called.
Definition: Timer.cpp:141
uniform float time
Definition: spawn.cs:50