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() void Game::Run()
{ {
float deltaTime = (SDL_GetTicks() - m_TickCount) / 1000.0f; uint64_t currentTicksNS = SDL_GetTicksNS();
uint64_t ticks = SDL_GetTicks() - m_TickCount; uint64_t frameTicks = currentTicksNS - m_TickCount;
ticks = ticks > 0 ? ticks : 1; if(frameTicks < m_MinTicksPerFrame)
int fps = 1000/(ticks); {
m_TickCount = SDL_GetTicks(); 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(); HandleMovement();
m_Player1->Update(deltaTime); m_Player1->Update(deltaTime);

View file

@ -24,9 +24,12 @@ public:
void EndGame(); void EndGame();
private: private:
const float c_GamepadAxisDeadzone = 0.2; //TODO: make this a setting? const float c_GamepadAxisDeadzone = 0.2; //TODO: make this a setting?
const float c_NSPerSecond = 1000000000;
bool m_running = true; bool m_running = true;
uint64_t m_TickCount; uint64_t m_TickCount;
uint64_t m_MinTicksPerFrame = c_NSPerSecond/60;
std::map<SDL_Scancode, GameCommand*> m_KeyboardCommandMap; std::map<SDL_Scancode, GameCommand*> m_KeyboardCommandMap;
std::map<SDL_GamepadButton, GameCommand*> m_Gamepad1ButtonCommandMap; std::map<SDL_GamepadButton, GameCommand*> m_Gamepad1ButtonCommandMap;
std::map<SDL_GamepadButton, GameCommand*> m_Gamepad2ButtonCommandMap; std::map<SDL_GamepadButton, GameCommand*> m_Gamepad2ButtonCommandMap;