Skip to content

Commit

Permalink
core: Avoid querying the input state multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremoreau committed Sep 1, 2021
1 parent b6ebf2e commit 3d8dfa6
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/core/InputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ void InputHandler::Advance()

void InputHandler::DownEvent(std::unordered_map<size_t, IState> &map, size_t loc)
{
auto sc = map.find(loc);
if (sc == map.end())
map[static_cast<size_t>(loc)] = IState();
map[loc].mIsDown = true;
map[loc].mDownTick = mTick;
// If the key already exists, insert does not modify the associated value
// and just returns an iterator to it.
auto insertionResult = map.insert({ loc, IState() });
auto& state = insertionResult.first->second;
state.mIsDown = true;
state.mDownTick = mTick;
}

void InputHandler::UpEvent(std::unordered_map<size_t, IState> &map, size_t loc)
{
auto sc = map.find(loc);
if (sc == map.end())
map[static_cast<size_t>(loc)] = IState();
map[loc].mIsDown = false;
map[loc].mUpTick = mTick;
// If the key already exists, insert does not modify the associated value
// and just returns an iterator to it.
auto insertionResult = map.insert({ loc, IState() });
auto& state = insertionResult.first->second;
state.mIsDown = false;
state.mUpTick = mTick;
}

void InputHandler::FeedKeyboard(int key, int scancode, int action)
Expand Down Expand Up @@ -74,12 +76,15 @@ void InputHandler::FeedMouseButtons(int button, int action)

std::uint32_t InputHandler::GetState(std::unordered_map<size_t, IState> &map, size_t loc)
{
auto sc = map.find(loc);
auto const sc = map.find(loc);
if (sc == map.end())
return RELEASED;
std::uint32_t s = map[static_cast<std::uint32_t>(loc)].mIsDown ? PRESSED : RELEASED;
s |= mTick-1 == map[loc].mDownTick ? JUST_PRESSED : 0;
s |= mTick-1 == map[loc].mUpTick ? JUST_RELEASED : 0;

auto const& state = sc->second;
std::uint32_t s = state.mIsDown ? PRESSED : RELEASED;
s |= mTick-1 == state.mDownTick ? JUST_PRESSED : 0;
s |= mTick-1 == state.mUpTick ? JUST_RELEASED : 0;

return s;
}

Expand Down

0 comments on commit 3d8dfa6

Please sign in to comment.