1
General / How to realize a state machine based SFML?
« on: April 20, 2024, 01:38:41 pm »
Hello everyone.
I am trying to realize a simple game engine based on SFML. It has a state machine, that represents the LIFO queue. My base state class looks something like this:
Methods of the state machine are cached to be called simultaneously when the state machine has finished handle events, update logic and draw all states.
I pushed the Level state. I set pause and pushed Menu state by pressing a mouse button. It works, but I can get a few identical events (sf::Event::MouseButtonPressed) at the one frame loop and I have the three same states (but I should have the only one):
I can add a boolean flag that will collect the general value of this event and I will handle it at the "updateLogic" method. However, my state machine blocks this method when it paused.
So how and where should I change the states and how should I realize the game pause?
PS: sorry for my bad Engish. Thank you very much!
I am trying to realize a simple game engine based on SFML. It has a state machine, that represents the LIFO queue. My base state class looks something like this:
class AbstractScene
{
public:
virtual handleEvents(const sf::Event& event) = 0;
virtual updateLogic(const sf::Time& time) = 0;
virtual draw() = 0;
};
{
public:
virtual handleEvents(const sf::Event& event) = 0;
virtual updateLogic(const sf::Time& time) = 0;
virtual draw() = 0;
};
Methods of the state machine are cached to be called simultaneously when the state machine has finished handle events, update logic and draw all states.
I pushed the Level state. I set pause and pushed Menu state by pressing a mouse button. It works, but I can get a few identical events (sf::Event::MouseButtonPressed) at the one frame loop and I have the three same states (but I should have the only one):
class Level : public AbstractScene
{
public:
void handleEvents(const sf::Event& event) override
{
if(event.type == sf::MouseButtonPressed)
{
// append new scene
stateMachine.push(new Menu());
}
...
}
};
{
public:
void handleEvents(const sf::Event& event) override
{
if(event.type == sf::MouseButtonPressed)
{
// append new scene
stateMachine.push(new Menu());
}
...
}
};
I can add a boolean flag that will collect the general value of this event and I will handle it at the "updateLogic" method. However, my state machine blocks this method when it paused.
So how and where should I change the states and how should I realize the game pause?
PS: sorry for my bad Engish. Thank you very much!