Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. void zz_system::tick_time ()
  2. {
  3. if (use_fixed_framerate) {
  4. add_time(80);
  5. }
  6. #ifdef USE_SMOOTH_FRAMERATE
  7. else {
  8. static float accum_error = 0; // accumulated diff error
  9. const zz_time MAX_ACCUM_ERROR = ZZ_TICK_PER_SEC / 1; // max error = 1 sec delay
  10. const zz_time MIN_ACCUM_ERROR = ZZ_TICK_PER_SEC / 10; // min error = .1 sec delay
  11.  
  12. const int MIN_FRAMERATE = 5; // not to get slower than min_framerate
  13. const zz_time MAX_DIFF = ZZ_TICK_PER_SEC / MIN_FRAMERATE; // time(1sec) / frame
  14. const zz_time ERROR_MAX_DIFF = ZZ_TICK_PER_SEC*1; // 1 sec
  15.  
  16. zz_time now = timer.get_time();
  17. zz_time diff = now - last_time_; // new real diff
  18.  
  19. if (use_force_diff_time) {
  20. diff = force_diff_time;
  21. use_force_diff_time = false;
  22. force_diff_time = 0;
  23. }
  24.  
  25. if (diff > ERROR_MAX_DIFF) { // zhotest
  26. diff = ERROR_MAX_DIFF;
  27. }
  28.  
  29. if (0) {
  30. if (diff > MAX_DIFF) {
  31. accum_error += diff - MAX_DIFF;
  32. if (accum_error > MAX_ACCUM_ERROR) { // if error exceeded max error
  33. diff = zz_time(accum_error) + MAX_DIFF;
  34. accum_error = 0;
  35. }
  36. else {
  37. diff = MAX_DIFF;
  38. }
  39. }
  40. else {
  41. if (accum_error < MIN_ACCUM_ERROR) {
  42. diff += zz_time(accum_error);
  43. accum_error = 0;
  44. }
  45. else {
  46. diff += MIN_ACCUM_ERROR;
  47. accum_error -= float(MIN_ACCUM_ERROR);
  48. assert(accum_error >= 0);
  49. }
  50. }
  51. }
  52. else if (1) {
  53. const zz_time ACCUM_ERROR_FULL = 2 * ZZ_TICK_PER_SEC; // not to exceed 2 sec
  54. const float KP = .3f; // magic constants. damper
  55. zz_time extra_add = 0;
  56.  
  57. accum_error += float(diff); // it's current time
  58.  
  59. if (accum_error > ACCUM_ERROR_FULL) {
  60. extra_add = zz_time(accum_error) - ACCUM_ERROR_FULL;
  61. accum_error = ACCUM_ERROR_FULL;
  62. }
  63.  
  64. assert(accum_error <= ACCUM_ERROR_FULL);
  65.  
  66. assert(KP <= 1.0f);
  67. diff = zz_time (KP * accum_error * accum_error / ACCUM_ERROR_FULL);
  68.  
  69.  
  70. accum_error -= diff;
  71. diff += extra_add;
  72.  
  73. assert(accum_error >= 0);
  74. }
  75.  
  76. set_current_time(now); // update current_time and last_time
  77. last_time = now - diff; // overwrite last_time set by set_current_time
  78. last_time_ = now;
  79.  
  80. //ZZ_LOG("system: tick_time(). diff(%10d),last(%10d), now(%10d)\n", diff, last_time, now);
  81. //ZZ_LOG("%f, \n", accum_error);
  82. //ZZ_LOG("%d, \n", diff);
  83. }
  84. #else // NON SMOOTH FRAMERATE
  85. else { // dynamic frame rate
  86. set_current_time();
  87. }
  88. #endif
  89. }
Add Comment
Please, Sign In to add comment