From what I’ve seen, the Controls class can be summarized by these three functions:
void Set(unsigned buttons, bool down = true)
{
if (down)
buttons_ |= buttons;
else
buttons_ &= ~buttons;
}
/// Check if a button is held down.
bool IsDown(unsigned button) const
{
return (buttons_ & button) != 0;
}
/// Check if a button was pressed on this frame. Requires previous frame's controls.
bool IsPressed(unsigned button, const Controls &previousControls) const
{
return (buttons_ & button) != 0 && (previousControls.buttons_ & button) == 0;
}
How is this code able to keep track of the state of any number of buttons in a single button
variable? Is this the standard way to keep track of user input?