Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool FrameLimit()
- {
- // Frame limit
- double TimeWaitS;
- double TimeSpent;
- double prehalttime = Time::get_us(); // naively prevent error in non-critical debug code
- double halttime;
- if( Time::dostart )
- {
- SDL_Delay(1000.0/Time::Framerate);
- Time::dostart = false;
- Time::deviance = 0;
- halttime = Time::get_us();
- }
- else
- {
- // get time before halting
- // check time between end of last frame limit delay and current code
- TimeSpent = Time::get_us() - Time::simstart_us;
- // calculate desired amount to wait
- TimeWaitS = maximum(0.0, (1000.0 / Time::Framerate) - TimeSpent/1000.0 - Time::deviance);
- // store time for delay deviance check
- prehalttime = Time::get_us();
- SDL_Delay(round(TimeWaitS));
- // measure current time directly after frame limit delay
- halttime = Time::get_us();
- Time::error = (halttime - prehalttime)/1000.0 - round(TimeWaitS);
- // calculate amount of time that halt was inaccurate by and store it for the next iteration to compensate for
- Time::deviance = (halttime - prehalttime)/1000.0 - TimeWaitS;
- // incrememt tick count
- Time::ticks = fmod(Time::ticks + 1.0, Time::Framerate);
- }
- // push timings to buffer
- Time::last_us = Time::simstart_us;
- Time::delta_us = Time::simstart_us - Time::last_us;
- Time::delta = Time::delta_us * Time::scale;
- Time::simstart_us = halttime;
- Time::frames.push_back( Time::simstart_us );
- // Throw away old timings
- while ( Time::frames.size() > 2 )
- Time::frames.erase( Time::frames.begin() );
- if(true) // debug
- {
- std::cout << std::fixed << std::setprecision(2)
- <<"\rfps " << Time::scale / ((Time::frames.back() - Time::frames.front())/(Time::frames.size()-1))
- << " sim " << TimeSpent / 1000
- //<< " bad " << (Time::last - Time::simstart_us) * 1000
- //<< " ask " << std::setprecision(3) << TimeWaitS
- << " halt " << (halttime-prehalttime) / 1000
- //<< " miss " << std::setprecision(0) << Time::deviance * 1000
- << " tick " << Time::ticks
- << " dev " << Time::deviance << "\n";
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement