deltatime
This commit is contained in:
@@ -11,6 +11,6 @@ namespace Game::AGame {
|
|||||||
public:
|
public:
|
||||||
~Player() override = default;
|
~Player() override = default;
|
||||||
void start() override;
|
void start() override;
|
||||||
void update() override;
|
void update(float deltaTime) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,8 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace Game {
|
namespace Game {
|
||||||
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
class GameManager {
|
class GameManager {
|
||||||
public:
|
public:
|
||||||
GameManager() { LOG("Created GameManager"); };
|
GameManager() { LOG("Created GameManager"); };
|
||||||
@@ -21,5 +23,6 @@ namespace Game {
|
|||||||
int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; }
|
int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; }
|
||||||
private:
|
private:
|
||||||
int mTargetUpdatesPerSecond = 60;
|
int mTargetUpdatesPerSecond = 60;
|
||||||
|
clock::time_point mLastUpdate;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@ namespace Game::Object {
|
|||||||
virtual ~Entity() = 0;
|
virtual ~Entity() = 0;
|
||||||
|
|
||||||
virtual void start() = 0;
|
virtual void start() = 0;
|
||||||
virtual void update() = 0;
|
virtual void update(float deltaTime) = 0;
|
||||||
void render(Game::Renderer::Renderer* renderer);
|
void render(Game::Renderer::Renderer* renderer);
|
||||||
|
|
||||||
// Setters and getters
|
// Setters and getters
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace Game::AGame {
|
|||||||
void Player::start() {
|
void Player::start() {
|
||||||
LOG("Created the Player");
|
LOG("Created the Player");
|
||||||
|
|
||||||
if (mTex && mTex->getId() == "Arial") {
|
if (mTex && mTex->getId() == "Roboto") {
|
||||||
LOG("Player texture is a font");
|
LOG("Player texture is a font");
|
||||||
// Treat as Font and build it
|
// Treat as Font and build it
|
||||||
std::shared_ptr<Renderer::Font> font = std::dynamic_pointer_cast<Renderer::Font>(mTex);
|
std::shared_ptr<Renderer::Font> font = std::dynamic_pointer_cast<Renderer::Font>(mTex);
|
||||||
@@ -16,10 +16,12 @@ namespace Game::AGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::update() {
|
void Player::update(float deltaTime) {
|
||||||
if (!mIsActive) return;
|
if (!mIsActive) return;
|
||||||
//LOG("Updated Player");
|
//LOG("Updated Player");
|
||||||
mTransform.x += 0.5f; // Move right at a constant speed for testing
|
mTransform.x += 1.f; // Move right at a constant speed for testing
|
||||||
mTransform.rotation += 1.f; // Rotate clockwise for testing
|
mTransform.rotation += 1.f; // Rotate clockwise for testing
|
||||||
|
//LOG(mName << " position: " << mTransform.x << ' ' << mTransform.y);
|
||||||
|
//LOG("DeltaTime: " << deltaTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,21 +6,29 @@ namespace Game {
|
|||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
LOG("GameManager thread started");
|
LOG("GameManager thread started");
|
||||||
|
|
||||||
|
mLastUpdate = clock::now(); // Get the update
|
||||||
|
|
||||||
while (!stopToken.stop_requested()) {
|
while (!stopToken.stop_requested()) {
|
||||||
|
clock::time_point now = clock::now();
|
||||||
|
std::chrono::duration<float> elapsedDt = now - mLastUpdate;
|
||||||
|
float seconds = elapsedDt.count();
|
||||||
|
|
||||||
const int updatesPerSecond = std::max(1, mTargetUpdatesPerSecond);
|
const int updatesPerSecond = std::max(1, mTargetUpdatesPerSecond);
|
||||||
const auto frameDuration = std::chrono::duration<double>(1.0 / static_cast<double>(updatesPerSecond));
|
const auto frameDuration = std::chrono::duration<double>(1.0 / static_cast<double>(updatesPerSecond));
|
||||||
const auto frameStart = std::chrono::steady_clock::now();
|
const auto frameStart = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
State::GameState::getInstance().withEntitiesLocked([](auto& entities) {
|
State::GameState::getInstance().withEntitiesLocked([seconds](auto& entities) {
|
||||||
for (auto& entity : entities) {
|
for (auto& entity : entities) {
|
||||||
entity->update();
|
entity->update(seconds);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
ERROR("Exception in GameManager thread: " << e.what());
|
ERROR("Exception in GameManager thread: " << e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mLastUpdate = now;
|
||||||
|
|
||||||
const auto elapsed = std::chrono::steady_clock::now() - frameStart;
|
const auto elapsed = std::chrono::steady_clock::now() - frameStart;
|
||||||
const auto remaining = frameDuration - elapsed;
|
const auto remaining = frameDuration - elapsed;
|
||||||
if (remaining > 0s) {
|
if (remaining > 0s) {
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ int main() {
|
|||||||
Window::Window window = Window::Window();
|
Window::Window window = Window::Window();
|
||||||
window.init(1280, 720, "Game Window");
|
window.init(1280, 720, "Game Window");
|
||||||
|
|
||||||
Object::Transform t1{100.f, 100.f, 0.f, 1.f, 1.f};
|
//Object::Transform t1{100.f, 100.f, 0.f, 1.f, 1.f};
|
||||||
State::GameState::getInstance().addEntity(std::make_unique<AGame::Player>("Player", std::make_shared<Game::Renderer::Texture>("../resources/missing_texture.png", window.getRenderer()->getSDLRenderer()), Object::DEFAULT_TRANSFORM));
|
State::GameState::getInstance().addEntity(std::make_unique<AGame::Player>("Player", std::make_shared<Game::Renderer::Texture>("../resources/missing_texture.png", window.getRenderer()->getSDLRenderer()), Object::DEFAULT_TRANSFORM));
|
||||||
State::GameState::getInstance().addEntity(std::make_unique<AGame::Player>("Player2", std::make_shared<Game::Renderer::Font>("../resources/roboto.ttf", window.getRenderer()->getSDLRenderer(), 128, "Arial"), t1));
|
//State::GameState::getInstance().addEntity(std::make_unique<AGame::Player>("Player2", std::make_shared<Game::Renderer::Font>("../resources/roboto.ttf", window.getRenderer()->getSDLRenderer(), 128, "Roboto"), t1));
|
||||||
|
|
||||||
window.run();
|
window.run();
|
||||||
|
|
||||||
|
|||||||
@@ -15,14 +15,16 @@ namespace Game::Renderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Font::Font(const Font& other) {
|
Font::Font(const Font& other)
|
||||||
this->mTex = other.mTex;
|
: Texture(other) {
|
||||||
this->mFont = other.mFont;
|
this->mFont = other.mFont;
|
||||||
}
|
}
|
||||||
|
|
||||||
Font& Font::operator=(const Font& other) {
|
Font& Font::operator=(const Font& other) {
|
||||||
this->mTex = other.mTex;
|
if (this != &other) {
|
||||||
|
Texture::operator=(other);
|
||||||
this->mFont = other.mFont;
|
this->mFont = other.mFont;
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user