Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- timer.hpp
- =================
- #ifndef TIMER_HPP
- #define TIMER_HPP
- #include <chrono>
- namespace hrishi { namespace utils {
- using clock_t = std::chrono::high_resolution_clock;
- using timepoint_t = std::chrono::high_resolution_clock::time_point;
- using time_t = std::chrono::milliseconds;
- using time_count_t = unsigned long long int;
- class Timer {
- private:
- clock_t *classy_clock;
- timepoint_t m_start;
- time_count_t m_elapsed_time;
- bool m_ignate;
- public:
- Timer();
- Timer(bool m_ignate);
- ~Timer();
- void start();
- void reset();
- time_count_t ticks_in_milli();
- };
- }}
- #endif // TIMER_HPP
- timer.cpp
- ============
- #include<timer.hpp>
- namespace hrishi { namespace utils {
- Timer::Timer(bool m_ignate):m_ignate(false) {
- if(m_ignate)
- start();
- }
- Timer::Timer():Timer(false){}
- Timer::~Timer() { delete classy_clock; }
- void Timer::start(){ m_start = classy_clock->now(); }
- void Timer::reset(){ start(); }
- time_count_t Timer::ticks_in_milli() {
- time_t diff = std::chrono::duration_cast<time_t>(classy_clock->now() - m_start);
- m_elapsed_time = diff.count();
- return m_elapsed_time;
- }
- }}
- === example use
- Timer m_time;
- mtime.start();
- if(m_time.ticks_in_milli() > 1000 )
- {
- m_time.reset();
- std::cout << m_frames << "FPS" << std::endl;
- m_frames = 0;
- }
- ++m_frames;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement