Suby

timer.cpp

May 25th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*Timer class taken from Lazy Foo' Productions [http://lazyfoo.net/index.php]*/
  2.  
  3. #include "SDL.h"
  4. #include "timer.h"
  5.  
  6. Timer::Timer()
  7. {
  8.     //Initialize the variables
  9.     startTicks = 0;
  10.     pausedTicks = 0;
  11.     paused = false;
  12.     started = false;    
  13. }
  14.  
  15. void Timer::start()
  16. {
  17.     //Start the timer
  18.     started = true;
  19.    
  20.     //Unpause the timer
  21.     paused = false;
  22.    
  23.     //Get the current clock time
  24.     startTicks = SDL_GetTicks();    
  25. }
  26.  
  27. void Timer::stop()
  28. {
  29.     //Stop the timer
  30.     started = false;
  31.    
  32.     //Unpause the timer
  33.     paused = false;    
  34. }
  35.  
  36. void Timer::pause()
  37. {
  38.     //If the timer is running and isn't already paused
  39.     if( ( started == true ) && ( paused == false ) )
  40.     {
  41.         //Pause the timer
  42.         paused = true;
  43.    
  44.         //Calculate the paused ticks
  45.         pausedTicks = SDL_GetTicks() - startTicks;
  46.     }
  47. }
  48.  
  49. void Timer::unpause()
  50. {
  51.     //If the timer is paused
  52.     if( paused == true )
  53.     {
  54.         //Unpause the timer
  55.         paused = false;
  56.    
  57.         //Reset the starting ticks
  58.         startTicks = SDL_GetTicks() - pausedTicks;
  59.        
  60.         //Reset the paused ticks
  61.         pausedTicks = 0;
  62.     }
  63. }
  64.  
  65. int Timer::get_ticks()
  66. {
  67.     //If the timer is running
  68.     if( started == true )
  69.     {
  70.         //If the timer is paused
  71.         if( paused == true )
  72.         {
  73.             //Return the number of ticks when the the timer was paused
  74.             return pausedTicks;
  75.         }
  76.         else
  77.         {
  78.             //Return the current time minus the start time
  79.             return SDL_GetTicks() - startTicks;
  80.         }    
  81.     }
  82.    
  83.     //If the timer isn't running
  84.     return 0;    
  85. }
  86.  
  87. bool Timer::is_started()
  88. {
  89.     return started;    
  90. }
  91.  
  92. bool Timer::is_paused()
  93. {
  94.     return paused;    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment