Nazaj na multithreadanje - fonti
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <object/entity.hpp>
|
||||
#include <renderer/texture.hpp>
|
||||
#include <renderer/font.hpp>
|
||||
|
||||
namespace Game::AGame {
|
||||
class Player : public Object::Entity {
|
||||
using Object::Entity::Entity;
|
||||
|
||||
public:
|
||||
~Player() override = default;
|
||||
void start() override;
|
||||
void update() override;
|
||||
};
|
||||
25
include/game/gamemanager.hpp
Normal file
25
include/game/gamemanager.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <state/gamestate.hpp>
|
||||
#include <object/entity.hpp>
|
||||
#include <utils.hpp>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
namespace Game {
|
||||
class GameManager {
|
||||
public:
|
||||
GameManager() { LOG("Created GameManager"); };
|
||||
DISABLE_COPY_AND_MOVE(GameManager);
|
||||
~GameManager() = default;
|
||||
|
||||
// Start the game logic slave thread, which will update the gamestate and entities; Run as jthread
|
||||
void run(std::stop_token stopToken);
|
||||
void setTargetUpdatesPerSecond(int target) { mTargetUpdatesPerSecond = target; }
|
||||
int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; }
|
||||
private:
|
||||
int mTargetUpdatesPerSecond = 60;
|
||||
};
|
||||
}
|
||||
@@ -22,7 +22,7 @@ namespace Game::Object {
|
||||
Entity& operator=(const Entity&);
|
||||
Entity(Entity&&) noexcept;
|
||||
Entity& operator=(Entity&&) noexcept;
|
||||
~Entity();
|
||||
virtual ~Entity() = 0;
|
||||
|
||||
virtual void start() = 0;
|
||||
virtual void update() = 0;
|
||||
|
||||
27
include/renderer/font.hpp
Normal file
27
include/renderer/font.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <utils.hpp>
|
||||
#include <string>
|
||||
#include <renderer/texture.hpp>
|
||||
|
||||
namespace Game::Renderer {
|
||||
class Font : public Texture {
|
||||
public:
|
||||
Font(const std::string& path, SDL_Renderer* renderer, int ptSize, std::string id = "noname");
|
||||
Font(const Font&);
|
||||
Font& operator=(const Font&);
|
||||
DISABLE_MOVE(Font);
|
||||
~Font();
|
||||
|
||||
// Build the texture for the font; Call getSDLTexture() afterwards
|
||||
void build(SDL_Color color, std::string text);
|
||||
|
||||
SDL_Texture* getSDLTexture();
|
||||
std::string getId();
|
||||
private:
|
||||
TTF_Font* mFont;
|
||||
SDL_Renderer* mRenderer;
|
||||
};
|
||||
}
|
||||
@@ -8,15 +8,16 @@
|
||||
namespace Game::Renderer {
|
||||
class Texture {
|
||||
public:
|
||||
Texture(std::string id = "noname");
|
||||
Texture(const std::string& path, SDL_Renderer* renderer, std::string id = "noname");
|
||||
Texture(const Texture&);
|
||||
Texture& operator=(const Texture&);
|
||||
DISABLE_MOVE(Texture);
|
||||
~Texture();
|
||||
virtual ~Texture();
|
||||
|
||||
SDL_Texture* getSDLTexture();
|
||||
std::string getId();
|
||||
private:
|
||||
protected:
|
||||
SDL_Texture* mTex;
|
||||
std::string mId;
|
||||
};
|
||||
|
||||
@@ -4,20 +4,27 @@
|
||||
#include <memory>
|
||||
#include <utils.hpp>
|
||||
#include <object/entity.hpp>
|
||||
#include <mutex>
|
||||
|
||||
namespace Game::State {
|
||||
class GameState {
|
||||
public:
|
||||
static GameState& getInstance() { static GameState instance; return instance; }
|
||||
|
||||
// Retrieve a REFERENCE of the entities; DANGEROUS!
|
||||
std::vector<std::unique_ptr<Object::Entity>>* getEntitiesRef();
|
||||
// Update entity at index, by REFERENCE
|
||||
Object::Entity* getAtIndex(size_t at);
|
||||
// Execute work while holding the GameState mutex to keep access thread-safe.
|
||||
template<typename Fn>
|
||||
void withEntitiesLocked(Fn&& fn) {
|
||||
std::scoped_lock lock(mMutex);
|
||||
fn(mEntities);
|
||||
}
|
||||
|
||||
// Update entity at index, by REFERENCE.
|
||||
Object::Entity* getAtIndex(size_t at);
|
||||
// Add an entity to the gamestate.
|
||||
void addEntity(std::unique_ptr<Object::Entity> entity);
|
||||
|
||||
private:
|
||||
mutable std::mutex mMutex; // Shared mutex for thread safety
|
||||
std::vector<std::unique_ptr<Object::Entity>> mEntities;
|
||||
};
|
||||
}
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <thread>
|
||||
#include <utils.hpp>
|
||||
#include <renderer/renderer.hpp>
|
||||
#include <state/gamestate.hpp>
|
||||
#include <game/gamemanager.hpp>
|
||||
|
||||
namespace Game::Window {
|
||||
class Window {
|
||||
@@ -18,11 +20,17 @@ namespace Game::Window {
|
||||
bool init(int width, int height, const std::string& title);
|
||||
void run();
|
||||
|
||||
void setTargetFPS(int fps) { mTargetFPS = fps; }
|
||||
int getTargetFPS() { return mTargetFPS; }
|
||||
|
||||
Renderer::Renderer* getRenderer() { return &mRenderer; }
|
||||
|
||||
private:
|
||||
SDL_Window* mWindow;
|
||||
Renderer::Renderer mRenderer;
|
||||
Game::GameManager mGameManager;
|
||||
std::jthread mGameThread;
|
||||
bool mRunning;
|
||||
int mTargetFPS = 60;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user