Przeglądaj źródła

Using key polling. SHould be cleaned.

Eiyeron Fulmincendii 10 lat temu
rodzic
commit
c9fec40076

+ 30 - 10
platform/include/Input.h

@@ -5,16 +5,36 @@ namespace WalrusRPG
 {
     namespace Input
     {
-        bool key_a();
-        bool key_b();
-        bool key_l();
-        bool key_r();
-        bool key_up();
-        bool key_down();
-        bool key_left();
-        bool key_right();
-        bool key_start();
-        bool key_select();
+        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();
+
+        bool key_pressed(Key key);
+        bool key_released(Key key);
+        bool key_down(Key key);
+        bool key_up(Key key);
     }
 }
 

+ 52 - 32
platform/nspire/Input.cpp

@@ -3,52 +3,72 @@
 
 #define INPUT WalrusRPG::Input
 
-bool INPUT::key_a()
-{
-    return isKeyPressed(KEY_NSPIRE_CTRL);
-}
+using WalrusRPG::Input::Key;
+using WalrusRPG::Input::KeyState;
 
-bool INPUT::key_b()
+struct InputMap
 {
-    return isKeyPressed(KEY_NSPIRE_SHIFT);
-}
+    Key key;
+    t_key key_code;
+};
 
-bool INPUT::key_l()
-{
-    return isKeyPressed(KEY_NSPIRE_TAB);
-}
+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},
 
-bool INPUT::key_r()
-{
-    return isKeyPressed(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},
 
-bool INPUT::key_up()
-{
-    return isKeyPressed(KEY_NSPIRE_8);
-}
+    {Key::K_START, KEY_NSPIRE_HOME},
+    {Key::K_SELECT, KEY_NSPIRE_ESC},
+};
 
-bool INPUT::key_down()
+void INPUT::key_poll()
 {
-    return isKeyPressed(KEY_NSPIRE_5);
-}
+    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];
 
-bool INPUT::key_left()
-{
-    return isKeyPressed(KEY_NSPIRE_4);
+        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_right()
+bool INPUT::key_pressed(Key key)
 {
-    return isKeyPressed(KEY_NSPIRE_6);
+    return key_states[key] == KS_JUST_PRESSED;
 }
+bool INPUT::key_released(Key key)
+{
+    return key_states[key] == KS_JUST_RELEASED;
 
-bool INPUT::key_start()
+}
+bool INPUT::key_down(Key key)
 {
-    return isKeyPressed(KEY_NSPIRE_HOME);
+    return key_states[key] == KS_JUST_PRESSED || key_states[key] == KS_PRESSED;
 }
-
-bool INPUT::key_select()
+bool INPUT::key_up(Key key)
 {
-    return isKeyPressed(KEY_NSPIRE_ESC);
+    return key_states[key] == KS_JUST_RELEASED || key_states[key] == KS_RELEASED;
+
 }

+ 70 - 35
platform/sfml/Input.cpp

@@ -1,58 +1,93 @@
 #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;
 
-bool INPUT::key_a()
+struct InputMap
 {
-    return window.hasFocus() &&
-           (Keyboard::isKeyPressed(Keyboard::W) || Keyboard::isKeyPressed(Keyboard::Z));
-}
+    Key key;
+    sf::Keyboard::Key key_code;
+};
 
-bool INPUT::key_b()
-{
-    return window.hasFocus() && Keyboard::isKeyPressed(Keyboard::X);
-}
+KeyState key_states[Key::K_SIZE] = {KeyState::KS_RELEASED};
+InputMap key_map[] = {
+    {Key::K_A, Keyboard::X},
+    {Key::K_B, Keyboard::C},
+    {Key::K_L, Keyboard::D},
+    {Key::K_R, Keyboard::F},
 
-bool INPUT::key_l()
-{
-    return window.hasFocus() &&
-           (Keyboard::isKeyPressed(Keyboard::Q) || Keyboard::isKeyPressed(Keyboard::A));
-}
+    {Key::K_UP, Keyboard::Z},
+    {Key::K_DOWN, Keyboard::S},
+    {Key::K_LEFT, Keyboard::Q},
+    {Key::K_RIGHT, Keyboard::D},
 
-bool INPUT::key_r()
-{
-    return window.hasFocus() && Keyboard::isKeyPressed(Keyboard::S);
-}
+    {Key::K_START, Keyboard::Return},
+    {Key::K_SELECT, Keyboard::BackSpace},
+};
 
-bool INPUT::key_up()
+void INPUT::key_poll()
 {
-    return window.hasFocus() && (Keyboard::isKeyPressed(Keyboard::Up));
-}
+    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];
 
-bool INPUT::key_down()
-{
-    return window.hasFocus() && (Keyboard::isKeyPressed(Keyboard::Down));
-}
+        KeyState resulting_key_state = KS_RELEASED;
+        if(current_key_state)
+        {
+            switch(current_key_state)
+            {
+                case KS_RELEASED:
+                case KS_JUST_RELEASED:
+                    resulting_key_state = KS_JUST_PRESSED;
+                    break;
 
-bool INPUT::key_left()
-{
-    return window.hasFocus() && (Keyboard::isKeyPressed(Keyboard::Left));
-}
+                case KS_JUST_PRESSED:
+                case KS_PRESSED:
+                    resulting_key_state = KS_PRESSED;
+                    break;
+            }
+        }
+        else
+        {
+            switch(current_key_state)
+            {
+                case KS_RELEASED:
+                case KS_JUST_RELEASED:
+                    resulting_key_state = KS_RELEASED;
+                    break;
 
-bool INPUT::key_right()
-{
-    return window.hasFocus() && (Keyboard::isKeyPressed(Keyboard::Right));
+                case KS_JUST_PRESSED:
+                case KS_PRESSED:
+                    resulting_key_state = KS_JUST_RELEASED;
+                    break;
+            }
+        }
+        key_states[i] = resulting_key_state;
+    }
 }
 
-bool INPUT::key_start()
+bool INPUT::key_pressed(Key key)
 {
-    return window.hasFocus() && Keyboard::isKeyPressed(Keyboard::Return);
+    return key_states[key] == KS_JUST_PRESSED;
 }
+bool INPUT::key_released(Key key)
+{
+    return key_states[key] == KS_JUST_RELEASED;
 
-bool INPUT::key_select()
+}
+bool INPUT::key_down(Key key)
 {
-    return window.hasFocus() && Keyboard::isKeyPressed(Keyboard::BackSpace);
+    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 - 3
src/engine/StateMachine.cpp

@@ -9,6 +9,7 @@
 using namespace WalrusRPG::Graphics;
 using namespace WalrusRPG::States;
 using namespace WalrusRPG::Timing;
+using WalrusRPG::Input::Key;
 
 #define STATEMACHINE WalrusRPG::StateMachine
 
@@ -45,6 +46,7 @@ void STATEMACHINE::run()
     {
         update_stamp = Timing::gettime();
         update_time = update_stamp - last_update;
+        Input::key_poll();
         stack.back()->update(100*update_time/TIMER_FREQ);
         last_update = update_stamp;
 
@@ -65,10 +67,10 @@ void STATEMACHINE::run()
             Graphics::frame_end();
         }
 
-        if (Input::key_select())
+        if (Input::key_pressed(Key::K_SELECT))
         {
-            while (Input::key_select())
-                ;
+            while (Input::key_down(Key::K_SELECT))
+                Input::key_poll();
             this->pop();
         }
 

+ 5 - 4
src/render/Camera.cpp

@@ -5,6 +5,7 @@
 #define CAMERA WalrusRPG::Camera
 
 using namespace WalrusRPG;
+using WalrusRPG::Input::Key;
 
 CAMERA::Camera(signed x, signed y)
 {
@@ -30,13 +31,13 @@ void CAMERA::update(unsigned dt)
                 velocity += acceleration * dt;
          */
 
-    if (Input::key_down())
+    if (Input::key_down(Key::K_DOWN))
         y++;
-    if (Input::key_up())
+    if (Input::key_down(Key::K_UP))
         y--;
-    if (Input::key_right())
+    if (Input::key_down(Key::K_RIGHT))
         x++;
-    if (Input::key_left())
+    if (Input::key_down(Key::K_LEFT))
         x--;
 }