Threading for renderer, textures, entities, game state
This commit is contained in:
@@ -1,21 +1,33 @@
|
||||
#include <window/window.hpp>
|
||||
|
||||
namespace Game::Window {
|
||||
Window::Window() : mWindow(nullptr), mRunning(false), mRenderer() {}
|
||||
Window::Window() : mWindow(nullptr), mRunning(false) {
|
||||
}
|
||||
|
||||
Window::~Window() {
|
||||
// Stop render thread
|
||||
if (mRenderThread.joinable()) {
|
||||
mRenderThread.request_stop();
|
||||
mRenderThread.join();
|
||||
}
|
||||
|
||||
mRenderer.destroy();
|
||||
|
||||
if (mWindow) {
|
||||
SDL_DestroyWindow(mWindow);
|
||||
mWindow = nullptr;
|
||||
mRunning = false;
|
||||
LOG("Window destroyed successfully");
|
||||
SDL_Quit();
|
||||
}
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
bool Window::init(int width, int height, const std::string& title) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) { return false; }
|
||||
mWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
ERROR("Failed to initialize SDL: " << SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
mWindow = SDL_CreateWindow(title.c_str(), width, height, SDL_WINDOW_RESIZABLE);
|
||||
if (!mWindow) {
|
||||
ERROR("Failed to create window: " << SDL_GetError());
|
||||
SDL_Quit();
|
||||
@@ -24,12 +36,14 @@ namespace Game::Window {
|
||||
|
||||
LOG("Window created successfully");
|
||||
|
||||
// Spawn new thread for renderer
|
||||
if (!mRenderer.init(mWindow)) {
|
||||
SDL_DestroyWindow(mWindow);
|
||||
mWindow = nullptr;
|
||||
SDL_Quit();
|
||||
return false;
|
||||
}
|
||||
mRenderThread = std::jthread(std::bind_front(&Renderer::Renderer::run, &mRenderer));
|
||||
|
||||
mRunning = true;
|
||||
|
||||
@@ -40,16 +54,12 @@ namespace Game::Window {
|
||||
SDL_Event event;
|
||||
while (mRunning) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
mRunning = false;
|
||||
}
|
||||
|
||||
// Handle other events (e.g., keyboard, mouse) here
|
||||
}
|
||||
|
||||
mRenderer.clear();
|
||||
// Render game objects here
|
||||
mRenderer.present();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user