Explorar el Código

Move global input-related functions to input_globals

Daniele Bartolini hace 11 años
padre
commit
be48e38022

+ 70 - 0
engine/input/input.cpp

@@ -0,0 +1,70 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "input.h"
+#include "keyboard.h"
+#include "mouse.h"
+#include "touch.h"
+#include "memory.h"
+
+namespace crown
+{
+namespace input_globals
+{
+	Keyboard* _keyboard = NULL;
+	Mouse* _mouse = NULL;
+	Touch* _touch = NULL;
+
+	void init()
+	{
+		_keyboard = CE_NEW(default_allocator(), Keyboard);
+		_mouse = CE_NEW(default_allocator(), Mouse);
+		_touch = CE_NEW(default_allocator(), Touch);
+	}
+
+	void shutdown()
+	{
+		CE_DELETE(default_allocator(), _keyboard);
+		CE_DELETE(default_allocator(), _mouse);
+		CE_DELETE(default_allocator(), _touch);
+	}
+
+	Keyboard& keyboard()
+	{
+		return *_keyboard;
+	}
+
+	Mouse& mouse()
+	{
+		return *_mouse;
+	}
+
+	Touch& touch()
+	{
+		return *_touch;
+	}
+} // namespace input_globals
+} // namespace crown

+ 55 - 0
engine/input/input.h

@@ -0,0 +1,55 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "input_types.h"
+
+namespace crown
+{
+/// @defgroup Input Input
+
+/// Global input-related functions.
+///
+/// @ingroup Input
+namespace input_globals
+{
+	/// Initializes the input system.
+	void init();
+
+	/// Shutdowns the input system.
+	void shutdown();
+
+	/// Returns the default keyboard.
+	Keyboard& keyboard();
+
+	/// Returns the default mouse.
+	Mouse& mouse();
+
+	/// Rettuns the default touch panel.
+	Touch& touch();
+} // namespace input_globals
+} // namespace crown

+ 34 - 0
engine/input/input_types.h

@@ -0,0 +1,34 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+namespace crown
+{
+	struct Keyboard;
+	struct Mouse;
+	struct Touch;
+} // namespace crown

+ 1 - 5
engine/input/keyboard.h

@@ -26,16 +26,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include <cstring>
 #include "types.h"
 #include "key_code.h"
-#include "assert.h"
+#include <cstring> // mem*
 
 namespace crown
 {
-
-/// @defgroup Input Input
-
 /// Enumerates modifier keys.
 ///
 /// @ingroup Input

+ 8 - 22
engine/lua/lua_keyboard.cpp

@@ -25,22 +25,20 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "lua_stack.h"
-#include "device.h"
 #include "lua_environment.h"
+#include "input.h"
 #include "keyboard.h"
 
+
 namespace crown
 {
+using namespace input_globals;
 
 //-----------------------------------------------------------------------------
 static int keyboard_modifier_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t modifier = stack.get_int(1);
-
-	stack.push_bool(device()->keyboard()->modifier_pressed((ModifierButton::Enum) modifier));
-
+	stack.push_bool(keyboard().modifier_pressed((ModifierButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
@@ -48,11 +46,7 @@ static int keyboard_modifier_pressed(lua_State* L)
 static int keyboard_button_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t button = stack.get_int(1);
-
-	stack.push_bool(device()->keyboard()->button_pressed((KeyboardButton::Enum) button));
-
+	stack.push_bool(keyboard().button_pressed((KeyboardButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
@@ -60,11 +54,7 @@ static int keyboard_button_pressed(lua_State* L)
 static int keyboard_button_released(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t button = stack.get_int(1);
-
-	stack.push_bool(device()->keyboard()->button_released((KeyboardButton::Enum) button));
-
+	stack.push_bool(keyboard().button_released((KeyboardButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
@@ -72,9 +62,7 @@ static int keyboard_button_released(lua_State* L)
 static int keyboard_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->keyboard()->any_pressed());
-
+	stack.push_bool(keyboard().any_pressed());
 	return 1;
 }
 
@@ -82,9 +70,7 @@ static int keyboard_any_pressed(lua_State* L)
 static int keyboard_any_released(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->keyboard()->any_released());
-
+	stack.push_bool(keyboard().any_released());
 	return 1;
 }
 

+ 10 - 34
engine/lua/lua_mouse.cpp

@@ -25,23 +25,19 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "lua_stack.h"
-#include "device.h"
 #include "lua_environment.h"
+#include "input.h"
 #include "mouse.h"
-#include "log.h"
 
 namespace crown
 {
+using namespace input_globals;
 
 //-----------------------------------------------------------------------------
 static int mouse_button_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t button = stack.get_int(1);
-
-	stack.push_bool(device()->mouse()->button_pressed((MouseButton::Enum) button));
-
+	stack.push_bool(mouse().button_pressed((MouseButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
@@ -49,11 +45,7 @@ static int mouse_button_pressed(lua_State* L)
 static int mouse_button_released(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t button = stack.get_int(1);
-
-	stack.push_bool(device()->mouse()->button_released((MouseButton::Enum) button));
-
+	stack.push_bool(mouse().button_released((MouseButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
@@ -61,9 +53,7 @@ static int mouse_button_released(lua_State* L)
 static int mouse_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->mouse()->any_pressed());
-
+	stack.push_bool(mouse().any_pressed());
 	return 1;
 }
 
@@ -71,9 +61,7 @@ static int mouse_any_pressed(lua_State* L)
 static int mouse_any_released(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->mouse()->any_released());
-
+	stack.push_bool(mouse().any_released());
 	return 1;
 }
 
@@ -81,9 +69,7 @@ static int mouse_any_released(lua_State* L)
 static int mouse_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_vector2(device()->mouse()->cursor_xy());
-
+	stack.push_vector2(mouse().cursor_xy());
 	return 1;
 }
 
@@ -91,11 +77,7 @@ static int mouse_cursor_xy(lua_State* L)
 static int mouse_set_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
-
-	Vector2& xy = stack.get_vector2(1);
-
-	device()->mouse()->set_cursor_xy(xy);
-
+	mouse().set_cursor_xy(stack.get_vector2(1));
 	return 0;
 }
 
@@ -103,9 +85,7 @@ static int mouse_set_cursor_xy(lua_State* L)
 static int mouse_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_vector2(device()->mouse()->cursor_relative_xy());
-
+	stack.push_vector2(mouse().cursor_relative_xy());
 	return 1;
 }
 
@@ -113,11 +93,7 @@ static int mouse_cursor_relative_xy(lua_State* L)
 static int mouse_set_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
-
-	Vector2& xy = stack.get_vector2(1);
-
-	device()->mouse()->set_cursor_relative_xy(xy);
-
+	mouse().set_cursor_relative_xy(stack.get_vector2(1));
 	return 0;
 }
 

+ 7 - 22
engine/lua/lua_touch.cpp

@@ -25,22 +25,19 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "lua_stack.h"
-#include "device.h"
 #include "lua_environment.h"
+#include "input.h"
 #include "touch.h"
 
 namespace crown
 {
+using namespace input_globals;
 
 //-----------------------------------------------------------------------------
 static int touch_pointer_down(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t pointer = stack.get_int(1);
-
-	stack.push_bool(device()->touch()->pointer_down((uint8_t) pointer));
-
+	stack.push_bool(touch().pointer_down((uint8_t) stack.get_int(1)));
 	return 1;
 }
 
@@ -48,11 +45,7 @@ static int touch_pointer_down(lua_State* L)
 static int touch_pointer_up(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t pointer = stack.get_int(1);
-
-	stack.push_bool(device()->touch()->pointer_up((uint8_t) pointer));
-
+	stack.push_bool(touch().pointer_up((uint8_t) stack.get_int(1)));
 	return 1;
 }
 
@@ -60,9 +53,7 @@ static int touch_pointer_up(lua_State* L)
 static int touch_any_down(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->touch()->any_down());
-
+	stack.push_bool(touch().any_down());
 	return 1;
 }
 
@@ -70,9 +61,7 @@ static int touch_any_down(lua_State* L)
 static int touch_any_up(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_bool(device()->touch()->any_up());
-
+	stack.push_bool(touch().any_up());
 	return 1;
 }
 
@@ -80,11 +69,7 @@ static int touch_any_up(lua_State* L)
 static int touch_pointer_xy(lua_State* L)
 {
 	LuaStack stack(L);
-
-	int32_t pointer = stack.get_int(1);
-
-	stack.push_vector2(device()->touch()->pointer_xy((uint8_t) pointer));
-
+	stack.push_vector2(touch().pointer_xy((uint8_t) stack.get_int(1)));
 	return 1;
 }