Game Engine Architecture

(Ben Green) #1
353

if (m_sampleCount != 0)
{
return static_cast<F32>(m_sum)
/ static_cast<F32>(m_sampleCount);
}
return 0.0f;
}
};

8.5.4. Detecting Input Events


The low-level HID interface typically provides the game with the current
states of the device’s various inputs. However, games are oft en interested
in detecting events, such as changes in state, rather than just inspecting the
current state each frame. The most common HID events are probably butt on
down (pressed) and butt on up (released), but of course we can detect other
kinds of events as well.


8.5.4.1. Button Up and Button Down


Let’s assume for the moment that our butt ons’ input bits are 0 when not pressed
and 1 when pressed. The easiest way to detect a change in butt on state is to
keep track of the butt ons’ state bits as observed last frame and compare them
to the state bits observed this frame. If they diff er, we know an event occurred.
The current state of each butt on tells us whether the event is a butt on-up or a
butt on-down.
We can use simple bit-wise operators to detect butt on-down and but-
ton-up events. Given a 32-bit word buttonStates, containing the current
state bits of up to 32 butt ons, we want to generate two new 32-bit words:
one for butt on-down events which we’ll call buttonDowns and one for
butt on-up events which we’ll call buttonUps. In both cases, the bit corre-
sponding to each butt on will be 0 if the event has not occurred this frame
and 1 if it has. To implement this, we also need last frame’s butt on states,
prevButtonStates.
The exclusive OR (XOR) operator produces a 0 if its two inputs are iden-
tical and a 1 if they diff er. So if we apply the XOR operator to the previous
and current butt on state words, we’ll get 1’s only for butt ons whose states
have changed between last frame and this frame. To determine whether the
event is a butt on-up or a butt on-down, we need to look at the current state
of each butt on. Any butt on whose state has changed that is currently down
generates a butt on-down event, and vice-versa for butt on-up events. The fol-
lowing code applies these ideas in order to generate our two butt on event
words:


8.5. Game Engine HID Systems

Free download pdf