123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /*
- sftools - Copyright (c) 2012-2013 Marco Antognini <antognini.marco@gmail.com>
- This software is provided 'as-is', without any express or implied warranty. In
- no event will the authors be held liable for any damages arising from the use
- of this software.
- Permission is granted to anyone to use this software for any purpose, including
- commercial applications, and to alter it and redistribute it freely, subject to
- the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not claim
- that you wrote the original software. If you use this software in a product,
- an acknowledgment in the product documentation would be appreciated but is
- not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- /*!
- @file sftools/Chronometer.hpp
- @brief Defines Chronometer
- */
- #ifndef __SFTOOLS_BASE_CHRONOMETER_HPP__
- #define __SFTOOLS_BASE_CHRONOMETER_HPP__
- #include <cpp3ds/System/Clock.hpp>
- /*!
- @namespace sftools
- @brief Simple and Fast Tools
- */
- namespace sftools
- {
- /*!
- @class Chronometer
- @brief Provide functionalities of a chronometer, aka stop watch
- */
- class Chronometer
- {
- public:
- /*!
- @brief Constructor
- @param initialTime Initial time elapsed
- */
- Chronometer(cpp3ds::Time initialTime = cpp3ds::Time::Zero)
- {
- reset();
- add(initialTime);
- }
- /*!
- @brief Add some time
- @param time Time to be added to the time elapsed
- @return Time elapsed
- */
- cpp3ds::Time add(cpp3ds::Time time)
- {
- m_time += time;
- if (m_state == STOPPED) m_state = PAUSED;
- return getElapsedTime();
- }
- /*!
- @brief Reset the chronometer
- @param start if true the chronometer automatically starts
- @return Time elapsed on the chronometer before the reset
- */
- cpp3ds::Time reset(bool start = false)
- {
- cpp3ds::Time time = getElapsedTime();
- m_time = cpp3ds::Time::Zero;
- m_state = STOPPED;
- if (start) resume();
- return time;
- }
- /*!
- @brief Pause the chronometer
- @return Time elapsed
- @see toggle
- */
- cpp3ds::Time pause()
- {
- if (isRunning())
- {
- m_state = PAUSED;
- m_time += m_clock.getElapsedTime();
- }
- return getElapsedTime();
- }
- /*!
- @brief Resume the chronometer
- @return Time elapsed
- @see toggle
- */
- cpp3ds::Time resume()
- {
- if (!isRunning())
- {
- m_state = RUNNING;
- m_clock.restart();
- }
- return getElapsedTime();
- }
- /*!
- @brief Pause or resume the chronometer
- If the chronometer is running the it is paused;
- otherwise it is resumes.
- @return Time elapsed
- @see pause
- @see resume
- */
- cpp3ds::Time toggle()
- {
- if (isRunning()) pause();
- else resume();
- return getElapsedTime();
- }
- /*!
- @brief Tell the chronometer is running or not
- @brief chronometer's status
- */
- bool isRunning() const
- {
- return m_state == RUNNING;
- }
- /*!
- @brief Give the amount of time elapsed since the chronometer was started
- @return Time elapsed
- */
- cpp3ds::Time getElapsedTime() const
- {
- switch (m_state) {
- case STOPPED:
- return cpp3ds::Time::Zero;
- case RUNNING:
- return m_time + m_clock.getElapsedTime();
- case PAUSED:
- return m_time;
- }
- }
- /*!
- @brief Implicit conversion to cpp3ds::Time
- @return Time elapsed
- @see getElapsedTime
- */
- operator cpp3ds::Time() const
- {
- return getElapsedTime();
- }
- private:
- enum { STOPPED, RUNNING, PAUSED } m_state; //!< state
- cpp3ds::Time m_time; //!< time counter
- cpp3ds::Clock m_clock; //!< clock
- };
- }
- #endif // __SFTOOLS_BASE_CHRONOMETER_HPP__
|