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

96 lines
3.0 KiB

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