0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 15:53:42 +00:00

Compare commits

...

2 Commits

3 changed files with 27 additions and 21 deletions

View File

@ -19,48 +19,37 @@ public:
return factory; return factory;
} }
void registerClass(CreateFunc createFunc) { /*Game* get() {
this->creator = createFunc;
}
Game* get() {
assert(this->gameInstance != nullptr); assert(this->gameInstance != nullptr);
return this->gameInstance; return this->gameInstance;
} }*/
Game* create(GameInternal* gameInternal) { /*Game* create(GameInternal* gameInternal) {
Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling
game->gameInternal = gameInternal; game->gameInternal = gameInternal;
this->gameInstance = game; this->gameInstance = game;
return game; return game;
} }*/
/* named game instances for future use
void registerClass(const std::string& className, CreateFunc createFunc) { void registerClass(const std::string& className, CreateFunc createFunc) {
this->creators[className] = 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); auto it = this->creators.find(className);
if (it != creators.end()) { if (it != creators.end()) {
return it->second(); Game* game = it->second();
game->gameInternal = gameInternal;
return game;
} }
return nullptr; return nullptr;
} }
*/
private: private:
CreateFunc creator; CreateFunc creator;
Game* gameInstance = nullptr; //!< \depricated std::map<std::string, CreateFunc> creators;
// std::map<std::string, CreateFunc> creators;
}; };
#define REGISTER_GAME(className) \
static bool registered_##className = []() { \
GameFactory::instance().registerClass([]() -> Game* { return new className; }); \
return true; \
}();
/* /*
#define REGISTER_GAME(className) \ #define REGISTER_GAME(className) \
static bool registered_##className = []() { \ static bool registered_##className = []() { \

View File

@ -0,0 +1,17 @@
#pragma once
#include "GameFactory.h"
namespace vego {
template<typename T>
class GameRegistryHelper {
public:
GameRegistryHelper(const std::string& className) {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass(
className,
[]() -> 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();
} }