Files
MacroLibX/runtime/Sources/Core/Fps.cpp
T
DaemoandGitHub 63fe3088bf Fix inconsistencies with the FPS scheduler (#241)
The current Fps manager will sometime skip the intended delay and start
the next frame immediately, I'm not 100% sure why it happens, but I
rewrote most of it to stay consistent and improved the overall precision
by compensating for OS wake delay after sleep and finishing with a busy
wait
I also changed the behavior of mlx_set_fps_goal to allow zero/negative
values and treat them as uncapped (like most graphics libraries)
2026-08-08 15:45:56 +02:00

38 lines
880 B
C++

#include <PreCompiled.h>
#include <Core/Fps.h>
#ifndef __APPLE__
#include <emmintrin.h>
#endif
namespace mlx
{
void FpsManager::Init()
{
m_current_time = fps_clock::now();
m_target_time = m_current_time + m_target_delta;
}
void FpsManager::WaitUntilNextFrame()
{
m_current_time = fps_clock::now();
if(m_current_time < m_target_time)
{
std::this_thread::sleep_until(m_target_time - m_sleep_margin);
m_current_time = fps_clock::now();
while (m_current_time < m_target_time)
{
#ifndef __APPLE__
_mm_pause(); // reduces CPU usage on x86 without yielding
#endif
m_current_time = fps_clock::now();
}
}
else if (m_target_time < m_current_time - m_target_delta * 4)
m_target_time = m_current_time;
m_target_time += m_target_delta;
m_delta_time = m_current_time - m_last_time_record;
m_last_time_record = m_current_time;
}
}