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

Diverting the Game Flow – State Stack

Delayed pop/push operations

As you can see in the source, the StateStack class provides the pushState() and popState() functions to let us add and remove states from the active stack.

However, in the middle of an event or update iteration by the stack, it is not possible to alter the active state stack because it would generate a conflict when adding/ removing objects to a container that is being iterated.

Because of this, those functions don't immediately push or pop states into the stack, but rather register these actions in a pending action list, so they can be processed later, when it's safe.

Then, inside your own state logic code, you call requestStackPush() and requestStackPop(), allowing the states to alter the stack from within their own code, without risking the safety of the program, thanks to the delayed processing of push and pop operations.

A special kind of pop operation is also provided, allowing a state to call requestStackClear(), which will completely empty the active stack.

These delayed processing operations are done in the following function:

void StateStack::applyPendingChanges()

{

FOREACH(PendingChange change, mPendingList)

{

switch (change.action)

{

case Push: mStack.push_back(createState(change.stateID)); break;

case Pop: mStack.pop_back(); break;

case Clear: mStack.clear(); break;

}

}

mPendingList.clear();

}

[ 120 ]

www.it-ebooks.info

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