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

View File

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

View File

@ -1,5 +1,6 @@
module;
#include <cassert>
#include <memory>
#include <SDL3/SDL.h>
@ -15,27 +16,18 @@ export namespace game
{
class Game
{
// Whether this class is currently instantiated (to prevent multiple instances)
static bool instantiated_;
// Reference to the engine
core::Engine& engine_;
// Sprite for testing
std::unique_ptr<Sprite> sprite_ = nullptr;
public:
Game() = delete;
std::unique_ptr<Sprite> sprite_{nullptr};
// Private constructor
explicit Game(core::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.
auto texture = sdl_image::LoadTexture(
@ -46,6 +38,29 @@ export namespace game
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.
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);
}
// 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)
{
dest_rect_.x = x;

View File

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

View File

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