-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.c
64 lines (48 loc) · 1.33 KB
/
fps.c
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
/*****************************************************/
/* Ultimate Blocks */
/* Copyright (c) An Ly 2000, Owen Rudge 2001, 2008 */
/*****************************************************/
/* Module for measuring FPS for Allegro games - implementation.
Written by Miran Amon.
*/
#include "fps.h"
FPS *create_fps(int fps) {
int i;
FPS *ret = malloc(sizeof(FPS));
ret->nSamples = fps;
ret->samples = malloc(fps*sizeof(int));
for (i=0; i<fps; i++) {
ret->samples[i] = 1;
}
ret->curSample = 0;
ret->frameCounter = 0;
ret->framesPerSecond = fps;
return ret;
}
void destroy_fps(FPS *fps) {
if (fps == NULL)
return;
free(fps->samples);
fps->samples = NULL;
fps->nSamples = 0;
fps->frameCounter = 0;
free(fps);
fps = NULL;
}
void fps_tick(FPS *fps) {
fps->curSample++;
fps->curSample %= fps->nSamples;
fps->framesPerSecond -= fps->samples[fps->curSample];
fps->framesPerSecond += fps->frameCounter;
fps->samples[fps->curSample] = fps->frameCounter;
fps->frameCounter = 0;
}
void fps_frame(FPS *fps) {
++(fps->frameCounter);
}
int get_fps(FPS *fps) {
return fps->framesPerSecond;
}
void draw_fps(FPS *fps, BITMAP *bmp, FONT *font, int x, int y, int fg, char *format) {
textprintf(bmp, font, x, y, fg, format, get_fps(fps));
}