main_timer_sync.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* main_timer_sync.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef MAIN_TIMER_SYNC_H
  31. #define MAIN_TIMER_SYNC_H
  32. #include "core/config/engine.h"
  33. struct MainFrameTime {
  34. double process_step; // delta time to advance during process()
  35. int physics_steps; // number of times to iterate the physics engine
  36. double interpolation_fraction; // fraction through the current physics tick
  37. void clamp_process_step(double min_process_step, double max_process_step);
  38. };
  39. class MainTimerSync {
  40. // wall clock time measured on the main thread
  41. uint64_t last_cpu_ticks_usec = 0;
  42. uint64_t current_cpu_ticks_usec = 0;
  43. // logical game time since last physics timestep
  44. double time_accum = 0;
  45. // current difference between wall clock time and reported sum of process_steps
  46. double time_deficit = 0;
  47. // number of frames back for keeping accumulated physics steps roughly constant.
  48. // value of 12 chosen because that is what is required to make 144 Hz monitors
  49. // behave well with 60 Hz physics updates. The only worse commonly available refresh
  50. // would be 85, requiring CONTROL_STEPS = 17.
  51. static const int CONTROL_STEPS = 12;
  52. // sum of physics steps done over the last (i+1) frames
  53. int accumulated_physics_steps[CONTROL_STEPS];
  54. // typical value for accumulated_physics_steps[i] is either this or this plus one
  55. int typical_physics_steps[CONTROL_STEPS];
  56. int fixed_fps = 0;
  57. protected:
  58. // returns the fraction of p_physics_step required for the timer to overshoot
  59. // before advance_core considers changing the physics_steps return from
  60. // the typical values as defined by typical_physics_steps
  61. double get_physics_jitter_fix();
  62. // gets our best bet for the average number of physics steps per render frame
  63. // return value: number of frames back this data is consistent
  64. int get_average_physics_steps(double &p_min, double &p_max);
  65. // advance physics clock by p_process_step, return appropriate number of steps to simulate
  66. MainFrameTime advance_core(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);
  67. // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
  68. MainFrameTime advance_checked(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);
  69. // determine wall clock step since last iteration
  70. double get_cpu_process_step();
  71. public:
  72. MainTimerSync();
  73. // start the clock
  74. void init(uint64_t p_cpu_ticks_usec);
  75. // set measured wall clock time
  76. void set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec);
  77. //set fixed fps
  78. void set_fixed_fps(int p_fixed_fps);
  79. // advance one frame, return timesteps to take
  80. MainFrameTime advance(double p_physics_step, int p_physics_ticks_per_second);
  81. };
  82. #endif // MAIN_TIMER_SYNC_H