Replace Game::initialize() with factory method
This commit is contained in:
parent
d180b8a5b4
commit
e37eb0b03f
6 changed files with 43 additions and 37 deletions
|
|
@ -1,5 +1,6 @@
|
|||
module;
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
|
|
@ -15,35 +16,49 @@ 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;
|
||||
std::unique_ptr<Sprite> sprite_{nullptr};
|
||||
|
||||
// Private constructor
|
||||
explicit Game(core::Engine& engine)
|
||||
: engine_(engine)
|
||||
{
|
||||
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
|
||||
auto texture = sdl_image::LoadTexture(
|
||||
engine_.get_renderer().get_sdl_renderer(),
|
||||
"assets/neocat.png"
|
||||
);
|
||||
|
||||
sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100);
|
||||
}
|
||||
|
||||
public:
|
||||
Game() = delete;
|
||||
|
||||
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()
|
||||
~Game()
|
||||
{
|
||||
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
|
||||
auto texture = sdl_image::LoadTexture(
|
||||
engine_.get_renderer().get_sdl_renderer(),
|
||||
"assets/neocat.png"
|
||||
);
|
||||
instantiated_ = false;
|
||||
}
|
||||
|
||||
sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100);
|
||||
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.
|
||||
|
|
@ -75,4 +90,6 @@ export namespace game
|
|||
{
|
||||
}
|
||||
};
|
||||
|
||||
bool Game::instantiated_ = false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue