SDL2_framerate.c
Go to the documentation of this file.
/*
SDL2_framerate.c: framerate manager
Copyright (C) 2012-2014 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/
#include "SDL2_framerate.h"
Uint32 _getTicks()
{
Uint32 ticks = SDL_GetTicks();
/*
* Since baseticks!=0 is used to track initialization
* we need to ensure that the tick count is always >0
* since SDL_GetTicks may not have incremented yet and
* return 0 depending on the timing of the calls.
*/
if (ticks == 0) {
return 1;
} else {
return ticks;
}
}
void SDL_initFramerate(FPSmanager * manager)
{
/*
* Store some sane values
*/
manager->framecount = 0;
manager->rate = FPS_DEFAULT;
manager->rateticks = (1000.0f / (float) FPS_DEFAULT);
manager->baseticks = _getTicks();
manager->lastticks = manager->baseticks;
}
int SDL_setFramerate(FPSmanager * manager, Uint32 rate)
{
if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
manager->framecount = 0;
manager->rate = rate;
manager->rateticks = (1000.0f / (float) rate);
return (0);
} else {
return (-1);
}
}
int SDL_getFramerate(FPSmanager * manager)
{
if (manager == NULL) {
return (-1);
} else {
return ((int)manager->rate);
}
}
int SDL_getFramecount(FPSmanager * manager)
{
if (manager == NULL) {
return (-1);
} else {
return ((int)manager->framecount);
}
}
Uint32 SDL_framerateDelay(FPSmanager * manager)
{
Uint32 current_ticks;
Uint32 target_ticks;
Uint32 the_delay;
Uint32 time_passed = 0;
/*
* No manager, no delay
*/
if (manager == NULL) {
return 0;
}
/*
* Initialize uninitialized manager
*/
if (manager->baseticks == 0) {
SDL_initFramerate(manager);
}
/*
* Next frame
*/
manager->framecount++;
/*
* Get/calc ticks
*/
current_ticks = _getTicks();
time_passed = current_ticks - manager->lastticks;
manager->lastticks = current_ticks;
target_ticks = manager->baseticks + (Uint32) ((float) manager->framecount * manager->rateticks);
if (current_ticks <= target_ticks) {
the_delay = target_ticks - current_ticks;
SDL_Delay(the_delay);
} else {
manager->framecount = 0;
manager->baseticks = _getTicks();
}
return time_passed;
}
_getTicks
Uint32 _getTicks()
Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
Definition SDL2_framerate.c:37
SDL_initFramerate
void SDL_initFramerate(FPSmanager *manager)
Initialize the framerate manager.
Definition SDL2_framerate.c:62
SDL_getFramerate
int SDL_getFramerate(FPSmanager *manager)
Return the current target framerate in Hz.
Definition SDL2_framerate.c:107
SDL_getFramecount
int SDL_getFramecount(FPSmanager *manager)
Return the current framecount.
Definition SDL2_framerate.c:126
SDL_setFramerate
int SDL_setFramerate(FPSmanager *manager, Uint32 rate)
Set the framerate in Hz.
Definition SDL2_framerate.c:86
SDL_framerateDelay
Uint32 SDL_framerateDelay(FPSmanager *manager)
Delay execution to maintain a constant framerate and calculate fps.
Definition SDL2_framerate.c:146
SDL2_framerate.h
FPS_LOWER_LIMIT
#define FPS_LOWER_LIMIT
Lowest possible rate supported by framerate controller in Hz (1/s).
Definition SDL2_framerate.h:52
FPS_DEFAULT
#define FPS_DEFAULT
Default rate of framerate controller in Hz (1/s).
Definition SDL2_framerate.h:57
FPS_UPPER_LIMIT
#define FPS_UPPER_LIMIT
Highest possible rate supported by framerate controller in Hz (1/s).
Definition SDL2_framerate.h:47
FPSmanager
Structure holding the state and timing information of the framerate controller.
Definition SDL2_framerate.h:62
FPSmanager::framecount
Uint32 framecount
Definition SDL2_framerate.h:63
FPSmanager::rateticks
float rateticks
Definition SDL2_framerate.h:64
FPSmanager::lastticks
Uint32 lastticks
Definition SDL2_framerate.h:66
FPSmanager::baseticks
Uint32 baseticks
Definition SDL2_framerate.h:65
FPSmanager::rate
Uint32 rate
Definition SDL2_framerate.h:67