💿🐜 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.

93 lines
3.0 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. * @see https://gafferongames.com/post/fix_your_timestep/
  27. */
  28. class frame_scheduler
  29. {
  30. public:
  31. frame_scheduler();
  32. /**
  33. * Sets the update callback.
  34. *
  35. * @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()`.
  36. */
  37. void set_update_callback(std::function<void(double, double)> callback);
  38. /**
  39. * Sets the render callback.
  40. *
  41. * @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.
  42. */
  43. void set_render_callback(std::function<void(double)> callback);
  44. /**
  45. * Sets the update rate.
  46. *
  47. * @param frequency Frequency, in hertz, at which the update callback should be called.
  48. */
  49. void set_update_rate(double frequency);
  50. /**
  51. * 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.
  52. *
  53. * @param duration Maximum frame duration, in seconds..
  54. */
  55. void set_max_frame_duration(double duration);
  56. /**
  57. * Returns the duration of the last frame, in seconds.
  58. */
  59. double get_frame_duration() const;
  60. /**
  61. * Resets the total elapsed time, frame duration, and internal timers.
  62. */
  63. void reset();
  64. /**
  65. * Updates the internal timers and performs the scheduled update and render callbacks.
  66. */
  67. void tick();
  68. private:
  69. std::function<void(double, double)> update_callback;
  70. std::function<void(double)> render_callback;
  71. double update_rate;
  72. double update_timestep;
  73. double max_frame_duration;
  74. double elapsed_time;
  75. double accumulator;
  76. std::chrono::high_resolution_clock::time_point frame_start;
  77. std::chrono::high_resolution_clock::time_point frame_end;
  78. double frame_duration;
  79. };
  80. #endif // ANTKEEPER_FRAME_SCHEDULER_HPP