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