1 /*
2 
3 SDL_framerate.c: framerate manager
4 
5 Copyright (C) 2001-2012  Andreas Schiffler
6 
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 
20 2. Altered source versions must be plainly marked as such, and must not be
21 misrepresented as being the original software.
22 
23 3. This notice may not be removed or altered from any source
24 distribution.
25 
26 Andreas Schiffler -- aschiffler at ferzkopp dot net
27 
28 */
29 
30 #include "SDL_framerate.h"
31 
32 /*!
33 \brief Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
34 
35 \return The tick count.
36 */
_getTicks()37 Uint32 _getTicks()
38 {
39 	Uint32 ticks = SDL_GetTicks();
40 
41 	/*
42 	* Since baseticks!=0 is used to track initialization
43 	* we need to ensure that the tick count is always >0
44 	* since SDL_GetTicks may not have incremented yet and
45 	* return 0 depending on the timing of the calls.
46 	*/
47 	if (ticks == 0) {
48 		return 1;
49 	} else {
50 		return ticks;
51 	}
52 }
53 
54 /*!
55 \brief Initialize the framerate manager.
56 
57 Initialize the framerate manager, set default framerate of 30Hz and
58 reset delay interpolation.
59 
60 \param manager Pointer to the framerate manager.
61 */
SDL_initFramerate(FPSmanager * manager)62 void SDL_initFramerate(FPSmanager * manager)
63 {
64 	/*
65 	* Store some sane values
66 	*/
67 	manager->framecount = 0;
68 	manager->rate = FPS_DEFAULT;
69 	manager->rateticks = (1000.0f / (float) FPS_DEFAULT);
70 	manager->baseticks = _getTicks();
71 	manager->lastticks = manager->baseticks;
72 
73 }
74 
75 /*!
76 \brief Set the framerate in Hz
77 
78 Sets a new framerate for the manager and reset delay interpolation.
79 Rate values must be between FPS_LOWER_LIMIT and FPS_UPPER_LIMIT inclusive to be accepted.
80 
81 \param manager Pointer to the framerate manager.
82 \param rate The new framerate in Hz (frames per second).
83 
84 \return 0 for sucess and -1 for error.
85 */
SDL_setFramerate(FPSmanager * manager,Uint32 rate)86 int SDL_setFramerate(FPSmanager * manager, Uint32 rate)
87 {
88 	if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
89 		manager->framecount = 0;
90 		manager->rate = rate;
91 		manager->rateticks = (1000.0f / (float) rate);
92 		return (0);
93 	} else {
94 		return (-1);
95 	}
96 }
97 
98 /*!
99 \brief Return the current target framerate in Hz
100 
101 Get the currently set framerate of the manager.
102 
103 \param manager Pointer to the framerate manager.
104 
105 \return Current framerate in Hz or -1 for error.
106 */
SDL_getFramerate(FPSmanager * manager)107 int SDL_getFramerate(FPSmanager * manager)
108 {
109 	if (manager == NULL) {
110 		return (-1);
111 	} else {
112 		return ((int)manager->rate);
113 	}
114 }
115 
116 /*!
117 \brief Return the current framecount.
118 
119 Get the current framecount from the framerate manager.
120 A frame is counted each time SDL_framerateDelay is called.
121 
122 \param manager Pointer to the framerate manager.
123 
124 \return Current frame count or -1 for error.
125 */
SDL_getFramecount(FPSmanager * manager)126 int SDL_getFramecount(FPSmanager * manager)
127 {
128 	if (manager == NULL) {
129 		return (-1);
130 	} else {
131 		return ((int)manager->framecount);
132 	}
133 }
134 
135 /*!
136 \brief Delay execution to maintain a constant framerate and calculate fps.
137 
138 Generate a delay to accomodate currently set framerate. Call once in the
139 graphics/rendering loop. If the computer cannot keep up with the rate (i.e.
140 drawing too slow), the delay is zero and the delay interpolation is reset.
141 
142 \param manager Pointer to the framerate manager.
143 
144 \return The time that passed since the last call to the function in ms. May return 0.
145 */
SDL_framerateDelay(FPSmanager * manager)146 Uint32 SDL_framerateDelay(FPSmanager * manager)
147 {
148 	Uint32 current_ticks;
149 	Uint32 target_ticks;
150 	Uint32 the_delay;
151 	Uint32 time_passed = 0;
152 
153 	/*
154 	* No manager, no delay
155 	*/
156 	if (manager == NULL) {
157 		return 0;
158 	}
159 
160 	/*
161 	* Initialize uninitialized manager
162 	*/
163 	if (manager->baseticks == 0) {
164 		SDL_initFramerate(manager);
165 	}
166 
167 	/*
168 	* Next frame
169 	*/
170 	manager->framecount++;
171 
172 	/*
173 	* Get/calc ticks
174 	*/
175 	current_ticks = _getTicks();
176 	time_passed = current_ticks - manager->lastticks;
177 	manager->lastticks = current_ticks;
178 	target_ticks = manager->baseticks + (Uint32) ((float) manager->framecount * manager->rateticks);
179 
180 	if (current_ticks <= target_ticks) {
181 		the_delay = target_ticks - current_ticks;
182 		SDL_Delay(the_delay);
183 	} else {
184 		manager->framecount = 0;
185 		manager->baseticks = _getTicks();
186 	}
187 
188 	return time_passed;
189 }
190