-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.c
73 lines (55 loc) · 1.52 KB
/
timer.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
65
66
67
68
69
70
71
72
73
#include "main.h"
// run timer and compute seconds without modifying stats
float timer_peek( px_timer_t* stats )
{
float seconds;
px_timer_t old = *stats;
seconds = timer_run( stats );
*stats = old; // restore it
return seconds;
}
// send debug info to console
void timer_debug( char* name, px_timer_t* stats )
{
p("%s: seconds = %5f, best = %5f, worst = %5f\n",name,stats->seconds,stats->best,stats->worst);
}
// clear the timer
void timer_clear( px_timer_t* stats )
{
stats->last = 0;
stats->worst = 0;
stats->best = 0;
stats->seconds = 0.0F;
}
// run timer and compute calculations
float timer_run( px_timer_t* stats )
{
// current counter value
Uint32 time_now;
// ms between last and now
Uint32 time_diff;
// if there is no count_before then we know this is the first run
if(!stats->last)
{
// set the the count_before
stats->last = SDL_GetTicks();
// your program should know it's a first run
return 0.0F;
}
// get the count_after
time_now = SDL_GetTicks();
// calculate ms since last run
time_diff = time_now - stats->last;
// calculate the duration in seconds
stats->seconds = ((float)time_diff) / 1000.0F; // 1000 ms == 1 s
// reset the last value
stats->last = time_now;
// calculate the best
if (stats->best == 0.0F || stats->seconds < stats->best)
stats->best = stats->seconds;
// callculate the worst
if (stats->worst == 0.0F || stats->seconds > stats->worst)
stats->worst = stats->seconds;
// nice feature
return stats->seconds;
}