cleanes up deltaTime calculation and fps limit
This commit is contained in:
parent
4c3cc24de2
commit
32cdec6b7e
2 changed files with 44 additions and 21 deletions
55
src/Game.cpp
55
src/Game.cpp
|
@ -14,13 +14,13 @@
|
||||||
#include "DataStructures/Vector2D.h"
|
#include "DataStructures/Vector2D.h"
|
||||||
#include "DataStructures/Hitbox.h"
|
#include "DataStructures/Hitbox.h"
|
||||||
|
|
||||||
Game::Game(SDL_Window *window, SDL_Renderer *renderer)
|
Game::Game(SDL_Window *window, SDL_Renderer *renderer) :
|
||||||
: m_Window(window, SDL_DestroyWindow),
|
m_Player1DirectionInput({ 0,0 }),
|
||||||
|
m_Player2DirectionInput({ 0,0 }),
|
||||||
|
m_Window(window, SDL_DestroyWindow),
|
||||||
m_Renderer(renderer, SDL_DestroyRenderer),
|
m_Renderer(renderer, SDL_DestroyRenderer),
|
||||||
m_Gamepad1(nullptr, SDL_CloseGamepad),
|
m_Gamepad1(nullptr, SDL_CloseGamepad),
|
||||||
m_TickCount(0),
|
m_Gamepad2(nullptr, SDL_CloseGamepad)
|
||||||
m_Player1DirectionInput({ 0,0 }),
|
|
||||||
m_Player2DirectionInput({ 0,0 })
|
|
||||||
{
|
{
|
||||||
SDL_FRect player1Hitbox;
|
SDL_FRect player1Hitbox;
|
||||||
player1Hitbox.x = 0;
|
player1Hitbox.x = 0;
|
||||||
|
@ -29,6 +29,9 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer)
|
||||||
player1Hitbox.h = 20;
|
player1Hitbox.h = 20;
|
||||||
m_Player1 = ActorFactory::CreateMovingActor(Vector2D(0,0), player1Hitbox, player1Hitbox);
|
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
|
//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_ESCAPE, new EndGameCommand(this));
|
||||||
m_KeyboardCommandMap.emplace(SDL_SCANCODE_W, new MoveUpCommand(m_Player1DirectionButtonStatus));
|
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_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_LEFTX, new MoveXCommand(m_Player1DirectionInput));
|
||||||
m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTY, new MoveYCommand(m_Player1DirectionInput));
|
m_Gamepad1AxisCommandMap.emplace(SDL_GAMEPAD_AXIS_LEFTY, new MoveYCommand(m_Player1DirectionInput));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::Run()
|
void Game::Run()
|
||||||
{
|
{
|
||||||
uint64_t currentTicksNS = SDL_GetTicksNS();
|
float deltaTime = GetDeltaTime();
|
||||||
uint64_t frameTicks = currentTicksNS - m_TickCount;
|
ulong fps = lround(1/deltaTime);
|
||||||
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();
|
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()
|
void Game::HandleMovement()
|
||||||
{
|
{
|
||||||
NormalizedDirection direction;
|
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()
|
void Game::EndGame()
|
||||||
{
|
{
|
||||||
SDL_Event * event = new SDL_Event();
|
SDL_Event * event = new SDL_Event();
|
||||||
|
|
10
src/Game.h
10
src/Game.h
|
@ -15,20 +15,22 @@ public:
|
||||||
Game(SDL_Window *window, SDL_Renderer *renderer);
|
Game(SDL_Window *window, SDL_Renderer *renderer);
|
||||||
~Game() {};
|
~Game() {};
|
||||||
void Run();
|
void Run();
|
||||||
|
float GetDeltaTime();
|
||||||
void HandleMovement();
|
void HandleMovement();
|
||||||
void HandleKeyboardInput(const SDL_Event &event);
|
void HandleKeyboardInput(const SDL_Event &event);
|
||||||
void HandleGamepadButton(const SDL_Event &event);
|
void HandleGamepadButton(const SDL_Event &event);
|
||||||
void HandleGamepadAxis(const SDL_Event &event);
|
void HandleGamepadAxis(const SDL_Event &event);
|
||||||
void AddGamepad(SDL_JoystickID id);
|
void AddGamepad(SDL_JoystickID id);
|
||||||
void RemoveGamepad(SDL_JoystickID id);
|
void RemoveGamepad(SDL_JoystickID id);
|
||||||
|
void SetFps(uint16_t fps = 0);
|
||||||
void EndGame();
|
void EndGame();
|
||||||
private:
|
private:
|
||||||
const float c_GamepadAxisDeadzone = (float)0.2; //TODO: make this a setting?
|
const float c_GamepadAxisDeadzone = 0.2f; //TODO: make this a setting?
|
||||||
const float c_NSPerSecond = 1000000000;
|
const float c_NSPerSecond = 1000000000.0f;
|
||||||
|
|
||||||
bool m_running = true;
|
bool m_running = true;
|
||||||
uint64_t m_TickCount = 0;
|
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_Scancode, GameCommand*> m_KeyboardCommandMap;
|
||||||
std::map<SDL_GamepadButton, GameCommand*> m_Gamepad1ButtonCommandMap;
|
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_Window, decltype(&SDL_DestroyWindow)> m_Window;
|
||||||
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> m_Renderer;
|
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_Gamepad1;
|
||||||
//std::unique_ptr<SDL_Gamepad, decltype(&SDL_CloseGamepad)> m_Gamepad2;
|
std::unique_ptr<SDL_Gamepad, decltype(&SDL_CloseGamepad)> m_Gamepad2;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue