Quellcode durchsuchen

Fix Mouse and Keyboard input handling

Daniele Bartolini vor 12 Jahren
Ursprung
Commit
860a3556c1
4 geänderte Dateien mit 51 neuen und 64 gelöschten Zeilen
  1. 3 0
      engine/Device.cpp
  2. 26 29
      engine/input/Keyboard.h
  3. 20 35
      engine/input/Mouse.h
  4. 2 0
      engine/os/linux/main.cpp

+ 3 - 0
engine/Device.cpp

@@ -422,6 +422,9 @@ void Device::frame()
 		}
 
 		m_renderer->frame();
+
+		m_keyboard->update();
+		m_mouse->update();
 	}
 
 	m_frame_count++;

+ 26 - 29
engine/input/Keyboard.h

@@ -26,13 +26,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <cstring>
 #include "Types.h"
 #include "KeyCode.h"
 #include "Assert.h"
 
-#undef MK_SHIFT
-#undef MK_ALT
-
 namespace crown
 {
 
@@ -51,13 +49,10 @@ struct ModifierButton
 struct Keyboard
 {
 	Keyboard()
-		: m_modifier(0), m_any_pressed(false)
+		: m_modifier(0), m_last_button(KeyboardButton::NONE)
 	{
-		for (uint32_t i = 0; i < KeyboardButton::COUNT; i++)
-		{
-			m_last_state[i] = false;
-			m_current_state[i] = false;
-		}
+		memset(m_last_state, 0, KeyboardButton::COUNT);
+		memset(m_current_state, 0, KeyboardButton::COUNT);
 	}
 
 	/// Returns whether the specified @a modifier is pressed.
@@ -66,53 +61,55 @@ struct Keyboard
 	/// of another key when the two are pressed in combination. (Thanks wikipedia.)
 	/// @note
 	/// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
-	bool modifier_pressed(ModifierButton::Enum modifier)
+	bool modifier_pressed(ModifierButton::Enum modifier) const
 	{
 		return (m_modifier & (uint8_t) modifier) == modifier;
 	}
 
-	/// Returns whether the specified @a key is pressed in the current frame.
-	bool button_pressed(KeyboardButton::Enum b)
+	/// Returns whether the specified @a b button is pressed in the current frame.
+	bool button_pressed(KeyboardButton::Enum b) const
 	{
-		bool pressed = (m_current_state[b] == true && m_last_state[b] == false);
-
-		return pressed;
+		return bool(~m_last_state[b] & m_current_state[b]);
 	}
 
-	/// Returns whether the specified @a key is released in the current frame.
-	bool button_released(KeyboardButton::Enum b)
+	/// Returns whether the specified @a b button is released in the current frame.
+	bool button_released(KeyboardButton::Enum b) const
 	{
-		bool released = (m_current_state[b] == false && m_last_state[b] == true);
-
-		return released;
+		return bool(m_last_state[b] & ~m_current_state[b]);
 	}
 
-	/// Returns wheter any key is pressed in the current frame.
+	/// Returns wheter any button is pressed in the current frame.
 	bool any_pressed()
 	{
-		return m_any_pressed;
+		return button_pressed(m_last_button);
 	}
 
-	/// Returns whether any key is released in the current frame.
+	/// Returns whether any button is released in the current frame.
 	bool any_released()
 	{
-		return false;
+		return button_released(m_last_button);
 	}
 
+	//-------------------------------------------------------------------------
 	void set_button_state(KeyboardButton::Enum b, bool state)
 	{
-		m_last_state[b] = m_current_state[b];
+		m_last_button = b;
 		m_current_state[b] = state;
 	}
 
+	//-------------------------------------------------------------------------
+	void update()
+	{
+		memcpy(m_last_state, m_current_state, KeyboardButton::COUNT);
+	}
+
 public:
 
 	uint8_t m_modifier;
 
-	// Last key updated
-	bool m_any_pressed;
-	bool m_last_state[KeyboardButton::COUNT];
-	bool m_current_state[KeyboardButton::COUNT];
+	KeyboardButton::Enum m_last_button;
+	uint8_t m_last_state[KeyboardButton::COUNT];
+	uint8_t m_current_state[KeyboardButton::COUNT];
 };
 
 } // namespace crown

+ 20 - 35
engine/input/Mouse.h

@@ -25,12 +25,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #pragma once
-#undef MB_RIGHT
 
+#include <cstring>
 #include "Types.h"
 #include "Vec2.h"
-#include "Device.h"
-#include "OsWindow.h"
 
 namespace crown
 {
@@ -40,6 +38,7 @@ struct MouseButton
 {
 	enum Enum
 	{
+		NONE,
 		LEFT,
 		MIDDLE,
 		RIGHT,
@@ -54,40 +53,22 @@ public:
 
 	//-----------------------------------------------------------------------------
 	Mouse()
-		: m_last_button(MouseButton::COUNT)
+		: m_last_button(MouseButton::NONE)
 	{
-		m_last_state[MouseButton::LEFT] = false;
-		m_last_state[MouseButton::MIDDLE] = false;
-		m_last_state[MouseButton::RIGHT] = false;
-		m_current_state[MouseButton::LEFT] = false;
-		m_current_state[MouseButton::MIDDLE] = false;
-		m_current_state[MouseButton::RIGHT] = false;
+		memset(m_last_state, 0, MouseButton::COUNT);
+		memset(m_current_state, 0, MouseButton::COUNT);
 	}
 
-	/// Returns whether @a button is pressed in the current frame.
-	bool button_pressed(MouseButton::Enum button)
+	/// Returns whether the @a b button is pressed in the current frame.
+	bool button_pressed(MouseButton::Enum b)
 	{
-		bool pressed = (m_current_state[button] == true && m_last_state[button] == false);
-
-		if (pressed)
-		{
-			m_last_state[button] = m_current_state[button];
-		}
-
-		return pressed;
+		return bool(~m_last_state[b] & m_current_state[b]);
 	}
 
-	/// Returns whether @a button is released in the current frame.
-	bool button_released(MouseButton::Enum button)
+	/// Returns whether the @a b button is released in the current frame.
+	bool button_released(MouseButton::Enum b)
 	{
-		bool released = (m_current_state[button] == false && m_last_state[button] == true);
-
-		if (released)
-		{
-			m_last_state[button] = m_current_state[button];
-		}
-
-		return released;
+		return bool(m_last_state[b] & ~m_current_state[b]);
 	}
 
 	/// Returns wheter any button is pressed in the current frame.
@@ -161,17 +142,21 @@ public:
 	//-----------------------------------------------------------------------------
 	void set_button_state(MouseButton::Enum b, bool state)
 	{
-		m_last_state[b] = m_current_state[b];
-		m_current_state[b] = state;
 		m_last_button = b;
+		m_current_state[b] = state;
+	}
+
+	//-----------------------------------------------------------------------------
+	void update()
+	{
+		memcpy(m_last_state, m_current_state, MouseButton::COUNT);
 	}
 
 public:
 
-	// Last button updated
 	MouseButton::Enum m_last_button;
-	bool m_last_state[MouseButton::COUNT];
-	bool m_current_state[MouseButton::COUNT];
+	uint8_t m_last_state[MouseButton::COUNT];
+	uint8_t m_current_state[MouseButton::COUNT];
 
 	// Position within the window
 	uint16_t m_x;

+ 2 - 0
engine/os/linux/main.cpp

@@ -436,6 +436,8 @@ int main(int argc, char** argv)
 	
 	int32_t ret = engine->run(argc, argv);
 
+	CE_DELETE(crown::default_allocator(), engine);
+
 	crown::shutdown();
 	return ret;
 }