summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-17 07:23:38 +0200
committerkdx <kikoodx@paranoici.org>2023-07-17 07:23:38 +0200
commit353c17a4445bf352f371e6e3bca8ce4a71976e94 (patch)
treedb4df20ef23615d015461a6a307c21d522106af2
parentc8d7637ccbdad8579829e103779be93bb1abc89e (diff)
downloadfld-353c17a4445bf352f371e6e3bca8ce4a71976e94.tar.gz
get input states
-rw-r--r--src/FLD.hpp34
-rw-r--r--src/joyGet.cpp32
-rw-r--r--src/keyGet.cpp32
-rw-r--r--src/mouseGet.cpp32
4 files changed, 129 insertions, 1 deletions
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 <SDL2/SDL_render.h>
+#include <SDL2/SDL_scancode.h>
+#include <SDL2/SDL_gamecontroller.h>
#include <string>
#include <map>
-#include <optional>
+#include <array>
#include <climits>
#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<InputState, SDL_NUM_SCANCODES>;
+ using MouseStateArray = std::array<InputState, 256>;
+ using JoyStateArray = std::array<InputState, SDL_CONTROLLER_BUTTON_MAX>;
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);
+}