You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.2 KiB
76 lines
1.2 KiB
3 years ago
|
#ifndef GAMESTATE
|
||
|
#define GAMESTATE
|
||
|
|
||
|
#include <chrono> // time
|
||
|
#include <thread> // sleep_for
|
||
|
|
||
|
#include "inputoutput.h"
|
||
|
#include "window.h"
|
||
|
|
||
|
class GameState
|
||
|
{
|
||
|
public:
|
||
|
// Makes sure that the inherited class's destructor is called
|
||
|
virtual ~GameState() {}
|
||
|
|
||
|
virtual void initialize() {}
|
||
|
virtual void update() {}
|
||
|
virtual void render() {}
|
||
|
virtual void destroy() {}
|
||
|
|
||
|
static void sleep(int amount) {
|
||
|
// Sleep for amount ms
|
||
|
std::this_thread::sleep_for(std::chrono::milliseconds(amount));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class GameStateManager
|
||
|
{
|
||
|
public:
|
||
|
GameStateManager() : state(nullptr) {
|
||
|
Window::initialize();
|
||
|
}
|
||
|
|
||
|
~GameStateManager() {
|
||
|
if(this->state != nullptr) {
|
||
|
this->state->destroy();
|
||
|
delete this->state;
|
||
|
}
|
||
|
|
||
|
Window::destroy();
|
||
|
}
|
||
|
|
||
|
void setState(GameState* state) {
|
||
|
if(this->state != nullptr) {
|
||
|
this->state->destroy();
|
||
|
delete this->state;
|
||
|
}
|
||
|
|
||
|
this->state = state;
|
||
|
if(this->state != nullptr) {
|
||
|
this->state->initialize();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void update() {
|
||
|
GameState::sleep(15);
|
||
|
|
||
|
if(this->state != nullptr) {
|
||
|
this->state->update();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void render() {
|
||
|
if(this->state != nullptr) {
|
||
|
this->state->render();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
GameState *state;
|
||
|
};
|
||
|
|
||
|
extern GameStateManager gameStateManager;
|
||
|
|
||
|
#endif // GAMESTATE
|