From 32cdec6b7e89e1755ea62f247331b6f655c83879 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Wed, 7 May 2025 22:52:40 +0200 Subject: [PATCH] cleanes up deltaTime calculation and fps limit --- src/Game.cpp | 55 ++++++++++++++++++++++++++++++++++++---------------- src/Game.h | 10 ++++++---- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index e261d70..a52808a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -14,13 +14,13 @@ #include "DataStructures/Vector2D.h" #include "DataStructures/Hitbox.h" -Game::Game(SDL_Window *window, SDL_Renderer *renderer) - : m_Window(window, SDL_DestroyWindow), +Game::Game(SDL_Window *window, SDL_Renderer *renderer) : + m_Player1DirectionInput({ 0,0 }), + m_Player2DirectionInput({ 0,0 }), + m_Window(window, SDL_DestroyWindow), m_Renderer(renderer, SDL_DestroyRenderer), m_Gamepad1(nullptr, SDL_CloseGamepad), - m_TickCount(0), - m_Player1DirectionInput({ 0,0 }), - m_Player2DirectionInput({ 0,0 }) + m_Gamepad2(nullptr, SDL_CloseGamepad) { SDL_FRect player1Hitbox; player1Hitbox.x = 0; @@ -29,6 +29,9 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer) player1Hitbox.h = 20; m_Player1 = ActorFactory::CreateMovingActor(Vector2D(0,0), player1Hitbox, player1Hitbox); + //TODO: read game settings from config file + SetFps(60); + //TODO: read keybinds from config if it exists. otherwise initialze standard and write file m_KeyboardCommandMap.emplace(SDL_SCANCODE_ESCAPE, new EndGameCommand(this)); m_KeyboardCommandMap.emplace(SDL_SCANCODE_W, new MoveUpCommand(m_Player1DirectionButtonStatus)); @@ -41,22 +44,12 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer) m_Gamepad1ButtonCommandMap.emplace(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, new MoveRightCommand(m_Player1DirectionButtonStatus)); m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTX, new MoveXCommand(m_Player1DirectionInput)); m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTY, new MoveYCommand(m_Player1DirectionInput)); - } void Game::Run() { - 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; + float deltaTime = GetDeltaTime(); + ulong fps = lround(1/deltaTime); HandleMovement(); @@ -75,6 +68,20 @@ void Game::Run() } +float Game::GetDeltaTime() +{ + uint64_t currentTicksNS = SDL_GetTicksNS(); + uint64_t frameTicks = currentTicksNS - m_TickCount; + if(frameTicks < m_MinTicksPerFrame) + { + SDL_DelayPrecise(m_MinTicksPerFrame - frameTicks); + currentTicksNS = SDL_GetTicksNS(); + frameTicks = currentTicksNS - m_TickCount; + } + m_TickCount = currentTicksNS; + return frameTicks / c_NSPerSecond; +} + void Game::HandleMovement() { NormalizedDirection direction; @@ -156,6 +163,20 @@ void Game::RemoveGamepad(SDL_JoystickID id) } } +/// @brief sets the fps limit, 0 = no limit +/// @param fps +void Game::SetFps(uint16_t fps) +{ + if(fps == 0) + { + m_MinTicksPerFrame = 1; + } + else + { + m_MinTicksPerFrame = c_NSPerSecond/fps; + } +} + void Game::EndGame() { SDL_Event * event = new SDL_Event(); diff --git a/src/Game.h b/src/Game.h index 5e0ea99..3286d3f 100644 --- a/src/Game.h +++ b/src/Game.h @@ -15,20 +15,22 @@ public: Game(SDL_Window *window, SDL_Renderer *renderer); ~Game() {}; void Run(); + float GetDeltaTime(); void HandleMovement(); void HandleKeyboardInput(const SDL_Event &event); void HandleGamepadButton(const SDL_Event &event); void HandleGamepadAxis(const SDL_Event &event); void AddGamepad(SDL_JoystickID id); void RemoveGamepad(SDL_JoystickID id); + void SetFps(uint16_t fps = 0); void EndGame(); private: - const float c_GamepadAxisDeadzone = (float)0.2; //TODO: make this a setting? - const float c_NSPerSecond = 1000000000; + const float c_GamepadAxisDeadzone = 0.2f; //TODO: make this a setting? + const float c_NSPerSecond = 1000000000.0f; bool m_running = true; uint64_t m_TickCount = 0; - uint64_t m_MinTicksPerFrame = c_NSPerSecond/60; + uint64_t m_MinTicksPerFrame; std::map m_KeyboardCommandMap; std::map m_Gamepad1ButtonCommandMap; @@ -46,7 +48,7 @@ private: std::unique_ptr m_Window; std::unique_ptr m_Renderer; std::unique_ptr m_Gamepad1; - //std::unique_ptr m_Gamepad2; + std::unique_ptr m_Gamepad2; };