deltatime

This commit is contained in:
2026-03-12 21:23:31 +01:00
parent 74159a6fda
commit 4c07694a25
7 changed files with 28 additions and 13 deletions

View File

@@ -4,7 +4,7 @@ namespace Game::AGame {
void Player::start() {
LOG("Created the Player");
if (mTex && mTex->getId() == "Arial") {
if (mTex && mTex->getId() == "Roboto") {
LOG("Player texture is a font");
// Treat as Font and build it
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;
//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
//LOG(mName << " position: " << mTransform.x << ' ' << mTransform.y);
//LOG("DeltaTime: " << deltaTime);
}
}

View File

@@ -6,21 +6,29 @@ namespace Game {
using namespace std::chrono_literals;
LOG("GameManager thread started");
mLastUpdate = clock::now(); // Get the update
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 auto frameDuration = std::chrono::duration<double>(1.0 / static_cast<double>(updatesPerSecond));
const auto frameStart = std::chrono::steady_clock::now();
try {
State::GameState::getInstance().withEntitiesLocked([](auto& entities) {
State::GameState::getInstance().withEntitiesLocked([seconds](auto& entities) {
for (auto& entity : entities) {
entity->update();
entity->update(seconds);
}
});
} catch (const std::exception& e) {
ERROR("Exception in GameManager thread: " << e.what());
}
mLastUpdate = now;
const auto elapsed = std::chrono::steady_clock::now() - frameStart;
const auto remaining = frameDuration - elapsed;
if (remaining > 0s) {

View File

@@ -14,9 +14,9 @@ int main() {
Window::Window window = Window::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>("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();

View File

@@ -15,14 +15,16 @@ namespace Game::Renderer {
}
}
Font::Font(const Font& other) {
this->mTex = other.mTex;
Font::Font(const Font& other)
: Texture(other) {
this->mFont = other.mFont;
}
Font& Font::operator=(const Font& other) {
this->mTex = other.mTex;
this->mFont = other.mFont;
if (this != &other) {
Texture::operator=(other);
this->mFont = other.mFont;
}
return *this;
}