time.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * time.cpp - time
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <numeric>
  19. #include <core/time/time_period.h>
  20. namespace rainynite::core {
  21. bool Time::operator==(Time const& other) const {
  22. return seconds == other.seconds
  23. && frames * other.fps == other.frames * fps;
  24. }
  25. bool Time::operator<(Time const& other) const {
  26. return seconds < other.seconds
  27. || (seconds == other.seconds
  28. && frames * other.fps < other.frames * fps);
  29. }
  30. Time& Time::operator++() {
  31. set_frames(get_frames()+1);
  32. return *this;
  33. }
  34. Time& Time::operator--() {
  35. set_frames(get_frames()-1);
  36. return *this;
  37. }
  38. Time& Time::operator+=(Time const& other) {
  39. auto t = other;
  40. to_common_fps(t);
  41. set_frames(get_frames()+t.get_frames());
  42. return *this;
  43. }
  44. Time& Time::operator-=(Time const& other) {
  45. auto t = other;
  46. to_common_fps(t);
  47. set_frames(get_frames()-t.get_frames());
  48. return *this;
  49. }
  50. Time& Time::operator*=(double other) {
  51. set_frames(get_frames()*other);
  52. return *this;
  53. }
  54. Time& Time::operator/=(double other) {
  55. set_frames(get_frames()/other);
  56. return *this;
  57. }
  58. double Time::operator/(Time const& other) {
  59. return get_seconds() / other.get_seconds();
  60. }
  61. void Time::require_same_fps(Time const& other) const {
  62. // consider zero case
  63. if (fps != other.fps)
  64. throw std::runtime_error("Time: fps mis-match");
  65. }
  66. Time::fps_type Time::common_fps(Time const& other) const {
  67. return std::lcm(get_fps(), other.get_fps());
  68. }
  69. void Time::to_common_fps(Time& other) {
  70. auto cfps = common_fps(other);
  71. set_fps(cfps);
  72. other.set_fps(cfps);
  73. }
  74. void Time::set_frames(double frames_) {
  75. auto neg = std::copysign(1, frames_);
  76. frames_ = std::abs(frames_);
  77. int whole_frames = frames_;
  78. seconds = neg * whole_frames / fps;
  79. frames = neg * (whole_frames % fps + frames_ - whole_frames);
  80. }
  81. void Time::add_frames(double df) {
  82. set_frames(get_frames()+df);
  83. }
  84. void Time::set_seconds(double seconds_) {
  85. set_frames(seconds_*fps);
  86. }
  87. void Time::set_fps(fps_type fps_) {
  88. frames = frames*fps_/fps;
  89. fps = fps_;
  90. }
  91. void Time::change_fps(fps_type fps_) {
  92. auto total = seconds*fps + frames;
  93. fps = fps_;
  94. set_frames(total);
  95. }
  96. bool TimePeriodIter::equal(iter const& other) const {
  97. return period == other.period
  98. && (now == other.now
  99. || before_begin() && other.before_begin()
  100. || after_end() && other.after_end());
  101. }
  102. void TimePeriodIter::advance(int diff) {
  103. now.add_frames(diff);
  104. }
  105. bool TimePeriodIter::before_begin() const {
  106. return now < period.get_first();
  107. }
  108. bool TimePeriodIter::after_end() const {
  109. return now >= period.get_last();
  110. }
  111. } // namespace rainynite::core