1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00
openrw/rwgame/State.hpp

129 lines
2.0 KiB
C++
Raw Normal View History

2013-12-27 00:18:55 +01:00
#ifndef _GAME_STATE_HPP_
#define _GAME_STATE_HPP_
#include <functional>
#include <queue>
#include <SFML/Graphics/RenderWindow.hpp>
#include <render/ViewCamera.hpp>
2013-12-27 00:18:55 +01:00
#include "MenuSystem.hpp"
#include <glm/gtc/quaternion.hpp>
2013-12-27 00:18:55 +01:00
struct State
{
// Helper for global menu behaviour
Menu* currentMenu;
2014-01-01 04:13:23 +01:00
Menu* nextMenu;
2013-12-27 00:18:55 +01:00
State()
2014-01-01 04:13:23 +01:00
: currentMenu(nullptr), nextMenu(nullptr) {}
2013-12-27 00:18:55 +01:00
virtual void enter() = 0;
virtual void exit() = 0;
virtual void tick(float dt) = 0;
virtual void draw(sf::RenderWindow& w)
{
2014-01-01 04:13:23 +01:00
if(getCurrentMenu()) {
getCurrentMenu()->draw(w);
}
}
virtual ~State() {
if(getCurrentMenu()) {
delete getCurrentMenu();
2013-12-27 00:18:55 +01:00
}
}
void enterMenu(Menu* menu)
{
2014-01-01 04:13:23 +01:00
nextMenu = menu;
}
Menu* getCurrentMenu()
{
if(nextMenu) {
if(currentMenu) {
delete currentMenu;
}
currentMenu = nextMenu;
nextMenu = nullptr;
}
return currentMenu;
2013-12-27 00:18:55 +01:00
}
virtual void handleEvent(const sf::Event& e)
{
2014-01-01 04:13:23 +01:00
auto m = getCurrentMenu();
if(! m) return;
2013-12-27 00:18:55 +01:00
switch(e.type) {
case sf::Event::MouseButtonReleased:
2014-01-01 04:13:23 +01:00
m->click(e.mouseButton.x, e.mouseButton.y);
2013-12-27 00:18:55 +01:00
break;
2014-01-01 01:37:16 +01:00
case sf::Event::MouseMoved:
2014-01-01 04:13:23 +01:00
m->hover(e.mouseMove.x, e.mouseMove.y);
2014-01-01 01:37:16 +01:00
break;
case sf::Event::KeyPressed:
2014-01-01 04:13:23 +01:00
switch(e.key.code) {
2014-05-25 23:30:50 +02:00
default: break;
2014-01-01 04:13:23 +01:00
case sf::Keyboard::Up:
m->move(-1);
break;
case sf::Keyboard::Down:
m->move(1);
break;
case sf::Keyboard::Return:
m->activate();
break;
2014-01-01 01:37:16 +01:00
}
2013-12-27 00:18:55 +01:00
default: break;
};
}
virtual const ViewCamera& getCamera();
2013-12-27 00:18:55 +01:00
};
struct StateManager
{
static StateManager& get()
{
static StateManager m;
return m;
}
std::deque<State*> states;
void enter(State* state)
{
states.push_back(state);
state->enter();
}
2014-06-06 13:18:32 +02:00
void exec(State* state)
{
exit();
enter(state);
}
2013-12-27 00:18:55 +01:00
void tick(float dt)
{
states.back()->tick(dt);
}
void draw(sf::RenderWindow& w)
{
states.back()->draw(w);
}
void exit()
{
// TODO: Resole states being leaked.
states.back()->exit();
states.pop_back();
2013-12-27 22:58:27 +01:00
if(states.size() > 0) {
2013-12-27 00:18:55 +01:00
states.back()->enter();
}
}
};
2014-05-25 23:30:50 +02:00
#endif