|
1086
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 LOVE Development Team
|
|
|
3 *
|
|
|
4 * This software is provided 'as-is', without any express or implied
|
|
|
5 * warranty. In no event will the authors be held liable for any damages
|
|
|
6 * arising from the use of this software.
|
|
|
7 *
|
|
|
8 * Permission is granted to anyone to use this software for any purpose,
|
|
|
9 * including commercial applications, and to alter it and redistribute it
|
|
|
10 * freely, subject to the following restrictions:
|
|
|
11 *
|
|
|
12 * 1. The origin of this software must not be misrepresented; you must not
|
|
|
13 * claim that you wrote the original software. If you use this software
|
|
|
14 * in a product, an acknowledgment in the product documentation would be
|
|
|
15 * appreciated but is not required.
|
|
|
16 * 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
17 * misrepresented as being the original software.
|
|
|
18 * 3. This notice may not be removed or altered from any source distribution.
|
|
|
19 **/
|
|
|
20
|
|
|
21 #include "common/config.h"
|
|
|
22 #include "common/delay.h"
|
|
|
23
|
|
|
24 #ifdef LOVE_WINDOWS
|
|
|
25 # include <windows.h>
|
|
|
26 # include <time.h>
|
|
|
27 #else
|
|
|
28 # include <sys/time.h>
|
|
|
29 #endif
|
|
|
30
|
|
|
31 #include "Timer.h"
|
|
|
32
|
|
|
33 namespace love
|
|
|
34 {
|
|
|
35 namespace timer
|
|
|
36 {
|
|
|
37 namespace sdl
|
|
|
38 {
|
|
|
39
|
|
|
40 Timer::Timer()
|
|
|
41 : currTime(0)
|
|
|
42 , prevFpsUpdate(0)
|
|
|
43 , fps(0)
|
|
|
44 , fpsUpdateFrequency(1)
|
|
|
45 , frames(0)
|
|
|
46 , dt(0)
|
|
|
47 {
|
|
|
48 // Init the SDL timer system.
|
|
|
49 if (SDL_InitSubSystem(SDL_INIT_TIMER) < 0)
|
|
|
50 throw Exception(SDL_GetError());
|
|
|
51 }
|
|
|
52
|
|
|
53 Timer::~Timer()
|
|
|
54 {
|
|
|
55 // Quit SDL timer.
|
|
|
56 SDL_QuitSubSystem(SDL_INIT_TIMER);
|
|
|
57 }
|
|
|
58
|
|
|
59 const char *Timer::getName() const
|
|
|
60 {
|
|
|
61 return "love.timer.sdl";
|
|
|
62 }
|
|
|
63
|
|
|
64 void Timer::step()
|
|
|
65 {
|
|
|
66 // Frames rendered
|
|
|
67 frames++;
|
|
|
68
|
|
|
69 // "Current" time is previous time by now.
|
|
|
70 prevTime = currTime;
|
|
|
71
|
|
|
72 // Get ticks from SDL
|
|
|
73 currTime = SDL_GetTicks();
|
|
|
74
|
|
|
75 // Convert to number of seconds
|
|
|
76 dt = (currTime - prevTime)/1000.0;
|
|
|
77
|
|
|
78 double timeSinceLast = (currTime - prevFpsUpdate)/1000.0;
|
|
|
79 // Update FPS?
|
|
|
80 if (timeSinceLast > fpsUpdateFrequency)
|
|
|
81 {
|
|
|
82 fps = int((frames/timeSinceLast) + 0.5);
|
|
|
83 prevFpsUpdate = currTime;
|
|
|
84 frames = 0;
|
|
|
85 }
|
|
|
86 }
|
|
|
87
|
|
|
88 void Timer::sleep(double seconds)
|
|
|
89 {
|
|
|
90 if (seconds > 0)
|
|
|
91 delay((int)(seconds*1000));
|
|
|
92 }
|
|
|
93
|
|
|
94 double Timer::getDelta() const
|
|
|
95 {
|
|
|
96 return dt;
|
|
|
97 }
|
|
|
98
|
|
|
99 int Timer::getFPS() const
|
|
|
100 {
|
|
|
101 return fps;
|
|
|
102 }
|
|
|
103
|
|
|
104 double Timer::getTime() const
|
|
|
105 {
|
|
|
106 return SDL_GetTicks()/1000.0;
|
|
|
107 }
|
|
|
108
|
|
|
109 double Timer::getMicroTime() const
|
|
|
110 {
|
|
|
111 #ifdef LOVE_WINDOWS
|
|
|
112 static __int64 freq = 0;
|
|
|
113
|
|
|
114 if (!freq)
|
|
|
115 {
|
|
|
116 LARGE_INTEGER temp;
|
|
|
117 QueryPerformanceFrequency(&temp);
|
|
|
118
|
|
|
119 freq = (__int64) temp.QuadPart;
|
|
|
120 }
|
|
|
121
|
|
|
122 LARGE_INTEGER microTime;
|
|
|
123 QueryPerformanceCounter(µTime);
|
|
|
124
|
|
|
125 // The 64 to 32 bit integer conversion, assuming the fraction part down
|
|
|
126 // to microseconds takes 20 bits, should not be a problem unless the
|
|
|
127 // system has an uptime of a few decades.
|
|
|
128 return (double) microTime.QuadPart / (double) freq;
|
|
|
129 #else
|
|
|
130 timeval t;
|
|
|
131 gettimeofday(&t, NULL);
|
|
|
132 return t.tv_sec + t.tv_usec/1000000.0;
|
|
|
133 #endif
|
|
|
134 }
|
|
|
135
|
|
|
136 } // sdl
|
|
|
137 } // timer
|
|
|
138 } // love
|