Bladeren bron

Merge input into one source file

Dan Elkouby 10 jaren geleden
bovenliggende
commit
114c2ad1c6

+ 3 - 0
platform/include/Quirks.h

@@ -1,12 +1,15 @@
 #ifndef INCLUDE_QUIRKS_H
 #define INCLUDE_QUIRKS_H
 
+#include "platform.h"
+
 namespace WalrusRPG
 {
     namespace Quirks
     {
         void init();
         void deinit();
+        bool get_key(keycode_t key);
     }
 }
 

+ 6 - 0
platform/nspire/Quirks.cpp

@@ -1,5 +1,6 @@
 #include "Quirks.h"
 #include "Interrupts.h"
+#include "platform.h"
 
 using namespace WalrusRPG;
 using namespace Nspire;
@@ -13,3 +14,8 @@ void Quirks::deinit()
 {
     Interrupts::off();
 }
+
+bool Quirks::get_key(keycode_t key)
+{
+    return isKeyPressed(key);
+}

+ 2 - 0
platform/nspire/public/platform.h

@@ -2,10 +2,12 @@
 #define INCLUDE_PLATFORM_H
 
 #include <cstdint>
+#include <libndls.h>
 
 #define TIMER_FREQ 32768
 #define ACTIVE_WAIT
 
 typedef uint16_t *texture_data_t;
+typedef t_key keycode_t;
 
 #endif

+ 0 - 85
platform/sfml/Input.cpp

@@ -1,85 +0,0 @@
-#include "input/Input.h"
-#include "Graphics.h" // window
-#include "sfwindow.h"
-#include <SFML/Window/Keyboard.hpp>
-#include <SFML/Window.hpp>
-
-#define INPUT WalrusRPG::Input
-using WalrusRPG::Input::Key;
-using WalrusRPG::Input::KeyState;
-using sf::Keyboard;
-
-struct InputMap
-{
-    Key key;
-    sf::Keyboard::Key key_code;
-    sf::Keyboard::Key key_code_alt;
-};
-
-KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
-InputMap key_map[] = {
-    {Key::K_A, Keyboard::W, Keyboard::Z},
-    {Key::K_B, Keyboard::X, Keyboard::Unknown},
-    {Key::K_L, Keyboard::Q, Keyboard::A},
-    {Key::K_R, Keyboard::S, Keyboard::Unknown},
-
-    {Key::K_UP, Keyboard::Up, Keyboard::Unknown},
-    {Key::K_DOWN, Keyboard::Down, Keyboard::Unknown},
-    {Key::K_LEFT, Keyboard::Left, Keyboard::Unknown},
-    {Key::K_RIGHT, Keyboard::Right, Keyboard::Unknown},
-
-    {Key::K_START, Keyboard::Return, Keyboard::Unknown},
-    {Key::K_SELECT, Keyboard::BackSpace, Keyboard::Unknown},
-};
-
-KeyState INPUT::key_get_state(Key key)
-{
-        return key_states[key];
-}
-
-void INPUT::key_poll()
-{
-    bool hasFocus = window.hasFocus();
-    for (unsigned i = 0; i < K_SIZE; i++)
-    {
-        bool current_key_state = hasFocus && Keyboard::isKeyPressed(key_map[i].key_code);
-        KeyState previous_key_state = key_states[i];
-
-        KeyState resulting_key_state = KS_RELEASED;
-        if (current_key_state)
-        {
-            if (previous_key_state == KS_RELEASED ||
-                previous_key_state == KS_JUST_RELEASED)
-                resulting_key_state = KS_JUST_PRESSED;
-            else if (previous_key_state == KS_JUST_PRESSED ||
-                     previous_key_state == KS_PRESSED)
-                resulting_key_state = KS_PRESSED;
-        }
-        else
-        {
-            if (previous_key_state == KS_PRESSED || previous_key_state == KS_JUST_PRESSED)
-                resulting_key_state = KS_JUST_RELEASED;
-            else if (previous_key_state == KS_JUST_RELEASED ||
-                     previous_key_state == KS_RELEASED)
-                resulting_key_state = KS_RELEASED;
-        }
-        key_states[i] = resulting_key_state;
-    }
-}
-
-bool INPUT::key_pressed(Key key)
-{
-    return key_states[key] == KS_JUST_PRESSED;
-}
-bool INPUT::key_released(Key key)
-{
-    return key_states[key] == KS_JUST_RELEASED;
-}
-bool INPUT::key_down(Key key)
-{
-    return key_states[key] == KS_JUST_PRESSED || key_states[key] == KS_PRESSED;
-}
-bool INPUT::key_up(Key key)
-{
-    return key_states[key] == KS_JUST_RELEASED || key_states[key] == KS_RELEASED;
-}

+ 5 - 0
platform/sfml/Quirks.cpp

@@ -9,3 +9,8 @@ void Quirks::init()
 void Quirks::deinit()
 {
 }
+
+bool Quirks::get_key(keycode_t key)
+{
+    return sf::Keyboard::isKeyPressed(key);
+}

+ 2 - 0
platform/sfml/public/platform.h

@@ -2,9 +2,11 @@
 #define INCLUDE_PLATFORM_H
 
 #include <SFML/Graphics/Texture.hpp>
+#include <SFML/Window/Keyboard.hpp>
 
 #define TIMER_FREQ 1000000
 
 typedef sf::Texture texture_data_t;
+typedef sf::Keyboard::Key keycode_t;
 
 #endif

+ 26 - 5
platform/nspire/Input.cpp → src/input/Input.cpp

@@ -1,18 +1,38 @@
 #include "input/Input.h"
-#include <libndls.h>
+#include "Quirks.h"
+#include "platform.h"
 
 #define INPUT WalrusRPG::Input
-
 using WalrusRPG::Input::Key;
 using WalrusRPG::Input::KeyState;
 
 struct InputMap
 {
     Key key;
-    t_key key_code;
+    keycode_t key_code;
 };
 
-static KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
+KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
+
+// TODO: make these software-mappable
+#ifdef SFML
+using sf::Keyboard;
+InputMap key_map[] = {
+    {Key::K_A, Keyboard::W},
+    {Key::K_B, Keyboard::X},
+    {Key::K_L, Keyboard::Q},
+    {Key::K_R, Keyboard::S},
+
+    {Key::K_UP, Keyboard::Up},
+    {Key::K_DOWN, Keyboard::Down},
+    {Key::K_LEFT, Keyboard::Left},
+    {Key::K_RIGHT, Keyboard::Right},
+
+    {Key::K_START, Keyboard::Return},
+    {Key::K_SELECT, Keyboard::BackSpace},
+};
+#endif
+#ifdef NSPIRE
 static InputMap key_map[] = {
     {Key::K_A, KEY_NSPIRE_CTRL},     {Key::K_B, KEY_NSPIRE_SHIFT},
     {Key::K_L, KEY_NSPIRE_TAB},      {Key::K_R, KEY_NSPIRE_MENU},
@@ -22,6 +42,7 @@ static InputMap key_map[] = {
 
     {Key::K_START, KEY_NSPIRE_DOC}, {Key::K_SELECT, KEY_NSPIRE_ESC},
 };
+#endif
 
 KeyState INPUT::key_get_state(Key key)
 {
@@ -32,7 +53,7 @@ void INPUT::key_poll()
 {
     for (unsigned i = 0; i < K_SIZE; i++)
     {
-        bool current_key_state = isKeyPressed(key_map[i].key_code);
+        bool current_key_state = WalrusRPG::Quirks::get_key(key_map[i].key_code);
         KeyState previous_key_state = key_states[i];
 
         KeyState resulting_key_state = KS_RELEASED;

+ 42 - 0
src/input/Input.h

@@ -0,0 +1,42 @@
+#ifndef INCLUDE_INPUT_H
+#define INCLUDE_INPUT_H
+
+namespace WalrusRPG
+{
+    namespace Input
+    {
+        enum Key
+        {
+            K_A,
+            K_B,
+            K_L,
+            K_R,
+            K_UP,
+            K_DOWN,
+            K_LEFT,
+            K_RIGHT,
+            K_START,
+            K_SELECT,
+            K_SIZE
+        };
+
+        enum KeyState
+        {
+            KS_RELEASED,
+            KS_JUST_RELEASED,
+            KS_JUST_PRESSED,
+            KS_PRESSED
+        };
+
+
+        void key_poll();
+
+        KeyState key_get_state(Key key);
+        bool key_pressed(Key key);
+        bool key_released(Key key);
+        bool key_down(Key key);
+        bool key_up(Key key);
+    }
+}
+
+#endif