Mercurial > love-experiments
annotate src/modules/timer/sdl/Timer.cpp @ 1448:108ee2e81f65 default tip
Fix a case typo, imageData.h -> ImageData.h
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 18 Jan 2014 13:57:44 +0100 |
| parents | 357697c2e2a8 |
| children |
| 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() | |
|
1418
357697c2e2a8
More internal code cleanup
Alex Szpakowski <slime73@gmail.com>
parents:
1415
diff
changeset
|
41 : currTime(0) |
|
357697c2e2a8
More internal code cleanup
Alex Szpakowski <slime73@gmail.com>
parents:
1415
diff
changeset
|
42 , prevFpsUpdate(0) |
| 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()); | |
|
1418
357697c2e2a8
More internal code cleanup
Alex Szpakowski <slime73@gmail.com>
parents:
1415
diff
changeset
|
52 |
|
357697c2e2a8
More internal code cleanup
Alex Szpakowski <slime73@gmail.com>
parents:
1415
diff
changeset
|
53 prevFpsUpdate = currTime = getMicroTime(); |
| 1096 | 54 } |
| 55 | |
| 56 Timer::~Timer() | |
| 57 { | |
| 58 // Quit SDL timer. | |
| 59 SDL_QuitSubSystem(SDL_INIT_TIMER); | |
| 60 } | |
| 61 | |
| 62 const char *Timer::getName() const | |
| 63 { | |
| 64 return "love.timer.sdl"; | |
| 65 } | |
| 66 | |
| 67 void Timer::step() | |
| 68 { | |
| 69 // Frames rendered | |
| 70 frames++; | |
| 71 | |
| 72 // "Current" time is previous time by now. | |
| 73 prevTime = currTime; | |
| 74 | |
| 75 // 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
|
76 currTime = getMicroTime(); |
| 1096 | 77 |
| 78 // 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
|
79 dt = currTime - prevTime; |
| 1096 | 80 |
|
1355
0907c1a57ce6
Make love.timer.getFPS and love.timer.getAverageDelta microsecond-accurate
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1354
diff
changeset
|
81 double timeSinceLast = currTime - prevFpsUpdate; |
| 1096 | 82 // Update FPS? |
| 83 if (timeSinceLast > fpsUpdateFrequency) | |
| 84 { | |
| 85 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
|
86 averageDelta = timeSinceLast/frames; |
| 1096 | 87 prevFpsUpdate = currTime; |
| 88 frames = 0; | |
| 89 } | |
| 90 } | |
| 91 | |
|
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
|
92 void Timer::sleep(double seconds) const |
| 1096 | 93 { |
| 94 if (seconds > 0) | |
| 95 delay((int)(seconds*1000)); | |
| 96 } | |
| 97 | |
| 98 double Timer::getDelta() const | |
| 99 { | |
| 100 return dt; | |
| 101 } | |
| 102 | |
| 103 int Timer::getFPS() const | |
| 104 { | |
| 105 return fps; | |
| 106 } | |
| 107 | |
|
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
|
108 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
|
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 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
|
111 } |
|
0cef18e9d722
Add love.timer.getAverageDelta (issue #570) and constify love.timer.sleep
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1282
diff
changeset
|
112 |
| 1096 | 113 double Timer::getTime() const |
| 114 { | |
| 115 return SDL_GetTicks()/1000.0; | |
| 116 } | |
| 117 | |
| 118 double Timer::getMicroTime() const | |
| 119 { | |
| 120 #ifdef LOVE_WINDOWS | |
| 121 static __int64 freq = 0; | |
| 122 | |
| 123 if (!freq) | |
| 124 { | |
| 125 LARGE_INTEGER temp; | |
| 126 QueryPerformanceFrequency(&temp); | |
| 127 | |
| 128 freq = (__int64) temp.QuadPart; | |
| 129 } | |
| 130 | |
| 131 LARGE_INTEGER microTime; | |
| 132 QueryPerformanceCounter(µTime); | |
| 133 | |
| 134 // The 64 to 32 bit integer conversion, assuming the fraction part down | |
| 135 // to microseconds takes 20 bits, should not be a problem unless the | |
| 136 // system has an uptime of a few decades. | |
| 137 return (double) microTime.QuadPart / (double) freq; | |
| 138 #else | |
| 139 timeval t; | |
| 140 gettimeofday(&t, NULL); | |
| 141 return t.tv_sec + t.tv_usec/1000000.0; | |
| 142 #endif | |
| 143 } | |
| 144 | |
| 145 } // sdl | |
| 146 } // timer | |
| 147 } // love |
