From 353c17a4445bf352f371e6e3bca8ce4a71976e94 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 17 Jul 2023 07:23:38 +0200 Subject: get input states --- src/FLD.hpp | 34 +++++++++++++++++++++++++++++++++- src/joyGet.cpp | 32 ++++++++++++++++++++++++++++++++ src/keyGet.cpp | 32 ++++++++++++++++++++++++++++++++ src/mouseGet.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/joyGet.cpp create mode 100644 src/keyGet.cpp create mode 100644 src/mouseGet.cpp diff --git a/src/FLD.hpp b/src/FLD.hpp index 05677fb..958a724 100644 --- a/src/FLD.hpp +++ b/src/FLD.hpp @@ -1,8 +1,10 @@ #pragma once #include +#include +#include #include #include -#include +#include #include #include "soloud.h" @@ -13,6 +15,15 @@ public: using Error = int; using Uint = unsigned int; using MainLoop = int (*)(FLD& fld, void *udata); + enum class InputState { + Up, + Down, + Release, + Press + }; + using InputStateArray = std::array; + using MouseStateArray = std::array; + using JoyStateArray = std::array; struct Config { int width = 640; int height = 480; @@ -85,6 +96,9 @@ public: READONLY(SDL_Texture *, target, nullptr); READONLY(unsigned long, tick, 0); READONLY(Color, drawColor, Color({0, 0, 0, 1})); + READONLY(InputStateArray, keystates, {}); + READONLY(MouseStateArray, mousestates, {}); + READONLY(JoyStateArray, joystates, {}); public: FLD(const Config& config); FLD(const Config&& config); @@ -103,6 +117,24 @@ public: Error drawRectangle(const Rectangle& rect, Uint flags=NONE) const; [[nodiscard]] Image& image(const std::string& path); + + [[nodiscard]] InputState keyGet(SDL_Scancode sc) const; + [[nodiscard]] bool keyDown(SDL_Scancode sc) const; + [[nodiscard]] bool keyUp(SDL_Scancode sc) const; + [[nodiscard]] bool keyPressed(SDL_Scancode sc) const; + [[nodiscard]] bool keyReleased(SDL_Scancode sc) const; + + [[nodiscard]] InputState mouseGet(SDL_Scancode sc) const; + [[nodiscard]] bool mouseDown(SDL_Scancode sc) const; + [[nodiscard]] bool mouseUp(SDL_Scancode sc) const; + [[nodiscard]] bool mousePressed(SDL_Scancode sc) const; + [[nodiscard]] bool mouseReleased(SDL_Scancode sc) const; + + [[nodiscard]] InputState joyGet(SDL_Scancode sc) const; + [[nodiscard]] bool joyDown(SDL_Scancode sc) const; + [[nodiscard]] bool joyUp(SDL_Scancode sc) const; + [[nodiscard]] bool joyPressed(SDL_Scancode sc) const; + [[nodiscard]] bool joyReleased(SDL_Scancode sc) const; private: bool shouldQuit = false; struct { diff --git a/src/joyGet.cpp b/src/joyGet.cpp new file mode 100644 index 0000000..f48c783 --- /dev/null +++ b/src/joyGet.cpp @@ -0,0 +1,32 @@ +#include "FLD.hpp" + +FLD::InputState +FLD::joyGet(SDL_Scancode sc) const +{ + return joystates[sc]; +} + +bool +FLD::joyDown(SDL_Scancode sc) const +{ + const auto state = joyGet(sc); + return (state == InputState::Down || state == InputState::Press); +} + +bool +FLD::joyUp(SDL_Scancode sc) const +{ + return !joyDown(sc); +} + +bool +FLD::joyPressed(SDL_Scancode sc) const +{ + return (joyGet(sc) == InputState::Press); +} + +bool +FLD::joyReleased(SDL_Scancode sc) const +{ + return (joyGet(sc) == InputState::Release); +} diff --git a/src/keyGet.cpp b/src/keyGet.cpp new file mode 100644 index 0000000..005aa51 --- /dev/null +++ b/src/keyGet.cpp @@ -0,0 +1,32 @@ +#include "FLD.hpp" + +FLD::InputState +FLD::keyGet(SDL_Scancode sc) const +{ + return keystates[sc]; +} + +bool +FLD::keyDown(SDL_Scancode sc) const +{ + const auto state = keyGet(sc); + return (state == InputState::Down || state == InputState::Press); +} + +bool +FLD::keyUp(SDL_Scancode sc) const +{ + return !keyDown(sc); +} + +bool +FLD::keyPressed(SDL_Scancode sc) const +{ + return (keyGet(sc) == InputState::Press); +} + +bool +FLD::keyReleased(SDL_Scancode sc) const +{ + return (keyGet(sc) == InputState::Release); +} diff --git a/src/mouseGet.cpp b/src/mouseGet.cpp new file mode 100644 index 0000000..503757e --- /dev/null +++ b/src/mouseGet.cpp @@ -0,0 +1,32 @@ +#include "FLD.hpp" + +FLD::InputState +FLD::mouseGet(SDL_Scancode sc) const +{ + return mousestates[sc]; +} + +bool +FLD::mouseDown(SDL_Scancode sc) const +{ + const auto state = mouseGet(sc); + return (state == InputState::Down || state == InputState::Press); +} + +bool +FLD::mouseUp(SDL_Scancode sc) const +{ + return !mouseDown(sc); +} + +bool +FLD::mousePressed(SDL_Scancode sc) const +{ + return (mouseGet(sc) == InputState::Press); +} + +bool +FLD::mouseReleased(SDL_Scancode sc) const +{ + return (mouseGet(sc) == InputState::Release); +} -- cgit v1.2.3