diff --git a/AtmosphericEngine/input.cpp b/AtmosphericEngine/input.cpp index e36c45b..0ca562d 100644 --- a/AtmosphericEngine/input.cpp +++ b/AtmosphericEngine/input.cpp @@ -13,6 +13,7 @@ Input::Input() for (int k = static_cast(Key::SPACE); k <= static_cast(Key::ESCAPE); ++k) { auto key = static_cast(k); _keyStates[key] = KeyState::UNKNOWN; + _prevKeyStates[key] = KeyState::UNKNOWN; } } @@ -25,6 +26,7 @@ void Input::Process(float dt) { for (int k = static_cast(Key::SPACE); k <= static_cast(Key::ESCAPE); ++k) { auto key = static_cast(k); + _prevKeyStates[key] = _keyStates[key]; _keyStates[key] = Window::Get()->GetKeyState(key); } } @@ -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(); diff --git a/AtmosphericEngine/input.hpp b/AtmosphericEngine/input.hpp index 11a43ce..7eef69b 100644 --- a/AtmosphericEngine/input.hpp +++ b/AtmosphericEngine/input.hpp @@ -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 _keyStates; + std::unordered_map _prevKeyStates; }; \ No newline at end of file diff --git a/Example_MazeWorld/main.cpp b/Example_MazeWorld/main.cpp index 6fdba94..424f1c0 100644 --- a/Example_MazeWorld/main.cpp +++ b/Example_MazeWorld/main.cpp @@ -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(); } }