Skip to content

Commit

Permalink
feat: implement Input::IsKeyPressed and Input::IsKeyReleased
Browse files Browse the repository at this point in the history
  • Loading branch information
painfulexistence committed Nov 28, 2024
1 parent 55ff0c7 commit caa68b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
12 changes: 12 additions & 0 deletions AtmosphericEngine/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Input::Input()
for (int k = static_cast<int>(Key::SPACE); k <= static_cast<int>(Key::ESCAPE); ++k) {
auto key = static_cast<Key>(k);
_keyStates[key] = KeyState::UNKNOWN;
_prevKeyStates[key] = KeyState::UNKNOWN;
}
}

Expand All @@ -25,6 +26,7 @@ void Input::Process(float dt)
{
for (int k = static_cast<int>(Key::SPACE); k <= static_cast<int>(Key::ESCAPE); ++k) {
auto key = static_cast<Key>(k);
_prevKeyStates[key] = _keyStates[key];
_keyStates[key] = Window::Get()->GetKeyState(key);
}
}
Expand All @@ -49,6 +51,16 @@ bool Input::IsKeyUp(Key key)
return _keyStates[key] == KeyState::RELEASED;
}

bool Input::IsKeyPressed(Key key)
{
return _keyStates[key] == KeyState::PRESSED && _prevKeyStates[key] == KeyState::RELEASED;
}

bool Input::IsKeyReleased(Key key)
{
return _keyStates[key] == KeyState::RELEASED && _prevKeyStates[key] == KeyState::PRESSED;
}

glm::vec2 Input::GetMousePosition() // In pixel coordinate
{
return Window::Get()->GetMousePosition();
Expand Down
5 changes: 5 additions & 0 deletions AtmosphericEngine/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ class Input : public Server

bool IsKeyUp(Key key);

bool IsKeyPressed(Key key);

bool IsKeyReleased(Key key);

glm::vec2 GetMousePosition();

private:
std::unordered_map<Key, KeyState> _keyStates;
std::unordered_map<Key, KeyState> _prevKeyStates;
};
10 changes: 5 additions & 5 deletions Example_MazeWorld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,22 @@ class MazeGame : public Application {
if (input.GetKeyDown(Key::Z)) {
player->SetPhysicsActivated(false);
}
if (input.GetKeyDown(Key::I)) {
if (input.IsKeyPressed(Key::I)) {
physics.EnableDebugUI(!isPhysicsDebugUIEnabled);
isPhysicsDebugUIEnabled = !isPhysicsDebugUIEnabled;
}
if (input.GetKeyDown(Key::O)) {
if (input.IsKeyPressed(Key::O)) {
graphics.EnableWireframe(!isWireframeEnabled);
isWireframeEnabled = !isWireframeEnabled;
}
if (input.GetKeyDown(Key::P)) {
if (input.IsKeyPressed(Key::P)) {
graphics.EnablePostProcess(!isPostProcessEnabled);
isPostProcessEnabled = !isPostProcessEnabled;
}
if (input.GetKeyDown(Key::R)) {
if (input.IsKeyPressed(Key::R)) {
graphics.ReloadShaders();
}
if (input.GetKeyDown(Key::ESCAPE)) {
if (input.IsKeyPressed(Key::ESCAPE)) {
Quit();
}
}
Expand Down

0 comments on commit caa68b6

Please sign in to comment.