diff --git a/src/Game.cpp b/src/Game.cpp index d798315..afa31c9 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -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); diff --git a/src/Game.h b/src/Game.h index 68f49a8..e958a4b 100644 --- a/src/Game.h +++ b/src/Game.h @@ -24,9 +24,12 @@ public: void EndGame(); private: const float c_GamepadAxisDeadzone = 0.2; //TODO: make this a setting? + const float c_NSPerSecond = 1000000000; bool m_running = true; uint64_t m_TickCount; + uint64_t m_MinTicksPerFrame = c_NSPerSecond/60; + std::map m_KeyboardCommandMap; std::map m_Gamepad1ButtonCommandMap; std::map m_Gamepad2ButtonCommandMap;