timer_sync.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* timer_sync.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 TIMER_SYNC_H
  31. #define TIMER_SYNC_H
  32. #include "core/engine.h"
  33. struct MainFrameTime {
  34. float idle_step; // time to advance idles for (argument to process())
  35. int physics_steps; // number of times to iterate the physics engine
  36. void clamp_idle(float min_idle_step, float max_idle_step);
  37. };
  38. class MainTimerSync {
  39. // wall clock time measured on the main thread
  40. uint64_t last_cpu_ticks_usec;
  41. uint64_t current_cpu_ticks_usec;
  42. // logical game time since last physics timestep
  43. float time_accum;
  44. // current difference between wall clock time and reported sum of idle_steps
  45. float time_deficit;
  46. // number of frames back for keeping accumulated physics steps roughly constant.
  47. // value of 12 chosen because that is what is required to make 144 Hz monitors
  48. // behave well with 60 Hz physics updates. The only worse commonly available refresh
  49. // would be 85, requiring CONTROL_STEPS = 17.
  50. static const int CONTROL_STEPS = 12;
  51. // sum of physics steps done over the last (i+1) frames
  52. int accumulated_physics_steps[CONTROL_STEPS];
  53. // typical value for accumulated_physics_steps[i] is either this or this plus one
  54. int typical_physics_steps[CONTROL_STEPS];
  55. int fixed_fps;
  56. protected:
  57. // returns the fraction of p_frame_slice required for the timer to overshoot
  58. // before advance_core considers changing the physics_steps return from
  59. // the typical values as defined by typical_physics_steps
  60. float get_physics_jitter_fix();
  61. // gets our best bet for the average number of physics steps per render frame
  62. // return value: number of frames back this data is consistent
  63. int get_average_physics_steps(float &p_min, float &p_max);
  64. // advance physics clock by p_idle_step, return appropriate number of steps to simulate
  65. MainFrameTime advance_core(float p_frame_slice, int p_iterations_per_second, float p_idle_step);
  66. // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
  67. MainFrameTime advance_checked(float p_frame_slice, int p_iterations_per_second, float p_idle_step);
  68. // determine wall clock step since last iteration
  69. float get_cpu_idle_step();
  70. public:
  71. MainTimerSync();
  72. // start the clock
  73. void init(uint64_t p_cpu_ticks_usec);
  74. // set measured wall clock time
  75. void set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec);
  76. //set fixed fps
  77. void set_fixed_fps(int p_fixed_fps);
  78. // advance one frame, return timesteps to take
  79. MainFrameTime advance(float p_frame_slice, int p_iterations_per_second);
  80. };
  81. #endif // TIMER_SYNC_H