mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 09:03:42 +00:00
37 lines
762 B
C++
37 lines
762 B
C++
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <functional>
|
|
#include <stdexcept>
|
|
|
|
#include "Game.h"
|
|
|
|
class GameInternal;
|
|
|
|
class GameFactory {
|
|
public:
|
|
using CreateFunc = std::function<Game*()>;
|
|
|
|
static GameFactory& instance() {
|
|
static GameFactory factory;
|
|
return factory;
|
|
}
|
|
|
|
void registerClass(CreateFunc createFunc) {
|
|
this->creatorFunc = createFunc;
|
|
}
|
|
|
|
Game* create(GameInternal* gameInternal) {
|
|
if (this->creatorFunc == nullptr) {
|
|
throw std::runtime_error("No game implementation registered!");
|
|
return nullptr;
|
|
}
|
|
Game* game = (this->creatorFunc)();
|
|
game->gameInternal = gameInternal;
|
|
return game;
|
|
}
|
|
|
|
private:
|
|
CreateFunc creatorFunc = nullptr;
|
|
};
|