Game Development Reference
In-Depth Information
bool game::OnInput(std::shared_ptr<input_device> device)
{
if ( device->Active(eInput::Forward) )
{
// move forward
m_player->MoveForward();
}
}
And of course, we need to install our function pointer at some point during initialization,
or during construction, where we do this may depend on many factors.
void game::Init()
{
m_inputSystem.InstallInputCallback(game::OnInput);
}
Finally, we are able to invoke the callback whenever an input from the user is received.
void input_system::Update()
{
if ( keyboard->IsKeyPressed() )
{
if (m_inputCallback != nullptr )
{
m_inputCallback(keyboard);
}
}
}
Notice that we check if the pointer is valid, it's doesn't always need to be required, as the
system should be able to run even if we are not interested in providing a callback.
Thebiggest problem withthisexample isthat itisapplying asolution toageneral problem
within a specific feature, the danger of doing this is that we can end up applying the same
solutionindifferentpartsofthecode,creatingcodethatisduplicatedanddifficulttomain-
tain.
To find a more general way to solve this problem, we can draw inspiration from the way
C# implements delegates to create reusable, general purpose callbacks.
Search WWH ::




Custom Search