Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1.     bool FrameLimit()
  2.     {
  3.         // Frame limit
  4.         double TimeWaitS;
  5.         double TimeSpent;
  6.         double prehalttime = Time::get_us(); // naively prevent error in non-critical debug code
  7.         double halttime;
  8.        
  9.         if( Time::dostart )
  10.         {
  11.             SDL_Delay(1000.0/Time::Framerate);
  12.             Time::dostart = false;
  13.             Time::deviance = 0;
  14.             halttime = Time::get_us();
  15.         }
  16.         else
  17.         {
  18.             // get time before halting
  19.             // check time between end of last frame limit delay and current code
  20.             TimeSpent = Time::get_us() - Time::simstart_us;
  21.             // calculate desired amount to wait
  22.             TimeWaitS = maximum(0.0, (1000.0 / Time::Framerate) - TimeSpent/1000.0 - Time::deviance);
  23.            
  24.             // store time for delay deviance check
  25.             prehalttime = Time::get_us();
  26.             SDL_Delay(round(TimeWaitS));
  27.            
  28.             // measure current time directly after frame limit delay
  29.             halttime = Time::get_us();
  30.            
  31.             Time::error = (halttime - prehalttime)/1000.0 - round(TimeWaitS);
  32.             // calculate amount of time that halt was inaccurate by and store it for the next iteration to compensate for
  33.             Time::deviance = (halttime - prehalttime)/1000.0 - TimeWaitS;
  34.            
  35.             // incrememt tick count
  36.             Time::ticks = fmod(Time::ticks + 1.0, Time::Framerate);
  37.         }
  38.         // push timings to buffer
  39.         Time::last_us = Time::simstart_us;
  40.         Time::delta_us = Time::simstart_us - Time::last_us;
  41.         Time::delta = Time::delta_us * Time::scale;
  42.         Time::simstart_us = halttime;
  43.         Time::frames.push_back( Time::simstart_us );
  44.  
  45.         // Throw away old timings
  46.         while ( Time::frames.size() > 2 )
  47.             Time::frames.erase( Time::frames.begin() );
  48.        
  49.         if(true) // debug
  50.         {
  51.             std::cout << std::fixed << std::setprecision(2)
  52.                       <<"\rfps "    << Time::scale / ((Time::frames.back() - Time::frames.front())/(Time::frames.size()-1))
  53.                       << " sim "    << TimeSpent / 1000
  54.                       //<< " bad "    << (Time::last - Time::simstart_us) * 1000
  55.                       //<< " ask "    << std::setprecision(3) << TimeWaitS
  56.                       << " halt "   << (halttime-prehalttime) / 1000
  57.                       //<< " miss "   << std::setprecision(0) << Time::deviance * 1000
  58.                       << " tick "   << Time::ticks
  59.                       << " dev "  << Time::deviance << "\n";
  60.         }
  61.        
  62.         return false;
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement