ManaPlus
Functions
SDL2_framerate.cpp File Reference

(986a3bf)

#include "sdl2gfx/SDL2_framerate.h"
#include "debug.h"

Go to the source code of this file.

Functions

static Uint32 _getTicks ()
 Internal wrapper to SDL_GetTicks that ensures a non-zero return value. More...
 
void SDL_initFramerate (FPSmanager *manager)
 Initialize the framerate manager. More...
 
int SDL_setFramerate (FPSmanager *manager, Uint32 rate)
 Set the framerate in Hz. More...
 
int SDL_getFramerate (FPSmanager *manager)
 Return the current target framerate in Hz. More...
 
int SDL_getFramecount (FPSmanager *manager)
 Return the current framecount. More...
 
Uint32 SDL_framerateDelay (FPSmanager *manager)
 Delay execution to maintain a constant framerate and calculate fps. More...
 

Function Documentation

◆ _getTicks()

static Uint32 _getTicks ( )
static

Internal wrapper to SDL_GetTicks that ensures a non-zero return value.

Returns
The tick count.

Definition at line 41 of file SDL2_framerate.cpp.

42 {
43  Uint32 ticks = SDL_GetTicks();
44 
45  /*
46  * Since baseticks!=0 is used to track initialization
47  * we need to ensure that the tick count is always >0
48  * since SDL_GetTicks may not have incremented yet and
49  * return 0 depending on the timing of the calls.
50  */
51  if (ticks == 0)
52  {
53  return 1;
54  }
55  else
56  {
57  return ticks;
58  }
59 }

Referenced by SDL_framerateDelay(), and SDL_initFramerate().

◆ SDL_framerateDelay()

Uint32 SDL_framerateDelay ( FPSmanager manager)

Delay execution to maintain a constant framerate and calculate fps.

Generate a delay to accomodate currently set framerate. Call once in the graphics/rendering loop. If the computer cannot keep up with the rate (i.e. drawing too slow), the delay is zero and the delay interpolation is reset.

Parameters
managerPointer to the framerate manager.
Returns
The time that passed since the last call to the function in ms. May return 0.

Definition at line 162 of file SDL2_framerate.cpp.

163 {
164  Uint32 current_ticks;
165  Uint32 target_ticks;
166  Uint32 the_delay;
167  Uint32 time_passed = 0;
168 
169  /*
170  * No manager, no delay
171  */
172  if (manager == nullptr)
173  {
174  return 0;
175  }
176 
177  /*
178  * Initialize uninitialized manager
179  */
180  if (manager->baseticks == 0)
181  {
182  SDL_initFramerate(manager);
183  }
184 
185  /*
186  * Next frame
187  */
188  manager->framecount++;
189 
190  /*
191  * Get/calc ticks
192  */
193  current_ticks = _getTicks();
194  time_passed = current_ticks - manager->lastticks;
195  manager->lastticks = current_ticks;
196  target_ticks = manager->baseticks + static_cast<Uint32>(static_cast<float>(
197  manager->framecount) * manager->rateticks);
198 
199  if (current_ticks <= target_ticks)
200  {
201  the_delay = target_ticks - current_ticks;
202  SDL_Delay(the_delay);
203  }
204  else
205  {
206  manager->framecount = 0;
207  manager->baseticks = _getTicks();
208  }
209 
210  return time_passed;
211 }
static Uint32 _getTicks()
Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
void SDL_initFramerate(FPSmanager *manager)
Initialize the framerate manager.
Uint32 baseticks
Uint32 framecount
float rateticks
Uint32 lastticks

References _getTicks(), FPSmanager::baseticks, FPSmanager::framecount, FPSmanager::lastticks, FPSmanager::rateticks, and SDL_initFramerate().

Referenced by Client::gameExec().

◆ SDL_getFramecount()

int SDL_getFramecount ( FPSmanager manager)

Return the current framecount.

Get the current framecount from the framerate manager. A frame is counted each time SDL_framerateDelay is called.

Parameters
managerPointer to the framerate manager.
Returns
Current frame count or -1 for error.

Definition at line 139 of file SDL2_framerate.cpp.

140 {
141  if (manager == nullptr)
142  {
143  return (-1);
144  }
145  else
146  {
147  return (static_cast<int>(manager->framecount));
148  }
149 }

References FPSmanager::framecount.

◆ SDL_getFramerate()

int SDL_getFramerate ( FPSmanager manager)

Return the current target framerate in Hz.

Get the currently set framerate of the manager.

Parameters
managerPointer to the framerate manager.
Returns
Current framerate in Hz or -1 for error.

Definition at line 117 of file SDL2_framerate.cpp.

118 {
119  if (manager == nullptr)
120  {
121  return (-1);
122  }
123  else
124  {
125  return (static_cast<int>(manager->rate));
126  }
127 }

References FPSmanager::rate.

Referenced by WindowManager::getFramerate().

◆ SDL_initFramerate()

void SDL_initFramerate ( FPSmanager manager)

Initialize the framerate manager.

Initialize the framerate manager, set default framerate of 30Hz and reset delay interpolation.

Parameters
managerPointer to the framerate manager.

Definition at line 69 of file SDL2_framerate.cpp.

70 {
71  /*
72  * Store some sane values
73  */
74  manager->framecount = 0;
75  manager->rate = FPS_DEFAULT;
76  manager->rateticks = (1000.0F / static_cast<float>(FPS_DEFAULT));
77  manager->baseticks = _getTicks();
78  manager->lastticks = manager->baseticks;
79 
80 }
#define FPS_DEFAULT
Default rate of framerate controller in Hz (1/s).

References _getTicks(), FPSmanager::baseticks, FPS_DEFAULT, FPSmanager::framecount, FPSmanager::lastticks, FPSmanager::rate, and FPSmanager::rateticks.

Referenced by Client::gameInit(), and SDL_framerateDelay().

◆ SDL_setFramerate()

int SDL_setFramerate ( FPSmanager manager,
Uint32  rate 
)

Set the framerate in Hz.

Sets a new framerate for the manager and reset delay interpolation. Rate values must be between FPS_LOWER_LIMIT and FPS_UPPER_LIMIT inclusive to be accepted.

Parameters
managerPointer to the framerate manager.
rateThe new framerate in Hz (frames per second).
Returns
0 for sucess and -1 for error.

Definition at line 93 of file SDL2_framerate.cpp.

94 {
95  if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT))
96  {
97  manager->framecount = 0;
98  manager->rate = rate;
99  manager->rateticks = (1000.0F / static_cast<float>(rate));
100  return (0);
101  }
102  else
103  {
104  return (-1);
105  }
106 }
#define FPS_LOWER_LIMIT
Lowest possible rate supported by framerate controller in Hz (1/s).
#define FPS_UPPER_LIMIT
Highest possible rate supported by framerate controller in Hz (1/s).

References FPS_LOWER_LIMIT, FPS_UPPER_LIMIT, FPSmanager::framecount, FPSmanager::rate, and FPSmanager::rateticks.

Referenced by WindowManager::setFramerate().