Advertisement
Suby

timer.cpp

Jun 1st, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 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. #include "constants.h"
  6.  
  7. Timer::Timer()
  8. {
  9.     //Initialize the variables
  10.     interval = NORMAL_INTERVAL;
  11.     startTicks = 0;
  12.     pausedTicks = 0;
  13.     paused = false;
  14.     started = false;    
  15. }
  16.  
  17. void Timer::start()
  18. {
  19.     //Start the timer
  20.     started = true;
  21.    
  22.     //Unpause the timer
  23.     paused = false;
  24.    
  25.     //Get the current clock time
  26.     startTicks = SDL_GetTicks();    
  27. }
  28.  
  29. void Timer::stop()
  30. {
  31.     //Stop the timer
  32.     started = false;
  33.    
  34.     //Unpause the timer
  35.     paused = false;    
  36. }
  37.  
  38. void Timer::pause()
  39. {
  40.     //If the timer is running and isn't already paused
  41.     if( ( started == true ) && ( paused == false ) )
  42.     {
  43.         //Pause the timer
  44.         paused = true;
  45.    
  46.         //Calculate the paused ticks
  47.         pausedTicks = SDL_GetTicks() - startTicks;
  48.     }
  49. }
  50.  
  51. void Timer::unpause()
  52. {
  53.     //If the timer is paused
  54.     if( paused == true )
  55.     {
  56.         //Unpause the timer
  57.         paused = false;
  58.    
  59.         //Reset the starting ticks
  60.         startTicks = SDL_GetTicks() - pausedTicks;
  61.        
  62.         //Reset the paused ticks
  63.         pausedTicks = 0;
  64.     }
  65. }
  66.  
  67. int Timer::get_ticks()
  68. {
  69.     //If the timer is running
  70.     if( started == true )
  71.     {
  72.         //If the timer is paused
  73.         if( paused == true )
  74.         {
  75.             //Return the number of ticks when the the timer was paused
  76.             return pausedTicks;
  77.         }
  78.         else
  79.         {
  80.             //Return the current time minus the start time
  81.             return SDL_GetTicks() - startTicks;
  82.         }    
  83.     }
  84.    
  85.     //If the timer isn't running
  86.     return 0;    
  87. }
  88.  
  89. bool Timer::is_started()
  90. {
  91.     return started;    
  92. }
  93.  
  94. bool Timer::is_paused()
  95. {
  96.     return paused;    
  97. }
  98.  
  99. void Timer::setInterval(int value)
  100. {
  101.     interval = value;
  102. }
  103.  
  104. int Timer::getInterval()
  105. {
  106.     return interval;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement