💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_FRAME_SCHEDULER_HPP
  20. #define ANTKEEPER_FRAME_SCHEDULER_HPP
  21. #include <chrono>
  22. #include <functional>
  23. /**
  24. * Schedules fixed-timestep update calls and variable timestep render calls.
  25. */
  26. class frame_scheduler
  27. {
  28. public:
  29. frame_scheduler();
  30. /**
  31. * Sets the update callback.
  32. *
  33. * @param callback Function which takes two parameters: `t`, the total elapsed time, and `dt`, delta time (timestep) which is calculated as `1.0 / update_rate`. This function will be called at the frequency specified by `frame_scheduler::set_update_rate()`.
  34. */
  35. void set_update_callback(std::function<void(double, double)> callback);
  36. /**
  37. * Sets the render callback.
  38. *
  39. * @param callback Function which takes one parameter: `alpha`, which is a factor that can be used to interpolate between the previous and current update states.
  40. */
  41. void set_render_callback(std::function<void(double)> callback);
  42. /**
  43. * Sets the update rate.
  44. *
  45. * @param frequency Frequency, in hertz, at which the update callback should be called.
  46. */
  47. void set_update_rate(double frequency);
  48. /**
  49. * Sets the maximum duration of a frame. This limits the number of times the update callback is called per frame, thereby preventing a "spiral of death", in which the update callback is called too many times per frame while trying to catch up to the target update rate.
  50. *
  51. * @param duration Maximum frame duration, in seconds..
  52. */
  53. void set_max_frame_duration(double duration);
  54. /**
  55. * Returns the duration of the last frame, in seconds.
  56. */
  57. double get_frame_duration() const;
  58. /**
  59. * Resets the total elapsed time, frame duration, and internal timers.
  60. */
  61. void reset();
  62. /**
  63. * Updates the internal timers and performs the scheduled update and render callbacks.
  64. */
  65. void tick();
  66. private:
  67. std::function<void(double, double)> update_callback;
  68. std::function<void(double)> render_callback;
  69. double update_rate;
  70. double update_timestep;
  71. double max_frame_duration;
  72. double elapsed_time;
  73. double accumulator;
  74. std::chrono::high_resolution_clock::time_point frame_start;
  75. std::chrono::high_resolution_clock::time_point frame_end;
  76. double frame_duration;
  77. };
  78. #endif // ANTKEEPER_FRAME_SCHEDULER_HPP