1 #include "sdl2/timer.hpp"
2 
3 /***************************************************************************
4     SDL Based Timer.
5 
6     Will need to be replaced if SDL library is replaced.
7 
8     Copyright Chris White.
9     See license.txt for more details.
10 ***************************************************************************/
11 
Timer()12 Timer::Timer()
13 {
14     //Initialize the variables
15     startTicks = 0;
16     pausedTicks = 0;
17     paused = false;
18     started = false;
19 }
20 
start()21 void Timer::start()
22 {
23     //Start the timer
24     started = true;
25 
26     //Unpause the timer
27     paused = false;
28 
29     //Get the current clock time
30     startTicks = SDL_GetTicks();
31 }
32 
stop()33 void Timer::stop()
34 {
35     //Stop the timer
36     started = false;
37 
38     //Unpause the timer
39     paused = false;
40 }
41 
pause()42 void Timer::pause()
43 {
44     //If the timer is running and isn't already paused
45     if( ( started == true ) && ( paused == false ) )
46     {
47         //Pause the timer
48         paused = true;
49 
50         //Calculate the paused ticks
51         pausedTicks = SDL_GetTicks() - startTicks;
52     }
53 }
54 
unpause()55 void Timer::unpause()
56 {
57     //If the timer is paused
58     if( paused == true )
59     {
60         //Unpause the timer
61         paused = false;
62 
63         //Reset the starting ticks
64         startTicks = SDL_GetTicks() - pausedTicks;
65 
66         //Reset the paused ticks
67         pausedTicks = 0;
68     }
69 }
70 
get_ticks()71 int Timer::get_ticks()
72 {
73     //If the timer is running
74     if( started == true )
75     {
76         //If the timer is paused
77         if( paused == true )
78         {
79             //Return the number of ticks when the timer was paused
80             return pausedTicks;
81         }
82         else
83         {
84             //Return the current time minus the start time
85             return SDL_GetTicks() - startTicks;
86         }
87     }
88 
89     //If the timer isn't running
90     return 0;
91 }
92 
is_started()93 bool Timer::is_started()
94 {
95     return started;
96 }
97 
is_paused()98 bool Timer::is_paused()
99 {
100     return paused;
101 }
102