3
SDL_framerate.c: framerate manager
5
Copyright (C) 2001-2012 Andreas Schiffler
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.
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:
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.
20
2. Altered source versions must be plainly marked as such, and must not be
21
misrepresented as being the original software.
23
3. This notice may not be removed or altered from any source
26
Andreas Schiffler -- aschiffler at ferzkopp dot net
28
Changed for ManaPlus (C) 2013 ManaPlus developers
31
#include "SDL2_framerate.h"
34
\brief Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
36
\return The tick count.
40
const uint32_t ticks = SDL_GetTicks();
43
* Since baseticks!=0 is used to track initialization
44
* we need to ensure that the tick count is always >0
45
* since SDL_GetTicks may not have incremented yet and
46
* return 0 depending on the timing of the calls.
55
\brief Initialize the framerate manager.
57
Initialize the framerate manager, set default framerate of 30Hz and
58
reset delay interpolation.
60
\param manager Pointer to the framerate manager.
62
void SDL_initFramerate(FPSmanager *const manager)
65
* Store some sane values
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;
76
\brief Set the framerate in Hz
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.
81
\param manager Pointer to the framerate manager.
82
\param rate The new framerate in Hz (frames per second).
84
\return 0 for sucess and -1 for error.
86
int SDL_setFramerate(FPSmanager *const manager, const uint32_t rate)
88
if (rate >= FPS_LOWER_LIMIT && rate <= FPS_UPPER_LIMIT)
90
manager->framecount = 0;
92
manager->rateticks = (1000.0f / (float) rate);
102
\brief Return the current target framerate in Hz
104
Get the currently set framerate of the manager.
106
\param manager Pointer to the framerate manager.
108
\return Current framerate in Hz or -1 for error.
110
int SDL_getFramerate(FPSmanager *const manager)
115
return (int)manager->rate;
119
\brief Return the current framecount.
121
Get the current framecount from the framerate manager.
122
A frame is counted each time SDL_framerateDelay is called.
124
\param manager Pointer to the framerate manager.
126
\return Current frame count or -1 for error.
128
int SDL_getFramecount(FPSmanager *const manager)
133
return (int)manager->framecount;
137
\brief Delay execution to maintain a constant framerate and calculate fps.
139
Generate a delay to accomodate currently set framerate. Call once in the
140
graphics/rendering loop. If the computer cannot keep up with the rate (i.e.
141
drawing too slow), the delay is zero and the delay interpolation is reset.
143
\param manager Pointer to the framerate manager.
145
\return The time that passed since the last call to the function in ms. May return 0.
147
uint32_t SDL_framerateDelay(FPSmanager *const manager)
152
* No manager, no delay
158
* Initialize uninitialized manager
160
if (manager->baseticks == 0)
161
SDL_initFramerate(manager);
166
manager->framecount ++;
171
const uint32_t current_ticks = _getTicks();
172
const uint32_t time_passed = current_ticks - manager->lastticks;
173
manager->lastticks = current_ticks;
174
const uint32_t target_ticks = manager->baseticks + (uint32_t)(
175
(float)(manager->framecount) * manager->rateticks);
177
if (current_ticks <= target_ticks)
179
the_delay = target_ticks - current_ticks;
180
SDL_Delay(the_delay);
184
manager->framecount = 0;
185
manager->baseticks = _getTicks();