When Game::tick()
is called, cerlib polls events that occur in the system and stores them in a queue. This queue is emptied automatically at the beginning of every game tick.
An event is defined as a type switch (Variant
) of concrete event types:
using Event = Variant<WindowShownEvent,
WindowHiddenEvent,
...>;
You can handle events using the Game::handleAllEvents()
method:
while (game.tick()) {
game.handleAllEvents(
[](KeyPressEvent e) {
logInfo("Key {} was pressed, with modifiers {}.",
e.key, e.modifiers);
},
[](KeyReleaseEvent e) {
logInfo("Key {} was released, with modifiers {}.",
e.key, e.modifiers);
},
cerIgnoreEvent,
);
}
The cerIgnoreEvent
identifier is required in order to ignore all other event types. It’s implemented as a convenience macro that just represents a [](auto){}
lambda, which is a closure that accepts any remaining type and discards it.
Iterate events manually
If you have special requirements for event processing, you can iterate the game’s event queue manually. The Game::events()
method returns a span of all currently queued events.
while (game.tick()) {
auto events = game.events(); // : Span<Event>
for (const auto& event : events()) {
std::visit(util::VariantSwitch{
[](KeyPressEvent e) {
logInfo("Key {} was pressed, with modifiers {}.",
e.key, e.modifiers);
},
[](KeyReleaseEvent e) {
logInfo("Key {} was released, with modifiers {}.",
e.key, e.modifiers);
},
cerIgnoreEvent,
},
event);
}
}