2016-10-17 00:56:51 +02:00
|
|
|
#ifndef RWGAME_STATEMANAGER_HPP
|
|
|
|
#define RWGAME_STATEMANAGER_HPP
|
2018-12-09 22:43:42 +01:00
|
|
|
|
2016-10-17 00:56:51 +02:00
|
|
|
#include "State.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handles current state focus and transitions
|
|
|
|
*
|
|
|
|
* Possible states:
|
|
|
|
* Foreground (topmost state)
|
|
|
|
* Background (any other position)
|
|
|
|
*
|
|
|
|
* Transitions:
|
|
|
|
* New State (at the top)
|
|
|
|
* Suspended (a state was created above us)
|
|
|
|
* Resumed (blocking state was removed)
|
|
|
|
*/
|
|
|
|
class StateManager {
|
|
|
|
public:
|
|
|
|
std::deque<std::unique_ptr<State>> states;
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
cleared = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class... Targs>
|
|
|
|
void enter(Targs&&... args) {
|
|
|
|
// Notify the previous state it has been suspended
|
|
|
|
if (!states.empty()) {
|
|
|
|
states.back()->exit();
|
|
|
|
}
|
|
|
|
states.emplace_back(std::move(std::make_unique<T>(args...)));
|
|
|
|
states.back()->enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateStack() {
|
|
|
|
if (cleared) {
|
|
|
|
states.clear();
|
|
|
|
cleared = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!states.empty() && states.back()->hasExited()) {
|
|
|
|
states.back()->exit();
|
|
|
|
states.pop_back();
|
|
|
|
if (!states.empty()) {
|
|
|
|
states.back()->enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tick(float dt) {
|
|
|
|
states.back()->tick(dt);
|
|
|
|
}
|
|
|
|
|
2018-12-01 18:57:06 +01:00
|
|
|
void draw(GameRenderer& r) {
|
2016-10-17 00:56:51 +02:00
|
|
|
states.back()->draw(r);
|
|
|
|
}
|
|
|
|
|
2018-12-18 22:43:49 +01:00
|
|
|
State* currentState() {
|
|
|
|
if (states.empty()) {
|
2017-09-17 01:43:09 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-18 22:43:49 +01:00
|
|
|
return states.back().get();
|
2017-09-17 01:43:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 00:56:51 +02:00
|
|
|
private:
|
|
|
|
bool cleared = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|