| |
//ctimer.h
// MoMad
#include <time.h>
class CTimer
{
private:
time_t m_current_time;
time_t m_seconds_elapsed;
time_t m_start_time;
bool m_paused;
bool m_is_running;
public:
CTimer();
void reset();
void start();
void stop();
unsigned long getElapsedSecs();
};
// Mohamed Nuur
//ctimer.cpp
#include "ctimer.h"
CTimer::CTimer()
{
m_paused = false;
m_is_running = false;
m_seconds_elapsed = 0;
m_start_time = 0;
m_current_time = 0;
}
void CTimer::start()
{
if( !m_is_running )
{
m_is_running = true;
m_start_time = time(0);
}
}
void CTimer::reset()
{
if( !m_is_running )
{
//reset the timer.
m_start_time = time(0);
m_seconds_elapsed = 0;
}
else
{
//stop then reset then start again.
stop();
reset();
start();
}
}
void CTimer::stop()
{
if( m_is_running )
{
if(m_start_time == 0)
{
m_start_time = time(0);
}
//stop the time, add elapsed time, reset the first flag
m_current_time = time(0);
m_seconds_elapsed += (m_current_time - m_start_time);
m_start_time = time(0);
m_is_running = false;
}
}
unsigned long CTimer::getElapsedSecs()
{
if( m_is_running )
{
//Note: stop() updates the elapsed seconds.
stop();
start();
}
return m_seconds_elapsed;
} |