Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
194
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Diverting the Game Flow – State Stack

We have seen how to push states before so it is not a big deal anymore. However, the Exit option applies something new; it only requests to pop itself without pushing anything else in its place.

In consequence, after the MenuState class is popped, the state stack is left empty. This is exactly the condition that is checked in the Application class in order to exit its loop, so the game closes naturally when the user selects this option.

Of course, if there would be another state present in the stack at this moment, it would not really be empty, and the game could not close. A good way to prevent this would be to request a clearing of the whole stack, instead of just popping the menu state.

However, by doing it this way we can detect possible programming errors faster by not concealing the presence of a state that shouldn't be there in the first place.

Pausing the game

After having a cute menu that allows the game to start playing, we find ourselves locked again inside GameState. Indeed, we can play, but what about when we get tired of it or just want to take a break? That's when the pause screen comes into the scene!

We make use of the PauseState class not only as a way to rest and go have a coffee, but also as a gateway for going back to the main menu.

The implementation of this screen follows the line of the previous ones although it has some remarks and little things that make it special. To begin with, it is a state that is not meant to work by itself, but rather on the top of the state stack, living simultaneously with GameState, at least.

Because of this, we apply directly the concepts we implemented in the StateStack mechanism. The screen of PauseState is transparent and we can see the game in the background:

void PauseState::draw()

{

sf::RenderWindow& window = *getContext().window; window.setView(window.getDefaultView());

sf::RectangleShape backgroundShape; backgroundShape.setFillColor(sf::Color(0, 0, 0, 150)); backgroundShape.setSize(sf::Vector2f(window.getSize()));

window.draw(backgroundShape);

window.draw(mPausedText);

window.draw(mInstructionText);

}

[ 128 ]

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]