forked from coolsee/OpenMugen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_framerate.cpp
84 lines (68 loc) · 1.48 KB
/
SDL_framerate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
SDL_framerate: framerate manager
LGPL (c) A. Schiffler
*/
#include "SDL_framerate.h"
/*
Initialize the framerate manager
*/
void SDL_initFramerate(FPSmanager * manager)
{
/*
* Store some sane values
*/
manager->framecount = 0;
manager->rate = FPS_DEFAULT;
manager->rateticks = (1000.0 / (float) FPS_DEFAULT);
manager->lastticks = SDL_GetTicks();
}
/*
Set the framerate in Hz
*/
int SDL_setFramerate(FPSmanager * manager, int rate)
{
if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
manager->framecount = 0;
manager->rate = rate;
manager->rateticks = (1000.0 / (float) rate);
return (0);
} else {
return (-1);
}
}
/*
Return the current target framerate in Hz
*/
int SDL_getFramerate(FPSmanager * manager)
{
if (manager == NULL) {
return (-1);
} else {
return (manager->rate);
}
}
/*
Delay execution to maintain a constant framerate. Calculate fps.
*/
void SDL_framerateDelay(FPSmanager * manager)
{
Uint32 current_ticks;
Uint32 target_ticks;
Uint32 the_delay;
/*
* Next frame
*/
manager->framecount++;
/*
* Get/calc ticks
*/
current_ticks = SDL_GetTicks();
target_ticks = manager->lastticks + (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->lastticks = SDL_GetTicks();
}
}