0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-13 21:43:43 +00:00

Compare commits

..

1 Commits

3 changed files with 14 additions and 18 deletions

View File

@ -31,20 +31,23 @@ public:
return game; return game;
}*/ }*/
void registerClass(CreateFunc createFunc) { void registerClass(const std::string& className, CreateFunc createFunc) {
this->creatorFunc = createFunc; this->creators[className] = createFunc;
} }
Game* create(GameInternal* gameInternal) { Game* create(const std::string& className, GameInternal* gameInternal) {
if (this->creatorFunc == nullptr) auto it = this->creators.find(className);
return nullptr; if (it != creators.end()) {
Game* game = (this->creatorFunc)(); Game* game = it->second();
game->gameInternal = gameInternal; game->gameInternal = gameInternal;
return game; return game;
} }
return nullptr;
}
private: private:
CreateFunc creatorFunc = nullptr; CreateFunc creator;
std::map<std::string, CreateFunc> creators;
}; };
/* /*

View File

@ -6,17 +6,10 @@ namespace vego {
template<typename T> template<typename T>
class GameRegistryHelper { class GameRegistryHelper {
public: public:
[[deprecated("GameRegistryHelper() does not take a className anymore")]]
GameRegistryHelper(const std::string& className) { GameRegistryHelper(const std::string& className) {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game"); static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass( GameFactory::instance().registerClass(
[]() -> Game* { return new T; } className,
);
};
GameRegistryHelper() {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass(
[]() -> Game* { return new T; } []() -> Game* { return new T; }
); );
}; };

View File

@ -179,7 +179,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
// player2.addComponent<StatEffectsComponent>(); // player2.addComponent<StatEffectsComponent>();
// player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); // player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
this->gameInstance = GameFactory::instance().create(this); this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
this->gameInstance->init(); this->gameInstance->init();
} }