Browse Source

Merge branch 'input' into merge

Dan Elkouby 10 years ago
parent
commit
603b176b39

+ 3 - 0
platform/include/Quirks.h

@@ -3,6 +3,7 @@
 
 #include <memory>
 #include <TINYSTL/string.h>
+#include "platform.h"
 
 namespace WalrusRPG
 {
@@ -14,6 +15,8 @@ namespace WalrusRPG
         // Relative path to absolute path resolving.
         // Exists because at this time Ndless doesn't support relative paths.
         std::unique_ptr<char> solve_absolute_path(const char *path);
+
+        bool get_key(keycode_t key);
     }
 }
 

+ 0 - 75
platform/nspire/Input.cpp

@@ -1,75 +0,0 @@
-#include "Input.h"
-#include <libndls.h>
-
-#define INPUT WalrusRPG::Input
-
-using WalrusRPG::Input::Key;
-using WalrusRPG::Input::KeyState;
-
-struct InputMap
-{
-    Key key;
-    t_key key_code;
-};
-
-static KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
-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},
-
-    {Key::K_UP, KEY_NSPIRE_8},       {Key::K_DOWN, KEY_NSPIRE_5},
-    {Key::K_LEFT, KEY_NSPIRE_4},     {Key::K_RIGHT, KEY_NSPIRE_6},
-
-    {Key::K_START, KEY_NSPIRE_DOC}, {Key::K_SELECT, KEY_NSPIRE_ESC},
-};
-
-KeyState INPUT::key_get_state(Key key)
-{
-        return key_states[key];
-}
-
-void INPUT::key_poll()
-{
-    for (unsigned i = 0; i < K_SIZE; i++)
-    {
-        bool current_key_state = 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;
-}

+ 6 - 0
platform/nspire/Quirks.cpp

@@ -4,6 +4,7 @@
 #include <TINYSTL/string.h>
 #include "Quirks.h"
 #include "Interrupts.h"
+#include "platform.h"
 
 
 using namespace WalrusRPG;
@@ -51,3 +52,8 @@ std::unique_ptr<char> Quirks::solve_absolute_path(const char *path)
     result.get()[strlen(base_path) + strlen(path) + strlen(nspire_suffix)] = '\0';
     return result;
 }
+
+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.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;
-}

+ 6 - 0
platform/sfml/Quirks.cpp

@@ -1,6 +1,7 @@
 #include <cstring>
 #include "Quirks.h"
 #include "utility/misc.h"
+#include "sfwindow.h"
 
 using namespace WalrusRPG;
 using tinystl::string;
@@ -20,3 +21,8 @@ std::unique_ptr<char> Quirks::solve_absolute_path(const char *path)
     strcpy(result.get(), path);
     return result;
 }
+
+bool Quirks::get_key(keycode_t key)
+{
+    return sf::Keyboard::isKeyPressed(key) && window.hasFocus();
+}

+ 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

+ 1 - 1
src/engine/StateMachine.cpp

@@ -4,7 +4,7 @@
 #include "Graphics.h"
 // #include "render/Text.h"
 #include "version.h"
-#include "Input.h"
+#include "input/Input.h"
 
 using namespace WalrusRPG::Graphics;
 using namespace WalrusRPG::States;

+ 92 - 0
src/input/Input.cpp

@@ -0,0 +1,92 @@
+#include "input/Input.h"
+#include "Quirks.h"
+#include "platform.h"
+
+#define INPUT WalrusRPG::Input
+using WalrusRPG::Input::Key;
+using WalrusRPG::Input::KeyState;
+
+struct InputMap
+{
+    Key key;
+    keycode_t key_code;
+};
+
+struct KeyBuffer
+{
+    bool current;
+    bool previous;
+};
+struct KeyBuffer key_states[Key::K_SIZE] = {{false, false}};
+
+// 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},
+
+    {Key::K_UP, KEY_NSPIRE_8},       {Key::K_DOWN, KEY_NSPIRE_5},
+    {Key::K_LEFT, KEY_NSPIRE_4},     {Key::K_RIGHT, KEY_NSPIRE_6},
+
+    {Key::K_START, KEY_NSPIRE_DOC}, {Key::K_SELECT, KEY_NSPIRE_ESC},
+};
+#endif
+
+KeyState INPUT::key_get_state(Key key)
+{
+    // "Just" pressed/released before held/inactive to let these events through
+    if (key_pressed(key))
+        return KS_JUST_PRESSED;
+    if (key_down(key))
+        return KS_PRESSED;
+    if (key_released(key))
+        return KS_JUST_RELEASED;
+    if (key_up(key))
+        return KS_RELEASED;
+
+    // Default case to mute compiler warnings, shouldn't even reach here in practice
+    return KS_RELEASED;
+}
+
+void INPUT::key_poll()
+{
+    for (unsigned i = 0; i < K_SIZE; i++)
+    {
+        key_states[i].previous = key_states[i].current;
+        key_states[i].current = WalrusRPG::Quirks::get_key(key_map[i].key_code);
+    }
+}
+
+bool INPUT::key_pressed(Key key)
+{
+    return !key_states[key].previous && key_states[key].current;
+}
+bool INPUT::key_released(Key key)
+{
+    return key_states[key].previous && !key_states[key].current;
+}
+bool INPUT::key_down(Key key)
+{
+    return key_states[key].current;
+}
+bool INPUT::key_up(Key key)
+{
+    return !key_states[key].current;
+}

+ 0 - 0
platform/include/Input.h → src/input/Input.h


+ 1 - 1
src/render/Camera.cpp

@@ -1,6 +1,6 @@
 #include "Camera.h"
 #include "utility/misc.h"
-#include "Input.h"
+#include "input/Input.h"
 
 #define CAMERA WalrusRPG::Camera