added fps limit

This commit is contained in:
Sarah Faey 2025-05-07 18:23:23 +02:00
parent 794fa5bba0
commit 7012c8c38d
2 changed files with 16 additions and 5 deletions

View file

@ -43,11 +43,19 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer)
void Game::Run()
{
float deltaTime = (SDL_GetTicks() - m_TickCount) / 1000.0f;
uint64_t ticks = SDL_GetTicks() - m_TickCount;
ticks = ticks > 0 ? ticks : 1;
int fps = 1000/(ticks);
m_TickCount = SDL_GetTicks();
uint64_t currentTicksNS = SDL_GetTicksNS();
uint64_t frameTicks = currentTicksNS - m_TickCount;
if(frameTicks < m_MinTicksPerFrame)
{
SDL_DelayNS(m_MinTicksPerFrame - frameTicks);
currentTicksNS = SDL_GetTicksNS();
frameTicks = currentTicksNS - m_TickCount;
}
float deltaTime = frameTicks / c_NSPerSecond;
m_TickCount = currentTicksNS;
long fps = 1/deltaTime;
HandleMovement();
m_Player1->Update(deltaTime);