0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 21:23:41 +00:00

Compare commits

..

1 Commits

3 changed files with 21 additions and 27 deletions

View File

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

View File

@ -1,17 +0,0 @@
#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.addGroup((size_t) Entity::GroupLabel::PLAYERS);
this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
this->gameInstance = GameFactory::instance().create(this);
this->gameInstance->init();
}