time.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* time.cpp - time
  2. * Copyright (C) 2017 caryoscelus
  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. */
  17. #include <core/std/numeric.h>
  18. #include <core/time/period.h>
  19. namespace rainynite::core {
  20. bool Time::operator==(Time const& other) const {
  21. return seconds == other.seconds
  22. && frames * other.fps == other.frames * fps;
  23. }
  24. bool Time::operator<(Time const& other) const {
  25. return seconds < other.seconds
  26. || (seconds == other.seconds
  27. && frames * other.fps < other.frames * fps);
  28. }
  29. Time& Time::operator++() {
  30. set_frames(get_frames()+1);
  31. return *this;
  32. }
  33. Time& Time::operator--() {
  34. set_frames(get_frames()-1);
  35. return *this;
  36. }
  37. Time& Time::operator+=(Time const& other) {
  38. auto t = other;
  39. to_common_fps(t);
  40. set_frames(get_frames()+t.get_frames());
  41. return *this;
  42. }
  43. Time& Time::operator-=(Time const& other) {
  44. auto t = other;
  45. to_common_fps(t);
  46. set_frames(get_frames()-t.get_frames());
  47. return *this;
  48. }
  49. Time& Time::operator*=(double other) {
  50. set_frames(get_frames()*other);
  51. return *this;
  52. }
  53. Time& Time::operator/=(double other) {
  54. set_frames(get_frames()/other);
  55. return *this;
  56. }
  57. double Time::operator/(Time const& other) {
  58. return get_seconds() / other.get_seconds();
  59. }
  60. void Time::require_same_fps(Time const& other) const {
  61. // consider zero case
  62. if (fps != other.fps)
  63. throw std::runtime_error("Time: fps mis-match");
  64. }
  65. Time::fps_type Time::common_fps(Time const& other) const {
  66. return lcm(get_fps(), other.get_fps());
  67. }
  68. void Time::to_common_fps(Time& other) {
  69. auto cfps = common_fps(other);
  70. set_fps(cfps);
  71. other.set_fps(cfps);
  72. }
  73. void Time::set_frames(double frames_) {
  74. auto neg = std::copysign(1, frames_);
  75. frames_ = std::abs(frames_);
  76. int whole_frames = frames_;
  77. seconds = neg * whole_frames / fps;
  78. frames = neg * (whole_frames % fps + frames_ - whole_frames);
  79. }
  80. void Time::add_frames(double df) {
  81. set_frames(get_frames()+df);
  82. }
  83. void Time::set_seconds(double seconds_) {
  84. set_frames(seconds_*fps);
  85. }
  86. void Time::set_fps(fps_type fps_) {
  87. frames = frames*fps_/fps;
  88. fps = fps_;
  89. }
  90. void Time::change_fps(fps_type fps_) {
  91. auto total = seconds*fps + frames;
  92. fps = fps_;
  93. set_frames(total);
  94. }
  95. bool TimePeriodIter::equal(iter const& other) const {
  96. return period == other.period
  97. && (now == other.now
  98. || before_begin() && other.before_begin()
  99. || after_end() && other.after_end());
  100. }
  101. void TimePeriodIter::advance(int diff) {
  102. now.add_frames(diff);
  103. }
  104. bool TimePeriodIter::before_begin() const {
  105. return now < period.get_first();
  106. }
  107. bool TimePeriodIter::after_end() const {
  108. return now >= period.get_last();
  109. }
  110. } // namespace rainynite::core