Yet another huge code merge representing the current status of my rewrite. It include...
[supertux.git] / src / timer.c
1 //
2 // C Implementation: timer
3 //
4 // Description: 
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <SDL/SDL.h>
14 #include "defines.h"
15 #include "timer.h"
16
17 unsigned int st_get_ticks(void)
18 {
19 if(st_pause_count != 0)
20 return SDL_GetTicks() - st_pause_ticks - SDL_GetTicks() + st_pause_count;
21 else
22 return SDL_GetTicks() - st_pause_ticks;
23 }
24
25 void st_pause_ticks_init(void)
26 {
27 st_pause_ticks = 0;
28 st_pause_count = 0;
29 }
30
31 void st_pause_ticks_start(void)
32 {
33 st_pause_count = SDL_GetTicks();
34 }
35
36 void st_pause_ticks_stop(void)
37 {
38 st_pause_ticks += SDL_GetTicks() - st_pause_count;
39 st_pause_count = 0;
40 }
41
42 void timer_init(timer_type* ptimer)
43 {
44   ptimer->period = 0;
45   ptimer->time = 0;
46 }
47
48 void timer_start(timer_type* ptimer, unsigned int period)
49 {
50   ptimer->time = st_get_ticks();
51   ptimer->period = period;
52 }
53
54 void timer_stop(timer_type* ptimer)
55 {
56  timer_init(ptimer);
57 }
58
59 int timer_check(timer_type* ptimer)
60 {
61   if((ptimer->time != 0) && (ptimer->time + ptimer->period > st_get_ticks()))
62     return YES;
63   else
64     {
65       ptimer->time = 0;
66       return NO;
67     }
68 }
69
70 int timer_started(timer_type* ptimer)
71 {
72   if(ptimer->time != 0)
73     return YES;
74   else
75     return NO;
76 }
77
78 int timer_get_left(timer_type* ptimer)
79 {
80   return (ptimer->period - (st_get_ticks() - ptimer->time));
81 }
82
83 int timer_get_gone(timer_type* ptimer)
84 {
85   return (st_get_ticks() - ptimer->time);
86 }