SDL2_gfx  1.0.2
Graphics primitives and surface functions for SDL2
/build/libsdl2-gfx-F3NxcE/libsdl2-gfx-1.0.4+dfsg/SDL2_framerate.c
Go to the documentation of this file.
1 /*
2 
3 SDL2_framerate.c: framerate manager
4 
5 Copyright (C) 2012-2014 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 "SDL2_framerate.h"
31 
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 
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 
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 
108 {
109  if (manager == NULL) {
110  return (-1);
111  } else {
112  return ((int)manager->rate);
113  }
114 }
115 
127 {
128  if (manager == NULL) {
129  return (-1);
130  } else {
131  return ((int)manager->framecount);
132  }
133 }
134 
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 }
Uint32 _getTicks()
Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
void SDL_initFramerate(FPSmanager *manager)
Initialize the framerate manager.
int SDL_getFramerate(FPSmanager *manager)
Return the current target framerate in Hz.
int SDL_getFramecount(FPSmanager *manager)
Return the current framecount.
int SDL_setFramerate(FPSmanager *manager, Uint32 rate)
Set the framerate in Hz.
Uint32 SDL_framerateDelay(FPSmanager *manager)
Delay execution to maintain a constant framerate and calculate fps.
#define FPS_LOWER_LIMIT
Lowest possible rate supported by framerate controller in Hz (1/s).
#define FPS_DEFAULT
Default rate of framerate controller in Hz (1/s).
#define FPS_UPPER_LIMIT
Highest possible rate supported by framerate controller in Hz (1/s).
Structure holding the state and timing information of the framerate controller.
Uint32 framecount
float rateticks
Uint32 lastticks
Uint32 baseticks