cleanes up deltaTime calculation and fps limit

This commit is contained in:
Sarah Faey 2025-05-07 22:52:40 +02:00
parent 4c3cc24de2
commit 32cdec6b7e
2 changed files with 44 additions and 21 deletions

View file

@ -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();

View file

@ -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<SDL_Scancode, GameCommand*> m_KeyboardCommandMap;
std::map<SDL_GamepadButton, GameCommand*> m_Gamepad1ButtonCommandMap;
@ -46,7 +48,7 @@ private:
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> m_Window;
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> m_Renderer;
std::unique_ptr<SDL_Gamepad, decltype(&SDL_CloseGamepad)> m_Gamepad1;
//std::unique_ptr<SDL_Gamepad, decltype(&SDL_CloseGamepad)> m_Gamepad2;
std::unique_ptr<SDL_Gamepad, decltype(&SDL_CloseGamepad)> m_Gamepad2;
};