Threading for renderer, textures, entities, game state

This commit is contained in:
2026-03-11 09:01:10 +01:00
parent 755e14ad62
commit d748ca63a0
12 changed files with 218 additions and 30 deletions

25
include/object/entity.hpp Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <string>
#include <utils.hpp>
namespace Game::Renderer {
class Texture;
}
namespace Game::Object {
class Entity {
public:
Entity(std::string& name, Game::Renderer::Texture* tex) : mName(name), mTex(tex) {}
// I will define the copy and move constructors later - just deleted for now
DISABLE_COPY_AND_MOVE(Entity);
~Entity();
void start();
void update();
protected:
std::string mName;
Game::Renderer::Texture* mTex;
private:
};
}

View File

@@ -1,19 +1,27 @@
#pragma once
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include <iostream>
#include <thread>
#include <utils.hpp>
namespace Game::Renderer {
class Renderer {
public:
Renderer();
DISABLE_COPY_AND_MOVE(Renderer);
~Renderer();
bool init(SDL_Window* window);
void clear();
void present();
void run(std::stop_token stoken);
void destroy();
SDL_Renderer* getSDLRenderer() { return mRenderer; }
private:
void mClear();
void mPresent();
SDL_Renderer* mRenderer;
};
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <string>
#include <utils.hpp>
#include <renderer/renderer.hpp>
namespace Game::Renderer {
class Texture {
public:
Texture(std::string& path, Renderer* renderer, std::string id = "noname");
Texture(const Texture&);
Texture& operator=(const Texture&);
DISABLE_MOVE(Texture);
~Texture();
private:
SDL_Texture* mTex;
std::string mId;
};
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include <shared_mutex>
#include <vector>
#include <utils.hpp>
#include <object/entity.hpp>
namespace Game::State {
class GameState {
public:
static GameState& getInstance() { static GameState instance; return instance; }
// Retrieve a COPY of the entities - Only used by renderer, use getAtIndex() for generic access
const std::vector<Object::Entity>& getEntities();
// Retrieve a REFERENCE of the entities; DANGEROUS!
std::vector<Object::Entity>* getEntitiesRef();
// Update entity at index, by REFERENCE
Object::Entity* getAtIndex(size_t at);
private:
mutable std::shared_mutex mMutex;
std::vector<Object::Entity> mEntities;
};
}

View File

@@ -19,4 +19,13 @@
std::cout << "\033[33m[WARN] " << __PRETTY_FUNCTION__ << ' ' << Msg << "\033[0m\n";
#define ERROR(Msg) \
std::cout << "\033[31m[ERROR] " << __PRETTY_FUNCTION__ << ' ' << Msg << '\033[0m\n';
std::cout << "\033[31m[ERROR] " << __PRETTY_FUNCTION__ << ' ' << Msg << "\033[0m\n";
#define GAME_ENTITY(ClassName) \
class ClassName : public Object::Entity { \
using Object::Entity::Entity; // Inherit constructors \
#define END_GAME_ENTITY() \
};

View File

@@ -1,11 +1,14 @@
#pragma once
#include <string>
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include <iostream>
#include <utils.hpp>
#include <renderer/renderer.hpp>
#include <thread>
#include <functional>
#include <chrono>
namespace Game::Window {
class Window {
@@ -18,8 +21,9 @@ namespace Game::Window {
void run();
private:
SDL_Window *mWindow;
SDL_Window* mWindow;
Renderer::Renderer mRenderer;
std::jthread mRenderThread;
bool mRunning;
};
}