timer.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_SUPERTUX_TIMER_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_TIMER_HPP
  18. #include "supertux/globals.hpp"
  19. /** Simple timer designed to be used in the update functions of
  20. objects */
  21. class Timer final
  22. {
  23. public:
  24. Timer();
  25. /** start the timer with the given period (in seconds). If
  26. cyclic=true then the timer will be reset after each period. Set
  27. period to zero if you want to disable the timer. */
  28. void start(float period, bool cyclic = false);
  29. /** returns true if a period (or more) passed since start call or last
  30. successful check */
  31. bool check();
  32. /** stop the timer */
  33. void stop() { start(0); }
  34. /** pause the timer */
  35. void pause();
  36. /** resume (unpause) the timer */
  37. void resume();
  38. /** returns the period of the timer or 0 if it isn't started */
  39. float get_period() const { return m_period; }
  40. float get_timeleft() const { return m_period - (g_game_time - m_cycle_start); }
  41. float get_timegone() const { return g_game_time - m_cycle_start; }
  42. float get_progress() const { return get_timegone() / get_period(); }
  43. bool started() const { return (m_period != 0 && get_timeleft() > 0); }
  44. bool paused() const { return m_cycle_pause != 0; }
  45. private:
  46. float m_period;
  47. float m_cycle_start;
  48. float m_cycle_pause;
  49. bool m_cyclic;
  50. private:
  51. Timer(const Timer&) = delete;
  52. Timer& operator=(const Timer&) = delete;
  53. };
  54. #endif
  55. /* EOF */