Replace Game::initialize() with factory method

This commit is contained in:
Lexi / Zoe 2025-11-23 23:46:10 +01:00
parent d180b8a5b4
commit e37eb0b03f
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
6 changed files with 43 additions and 37 deletions

View File

@ -15,8 +15,8 @@ export
{ {
class App class App
{ {
std::unique_ptr<core::Engine> engine_ = nullptr; std::unique_ptr<core::Engine> engine_{nullptr};
std::unique_ptr<game::Game> game_ = nullptr; std::unique_ptr<game::Game> game_{nullptr};
public: public:
App() = default; App() = default;
@ -36,10 +36,9 @@ export
// Initialize SDL subsystems // Initialize SDL subsystems
sdl::Init(sdl::InitFlags::Video | sdl::InitFlags::Events); sdl::Init(sdl::InitFlags::Video | sdl::InitFlags::Events);
// Create engine (includes window and renderer) and game state
engine_ = core::Engine::create(); engine_ = core::Engine::create();
game_ = game::Game::create(*engine_);
game_ = std::make_unique<game::Game>(*engine_);
game_->initialize();
} }
catch (const std::runtime_error& e) { catch (const std::runtime_error& e) {
std::cerr << "Unhandled exception during initialization: " << e.what() << '\n'; std::cerr << "Unhandled exception during initialization: " << e.what() << '\n';

View File

@ -59,12 +59,12 @@ export namespace core
0 0
); );
return std::unique_ptr<Engine>( return std::unique_ptr<Engine>{
new Engine{ new Engine{
std::move(sdl_window), std::move(sdl_window),
Renderer{std::move(sdl_renderer)} Renderer{std::move(sdl_renderer)}
} }
); };
} }
bool keep_running() const bool keep_running() const

View File

@ -1,5 +1,6 @@
module; module;
#include <cassert>
#include <memory> #include <memory>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
@ -15,27 +16,18 @@ export namespace game
{ {
class Game class Game
{ {
// Whether this class is currently instantiated (to prevent multiple instances)
static bool instantiated_;
// Reference to the engine
core::Engine& engine_; core::Engine& engine_;
// Sprite for testing // Sprite for testing
std::unique_ptr<Sprite> sprite_ = nullptr; std::unique_ptr<Sprite> sprite_{nullptr};
public:
Game() = delete;
// Private constructor
explicit Game(core::Engine& engine) explicit Game(core::Engine& engine)
: engine_(engine) : engine_(engine)
{}
// No copy or move operations because we have a reference to the engine
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
~Game() = default;
void initialize()
{ {
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar. // TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
auto texture = sdl_image::LoadTexture( auto texture = sdl_image::LoadTexture(
@ -46,6 +38,29 @@ export namespace game
sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100); sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100);
} }
public:
Game() = delete;
// No copy or move operations because we have a reference to the engine
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
~Game()
{
instantiated_ = false;
}
static std::unique_ptr<Game> create(core::Engine& engine)
{
// Prevent the class from being instantiated multiple times
assert(!instantiated_);
return std::unique_ptr<Game>{
new Game(engine)
};
}
// Handles an SDL event. Returns true if the event has been handled. // Handles an SDL event. Returns true if the event has been handled.
bool handle_event(const sdl::Event* event) const bool handle_event(const sdl::Event* event) const
{ {
@ -75,4 +90,6 @@ export namespace game
{ {
} }
}; };
bool Game::instantiated_ = false;
} }

View File

@ -29,16 +29,6 @@ export namespace game
dest_rect_.h = static_cast<float>(height); dest_rect_.h = static_cast<float>(height);
} }
// Don't allow copy operations
Sprite(const Sprite&) = delete;
Sprite& operator=(const Sprite&) = delete;
// Default move operations
Sprite(Sprite&& other) = default;
Sprite& operator=(Sprite&& other) = default;
~Sprite() = default;
void move(const float x, const float y) void move(const float x, const float y)
{ {
dest_rect_.x = x; dest_rect_.x = x;

View File

@ -21,10 +21,10 @@ export namespace sdl
std::unique_ptr< std::unique_ptr<
SDL_Texture, SDL_Texture,
utils::FuncDeleter<SDL_DestroyTexture> utils::FuncDeleter<SDL_DestroyTexture>
> raw_texture_ = nullptr; > raw_texture_;
public: public:
Texture() = default; Texture() = delete;
explicit Texture(SDL_Texture* raw_texture) explicit Texture(SDL_Texture* raw_texture)
: raw_texture_{raw_texture} : raw_texture_{raw_texture}
@ -45,10 +45,10 @@ export namespace sdl
std::unique_ptr< std::unique_ptr<
SDL_Renderer, SDL_Renderer,
utils::FuncDeleter<SDL_DestroyRenderer> utils::FuncDeleter<SDL_DestroyRenderer>
> raw_renderer_ = nullptr; > raw_renderer_;
public: public:
Renderer() = default; Renderer() = delete;
explicit Renderer(SDL_Renderer* raw_renderer) explicit Renderer(SDL_Renderer* raw_renderer)
: raw_renderer_{raw_renderer} : raw_renderer_{raw_renderer}

View File

@ -18,10 +18,10 @@ export namespace sdl
std::unique_ptr< std::unique_ptr<
SDL_Window, SDL_Window,
utils::FuncDeleter<SDL_DestroyWindow> utils::FuncDeleter<SDL_DestroyWindow>
> raw_window_ = nullptr; > raw_window_;
public: public:
Window() = default; Window() = delete;
explicit Window(SDL_Window* raw_window) explicit Window(SDL_Window* raw_window)
: raw_window_{raw_window} : raw_window_{raw_window}