timer.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <math.h>
  17. #include "supertux/timer.hpp"
  18. Timer::Timer() :
  19. m_period(0),
  20. m_cycle_start(0),
  21. m_cycle_pause(0),
  22. m_cyclic(false)
  23. {
  24. }
  25. void
  26. Timer::start(float period, bool cyclic)
  27. {
  28. m_period = period;
  29. m_cyclic = cyclic;
  30. m_cycle_start = g_game_time;
  31. m_cycle_pause = 0;
  32. }
  33. bool
  34. Timer::check()
  35. {
  36. if (m_period == 0)
  37. return false;
  38. if (g_game_time - m_cycle_start >= m_period) {
  39. if (m_cyclic) {
  40. m_cycle_start = g_game_time - fmodf(g_game_time - m_cycle_start, m_period);
  41. } else {
  42. m_period = 0;
  43. }
  44. return true;
  45. }
  46. return false;
  47. }
  48. void
  49. Timer::pause()
  50. {
  51. float left = get_timeleft();
  52. stop();
  53. m_cycle_pause = left;
  54. }
  55. void
  56. Timer::resume()
  57. {
  58. start(m_cycle_pause);
  59. }
  60. /* EOF */