Nazaj na multithreadanje - fonti

This commit is contained in:
2026-03-12 16:18:51 +01:00
parent 834f0b29c3
commit 74159a6fda
20 changed files with 254 additions and 43 deletions

View File

@@ -1,11 +1,18 @@
#include <window/window.hpp>
namespace Game::Window {
Window::Window() : mWindow(nullptr), mRenderer(), mRunning(false) { }
Window::Window() : mWindow(nullptr), mRenderer(), mGameManager(), mRunning(false) { }
Window::~Window() {
mRenderer.destroy();
// Try to kill the game slave thread
if (mGameThread.joinable()) {
mGameThread.request_stop();
mGameThread.join();
LOG("Game thread stopped successfully");
}
if (mWindow) {
SDL_DestroyWindow(mWindow);
mWindow = nullptr;
@@ -20,6 +27,12 @@ namespace Game::Window {
return false;
}
if (!TTF_Init()) {
ERROR("Failed to initialize SDL_ttf: " << SDL_GetError());
SDL_Quit();
return false;
}
mWindow = SDL_CreateWindow(title.c_str(), width, height, SDL_WINDOW_RESIZABLE);
if (!mWindow) {
ERROR("Failed to create window: " << SDL_GetError());
@@ -36,6 +49,8 @@ namespace Game::Window {
return false;
}
mGameThread = std::jthread(std::bind_front(&Game::GameManager::run, &mGameManager));
mRunning = true;
return true;
@@ -52,13 +67,14 @@ namespace Game::Window {
// Handle other events (e.g., keyboard, mouse) here
}
/*
auto entities = State::GameState::getInstance().getEntitiesRef();
for (auto& entity : *entities) {
entity->update();
}
}*/
mRenderer.renderFrame();
SDL_Delay(16); // ~60 FPS target, maybe make dynamic based on avg. frame time - TODO
SDL_Delay(1000 / mTargetFPS); // Delay to cap the frame rate to the target FPS
}
}
}