Mercurial > love-experiments
annotate src/modules/timer/sdl/Timer.cpp @ 1415:db0719826fa5
Fixed love.timer.getFPS and love.timer.getAverageDelta during the first second of runtime
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Sat, 30 Mar 2013 21:56:57 -0300 |
| parents | 0907c1a57ce6 |
| children | 357697c2e2a8 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1096
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 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() | |
|
1415
db0719826fa5
Fixed love.timer.getFPS and love.timer.getAverageDelta during the first second of runtime
Alex Szpakowski <slime73@gmail.com>
parents:
1355
diff
changeset
|
41 : currTime(getMicroTime()) |
|
db0719826fa5
Fixed love.timer.getFPS and love.timer.getAverageDelta during the first second of runtime
Alex Szpakowski <slime73@gmail.com>
parents:
1355
diff
changeset
|
42 , prevFpsUpdate(currTime) |
| 1096 | 43 , fps(0) |
|
1354
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
44 , averageDelta(0) |
| 1096 | 45 , fpsUpdateFrequency(1) |
| 46 , frames(0) | |
| 47 , dt(0) | |
| 48 { | |
| 49 // Init the SDL timer system. | |
| 50 if (SDL_InitSubSystem(SDL_INIT_TIMER) < 0) | |
| 51 throw Exception(SDL_GetError()); | |
| 52 } | |
| 53 | |
| 54 Timer::~Timer() | |
| 55 { | |
| 56 // Quit SDL timer. | |
| 57 SDL_QuitSubSystem(SDL_INIT_TIMER); | |
| 58 } | |
| 59 | |
| 60 const char *Timer::getName() const | |
| 61 { | |
| 62 return "love.timer.sdl"; | |
| 63 } | |
| 64 | |
| 65 void Timer::step() | |
| 66 { | |
| 67 // Frames rendered | |
| 68 frames++; | |
| 69 | |
| 70 // "Current" time is previous time by now. | |
| 71 prevTime = currTime; | |
| 72 | |
| 73 // Get ticks from SDL | |
|
1355
0907c1a57ce6
Make love.timer.getFPS and love.timer.getAverageDelta microsecond-accurate
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1354
diff
changeset
|
74 currTime = getMicroTime(); |
| 1096 | 75 |
| 76 // Convert to number of seconds | |
|
1355
0907c1a57ce6
Make love.timer.getFPS and love.timer.getAverageDelta microsecond-accurate
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1354
diff
changeset
|
77 dt = currTime - prevTime; |
| 1096 | 78 |
|
1355
0907c1a57ce6
Make love.timer.getFPS and love.timer.getAverageDelta microsecond-accurate
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1354
diff
changeset
|
79 double timeSinceLast = currTime - prevFpsUpdate; |
| 1096 | 80 // Update FPS? |
| 81 if (timeSinceLast > fpsUpdateFrequency) | |
| 82 { | |
| 83 fps = int((frames/timeSinceLast) + 0.5); | |
|
1354
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
84 averageDelta = timeSinceLast/frames; |
| 1096 | 85 prevFpsUpdate = currTime; |
| 86 frames = 0; | |
| 87 } | |
| 88 } | |
| 89 | |
|
1354
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
90 void Timer::sleep(double seconds) const |
| 1096 | 91 { |
| 92 if (seconds > 0) | |
| 93 delay((int)(seconds*1000)); | |
| 94 } | |
| 95 | |
| 96 double Timer::getDelta() const | |
| 97 { | |
| 98 return dt; | |
| 99 } | |
| 100 | |
| 101 int Timer::getFPS() const | |
| 102 { | |
| 103 return fps; | |
| 104 } | |
| 105 | |
|
1354
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
106 double Timer::getAverageDelta() const |
|
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
107 { |
|
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
108 return averageDelta; |
|
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
109 } |
|
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
110 |
| 1096 | 111 double Timer::getTime() const |
| 112 { | |
| 113 return SDL_GetTicks()/1000.0; | |
| 114 } | |
| 115 | |
| 116 double Timer::getMicroTime() const | |
| 117 { | |
| 118 #ifdef LOVE_WINDOWS | |
| 119 static __int64 freq = 0; | |
| 120 | |
| 121 if (!freq) | |
| 122 { | |
| 123 LARGE_INTEGER temp; | |
| 124 QueryPerformanceFrequency(&temp); | |
| 125 | |
| 126 freq = (__int64) temp.QuadPart; | |
| 127 } | |
| 128 | |
| 129 LARGE_INTEGER microTime; | |
| 130 QueryPerformanceCounter(µTime); | |
| 131 | |
| 132 // The 64 to 32 bit integer conversion, assuming the fraction part down | |
| 133 // to microseconds takes 20 bits, should not be a problem unless the | |
| 134 // system has an uptime of a few decades. | |
| 135 return (double) microTime.QuadPart / (double) freq; | |
| 136 #else | |
| 137 timeval t; | |
| 138 gettimeofday(&t, NULL); | |
| 139 return t.tv_sec + t.tv_usec/1000000.0; | |
| 140 #endif | |
| 141 } | |
| 142 | |
| 143 } // sdl | |
| 144 } // timer | |
| 145 } // love |
