SDL3_gfx 1.0.1 Graphics primitives and surface functions for SDL3

SDL3_framerate.c
Go to the documentation of this file.
1/*
2
3 SDL3_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 "SDL3_framerate.h"
31
37Uint64 _getTicks()
38{
39 Uint64 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
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
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
107 int SDL_getFramerate(FPSmanager * manager)
108{
109 if (manager == NULL) {
110 return (-1);
111 } else {
112 return ((int)manager->rate);
113 }
114}
115
126 int SDL_getFramecount(FPSmanager * manager)
127{
128 if (manager == NULL) {
129 return (-1);
130 } else {
131 return ((int)manager->framecount);
132 }
133}
134
146Uint64 SDL_framerateDelay(FPSmanager * manager)
147{
148 Uint64 current_ticks;
149 Uint64 target_ticks;
150 Uint64 the_delay;
151 Uint64 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 + (Uint64) ((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}
SDL_initFramerate
void SDL_initFramerate(FPSmanager *manager)
Initialize the framerate manager.
Definition SDL3_framerate.c:62
SDL_getFramerate
int SDL_getFramerate(FPSmanager *manager)
Return the current target framerate in Hz.
Definition SDL3_framerate.c:107
SDL_getFramecount
int SDL_getFramecount(FPSmanager *manager)
Return the current framecount.
Definition SDL3_framerate.c:126
SDL_framerateDelay
Uint64 SDL_framerateDelay(FPSmanager *manager)
Delay execution to maintain a constant framerate and calculate fps.
Definition SDL3_framerate.c:146
_getTicks
Uint64 _getTicks()
Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
Definition SDL3_framerate.c:37
SDL_setFramerate
int SDL_setFramerate(FPSmanager *manager, Uint32 rate)
Set the framerate in Hz.
Definition SDL3_framerate.c:86
SDL3_framerate.h
FPS_LOWER_LIMIT
#define FPS_LOWER_LIMIT
Lowest possible rate supported by framerate controller in Hz (1/s).
Definition SDL3_framerate.h:52
FPS_DEFAULT
#define FPS_DEFAULT
Default rate of framerate controller in Hz (1/s).
Definition SDL3_framerate.h:57
FPS_UPPER_LIMIT
#define FPS_UPPER_LIMIT
Highest possible rate supported by framerate controller in Hz (1/s).
Definition SDL3_framerate.h:47
FPSmanager
Structure holding the state and timing information of the framerate controller.
Definition SDL3_framerate.h:62
FPSmanager::framecount
Uint32 framecount
Definition SDL3_framerate.h:63
FPSmanager::baseticks
Uint64 baseticks
Definition SDL3_framerate.h:65
FPSmanager::rateticks
float rateticks
Definition SDL3_framerate.h:64
FPSmanager::lastticks
Uint64 lastticks
Definition SDL3_framerate.h:66
FPSmanager::rate
Uint32 rate
Definition SDL3_framerate.h:67