diff --git a/src/Core/Game.cpp b/src/Core/Game.cpp index a834115..5e08773 100644 --- a/src/Core/Game.cpp +++ b/src/Core/Game.cpp @@ -16,20 +16,6 @@ Game::~Game() { SDL_Quit(); } -f64 Game::getDeltaTime() { - static u64 currentTime = 0; - static u64 lastTime = 0; - - lastTime = currentTime; - currentTime = SDL_GetPerformanceCounter(); - - return ((currentTime - lastTime) * 1000 / (double)SDL_GetPerformanceFrequency()); -} - -f64 Game::getDeltaTimeInSeconds() { - return this->getDeltaTime() / 1000; -} - void Game::run() { SDL_Event event = { 0 }; while (isRunning) { @@ -55,5 +41,9 @@ void Game::run() { SDL_RenderClear(this->renderer); this->player->render(this); SDL_RenderPresent(this->renderer); + + this->lastTime = this->currentTime; + this->currentTime = SDL_GetPerformanceCounter(); + this->deltaTime = (this->currentTime - this->lastTime) / (f64)SDL_GetPerformanceFrequency(); } } \ No newline at end of file diff --git a/src/Core/Game.hpp b/src/Core/Game.hpp index dc5b230..8a01ba3 100644 --- a/src/Core/Game.hpp +++ b/src/Core/Game.hpp @@ -11,11 +11,12 @@ struct Game { SDL_Renderer* renderer = nullptr; bool isRunning = true; std::unordered_map isKeyDown = {}; + u64 lastTime = 0; + u64 currentTime = 0; + f64 deltaTime = 0; Player* player = nullptr; Game(); ~Game(); - f64 getDeltaTime(); - f64 getDeltaTimeInSeconds(); void run(); }; diff --git a/src/Entity/Player.cpp b/src/Entity/Player.cpp index cfb469c..2a7bed2 100644 --- a/src/Entity/Player.cpp +++ b/src/Entity/Player.cpp @@ -2,6 +2,8 @@ #include "Core/Game.hpp" #include "Core/Types.hpp" +#include "glm/fwd.hpp" +#include Player::Player(glm::vec2 startingPosition) { this->position = startingPosition; @@ -11,8 +13,10 @@ Player::~Player() {} void Player::update(Game* game) { glm::vec2 direction = { 0, 0 }; - f32 speed = 1; - f32 dt = game->getDeltaTimeInSeconds(); + f32 distance = 1000; + f32 time = game->deltaTime; + + std::cout << time << std::endl; if (game->isKeyDown[SDLK_UP]) { direction.y -= 1; @@ -31,7 +35,7 @@ void Player::update(Game* game) { } if (glm::length(direction) > 0) { - this->position += glm::normalize(direction * speed * dt); + this->position += glm::normalize(direction) * distance * time; } }