mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 10:13:42 +00:00
implemented GameFactory to instance an implementation of game
This commit is contained in:
parent
68a3e48131
commit
5182312887
18
include/Game.h
Normal file
18
include/Game.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class GameInternal;
|
||||||
|
|
||||||
|
// TODO: add managers here
|
||||||
|
class Game {
|
||||||
|
public:
|
||||||
|
virtual ~Game() {}
|
||||||
|
|
||||||
|
virtual void init() = 0;
|
||||||
|
virtual void update() = 0;
|
||||||
|
|
||||||
|
GameInternal* gameInternal; //!< \deprecated
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// game factory include to simplify imports in implementation
|
||||||
|
#include "GameFactory.h"
|
||||||
70
include/GameFactory.h
Normal file
70
include/GameFactory.h
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "Game.h"
|
||||||
|
|
||||||
|
class GameInternal;
|
||||||
|
|
||||||
|
class GameFactory {
|
||||||
|
public:
|
||||||
|
using CreateFunc = std::function<Game*()>;
|
||||||
|
|
||||||
|
static GameFactory& instance() {
|
||||||
|
static GameFactory factory;
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerClass(CreateFunc createFunc) {
|
||||||
|
this->creator = createFunc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Game* get() {
|
||||||
|
assert(this->gameInstance != nullptr);
|
||||||
|
return this->gameInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
auto it = this->creators.find(className);
|
||||||
|
if (it != creators.end()) {
|
||||||
|
return it->second();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
private:
|
||||||
|
CreateFunc creator;
|
||||||
|
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 = []() { \
|
||||||
|
GameFactory::instance().registerClass(#className, []() -> Game* { return new className; }); \
|
||||||
|
return true; \
|
||||||
|
}();
|
||||||
|
*/
|
||||||
@ -17,13 +17,7 @@ class CollisionHandler;
|
|||||||
class TextureManager;
|
class TextureManager;
|
||||||
class SoundManager;
|
class SoundManager;
|
||||||
class Map;
|
class Map;
|
||||||
|
class Game;
|
||||||
namespace engine {
|
|
||||||
extern gamefunction init;
|
|
||||||
extern gamefunction update;
|
|
||||||
|
|
||||||
extern GameInternal* game; // this is a temporary fix to remove artifacts of chicken_game from the engine while the API is not yet finalized
|
|
||||||
}
|
|
||||||
|
|
||||||
class GameInternal
|
class GameInternal
|
||||||
{
|
{
|
||||||
@ -69,6 +63,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Game* gameInstance;
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
bool isRunning = false;
|
bool isRunning = false;
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
|
|||||||
@ -14,8 +14,8 @@
|
|||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
#include "StatEffectsComponent.h"
|
#include "StatEffectsComponent.h"
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
|
#include "Game.h"
|
||||||
GameInternal* engine::game = nullptr; // will be initialized in constructor
|
#include "GameFactory.h"
|
||||||
|
|
||||||
GameInternal::GameInternal() :
|
GameInternal::GameInternal() :
|
||||||
manager(this),
|
manager(this),
|
||||||
@ -27,9 +27,7 @@ GameInternal::GameInternal() :
|
|||||||
player1(manager.addEntity()),
|
player1(manager.addEntity()),
|
||||||
player2(manager.addEntity()),
|
player2(manager.addEntity()),
|
||||||
wall(manager.addEntity())
|
wall(manager.addEntity())
|
||||||
{
|
{};
|
||||||
engine::game = this;
|
|
||||||
};
|
|
||||||
|
|
||||||
GameInternal::~GameInternal() = default;
|
GameInternal::~GameInternal() = default;
|
||||||
|
|
||||||
@ -181,7 +179,8 @@ 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);
|
||||||
|
|
||||||
engine::init();
|
this->gameInstance = GameFactory::instance().create(this);
|
||||||
|
this->gameInstance->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||||
@ -299,7 +298,7 @@ void GameInternal::update()
|
|||||||
manager.refresh();
|
manager.refresh();
|
||||||
manager.update();
|
manager.update();
|
||||||
|
|
||||||
engine::update(); // TODO: this might have to be split up into two update functions, before and after manager...
|
this->gameInstance->update(); // TODO: this might have to be split up into two update functions, before and after manager...
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameInternal::render()
|
void GameInternal::render()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user