1 module prova.util.watch;
2 
3 import derelict.sdl2.sdl;
4 
5 /**
6  * Class used for measuring time
7  */
8 class Watch
9 {
10   private bool running = false;
11   private int startTime = 0;
12   private int timeStored = 0;
13 
14   /**
15    * Returns the state of the watch
16    */
17   @property bool isRunning()
18   {
19     return running;
20   }
21 
22   /**
23    * Starts or resumes the watch
24    */
25   void start()
26   {
27     if(running)
28       timeStored = getElapsedMilliseconds();
29 
30     startTime = SDL_GetTicks();
31     running = true;
32   }
33 
34   /**
35    * Pauses the watch
36    */
37   void pause()
38   {
39     timeStored = getElapsedMilliseconds();
40     running = false;
41   }
42 
43   /**
44    * Stops timing and resets
45    */
46   void reset()
47   {
48     startTime = 0;
49     timeStored = 0;
50     running = false;
51   }
52 
53   /**
54    * Resets and starts again
55    */
56   void restart()
57   {
58     reset();
59     start();
60   }
61 
62   // we currently only track using milliseconds so,
63   // maybe this should be renamed?
64   int getElapsedMilliseconds()
65   {
66     if(startTime == 0)
67       throw new Exception("Watch never started");
68     
69     return SDL_GetTicks() - startTime + timeStored;
70   }
71 }