Advertisement
xerpi

C++ Timer

Apr 2nd, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. ////****   timer.h ****////
  2.  
  3. #ifndef _Timer_H_
  4. #define _Timer_H_
  5.  
  6. #include <oslib/oslib.h>
  7.  
  8. #define getClock sceKernelLibcClock
  9. #define clockType clock_t
  10.  
  11. class Timer{
  12.     public:
  13.         //Constructor
  14.             Timer(void);
  15.         //Variables
  16.             bool timer_running;
  17.             //Time variables
  18.                 clockType lastTime;
  19.                 clockType pauseTime;
  20.         //Methods
  21.             void start();
  22.             void stop();
  23.             void reset();
  24.             void osc();
  25.             bool running();
  26.             int time();
  27. };
  28.  
  29. #endif
  30.  
  31.  
  32. ////****   timer.c ****////
  33.  
  34. #include "timer.h"
  35.  
  36.     //Constructor
  37.         Timer::Timer(void){
  38.             timer_running = false;
  39.             reset();
  40.         }
  41.        
  42.     //Methods              
  43.     void Timer::start(){
  44.         if(!timer_running){
  45.             timer_running = true;
  46.             lastTime = lastTime + (getClock()-pauseTime);
  47.         }
  48.     }
  49.    
  50.     void Timer::stop(){
  51.         if(timer_running){
  52.             timer_running = false;
  53.             pauseTime = getClock();
  54.         }
  55.     }
  56.    
  57.     void Timer::reset(){
  58.         lastTime = getClock();
  59.         pauseTime = lastTime;
  60.     }
  61.     void Timer::osc(){
  62.         if(timer_running){
  63.             stop();
  64.         }else{
  65.             start();
  66.         }
  67.     }
  68.     bool Timer::running(){
  69.         return timer_running;
  70.     }
  71.    
  72.     int Timer::time(){
  73.         if(timer_running){
  74.             return (int)((getClock()-lastTime)/1000);
  75.         }else{
  76.             return (int)((pauseTime-lastTime)/1000);
  77.         }
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement