Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. timer.hpp
  2. =================
  3. #ifndef TIMER_HPP
  4. #define TIMER_HPP
  5.  
  6. #include <chrono>
  7.  
  8. namespace hrishi { namespace utils {
  9.  
  10. using clock_t      = std::chrono::high_resolution_clock;
  11. using timepoint_t  = std::chrono::high_resolution_clock::time_point;
  12. using time_t       = std::chrono::milliseconds;
  13. using time_count_t = unsigned long long int;
  14.  
  15.  
  16. class Timer {
  17. private:
  18.     clock_t *classy_clock;
  19.     timepoint_t m_start;
  20.     time_count_t m_elapsed_time;
  21.     bool m_ignate;
  22.  
  23. public:
  24.     Timer();
  25.     Timer(bool m_ignate);
  26.     ~Timer();
  27.     void start();
  28.     void reset();
  29.     time_count_t ticks_in_milli();
  30. };
  31.  
  32. }}
  33.  
  34. #endif // TIMER_HPP
  35.  
  36.  
  37. timer.cpp
  38. ============
  39.  
  40. #include<timer.hpp>
  41.  
  42. namespace hrishi { namespace utils {
  43.  
  44. Timer::Timer(bool m_ignate):m_ignate(false) {
  45.     if(m_ignate)
  46.         start();
  47. }
  48. Timer::Timer():Timer(false){}
  49.  
  50. Timer::~Timer() { delete classy_clock; }
  51.  
  52. void Timer::start(){ m_start = classy_clock->now(); }
  53.  
  54. void Timer::reset(){ start(); }
  55.  
  56. time_count_t Timer::ticks_in_milli() {
  57.     time_t diff = std::chrono::duration_cast<time_t>(classy_clock->now() - m_start);
  58.     m_elapsed_time = diff.count();
  59.     return m_elapsed_time;
  60. }
  61.  
  62.  
  63. }}
  64.  
  65.  
  66.  
  67. === example use
  68.  
  69.     Timer m_time;
  70.     mtime.start();
  71.  
  72.    
  73.      if(m_time.ticks_in_milli() > 1000 )
  74.       {
  75.           m_time.reset();
  76.           std::cout << m_frames << "FPS" << std::endl;
  77.           m_frames = 0;
  78.       }
  79.  
  80.       ++m_frames;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement