mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 22:23:43 +00:00
Compare commits
2 Commits
d76ef6a869
...
fb27460c9d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb27460c9d | ||
| 8c5c5c7215 |
@ -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<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) \
|
||||
static bool registered_##className = []() { \
|
||||
|
||||
17
include/GameRegistryHelper.h
Normal file
17
include/GameRegistryHelper.h
Normal 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; }
|
||||
);
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -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(this);
|
||||
this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
|
||||
this->gameInstance->init();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user