From 8c5c5c7215e835ea8d6e08c008b4b648fe86743d Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Thu, 13 Jun 2024 23:22:21 +0200 Subject: [PATCH] removed define marco --- include/GameFactory.h | 29 +++++++++-------------------- include/GameRegistryHelper.h | 17 +++++++++++++++++ src/GameInternal.cpp | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 include/GameRegistryHelper.h diff --git a/include/GameFactory.h b/include/GameFactory.h index 24fcfcb..443a2d6 100644 --- a/include/GameFactory.h +++ b/include/GameFactory.h @@ -19,48 +19,37 @@ public: return factory; } - void registerClass(CreateFunc createFunc) { - this->creator = createFunc; - } - - Game* get() { + /*Game* get() { assert(this->gameInstance != nullptr); return this->gameInstance; - } + }*/ - Game* create(GameInternal* gameInternal) { + /*Game* create(GameInternal* gameInternal) { Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling game->gameInternal = gameInternal; this->gameInstance = game; return game; - } + }*/ - /* named game instances for future use void registerClass(const std::string& className, CreateFunc createFunc) { this->creators[className] = createFunc; } - Game* create(const std::string& className) { + Game* create(const std::string& className, GameInternal* gameInternal) { auto it = this->creators.find(className); if (it != creators.end()) { - return it->second(); + Game* game = it->second(); + game->gameInternal = gameInternal; + return game; } return nullptr; } - */ private: CreateFunc creator; - Game* gameInstance = nullptr; //!< \depricated - // std::map creators; + std::map creators; }; -#define REGISTER_GAME(className) \ - static bool registered_##className = []() { \ - GameFactory::instance().registerClass([]() -> Game* { return new className; }); \ - return true; \ - }(); - /* #define REGISTER_GAME(className) \ static bool registered_##className = []() { \ diff --git a/include/GameRegistryHelper.h b/include/GameRegistryHelper.h new file mode 100644 index 0000000..b980d70 --- /dev/null +++ b/include/GameRegistryHelper.h @@ -0,0 +1,17 @@ +#pragma once + +#include "GameFactory.h" + +namespace vego { + template + class GameRegistryHelper { + public: + GameRegistryHelper(const std::string& className) { + static_assert(std::is_base_of::value, "Your class must inherit from Game"); + GameFactory::instance().registerClass( + className, + []() -> Game* { return new T; } + ); + }; + }; +} \ No newline at end of file diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 0491db5..148a32e 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -179,7 +179,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he player2.addComponent(); 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(); }