Daniele Bartolini 10 лет назад
Родитель
Сommit
5e2cee969e
3 измененных файлов с 69 добавлено и 38 удалено
  1. 35 1
      src/input/input_device.cpp
  2. 8 0
      src/input/input_device.h
  3. 26 37
      src/input/input_manager.cpp

+ 35 - 1
src/input/input_device.cpp

@@ -5,7 +5,8 @@
 
 #include "input_device.h"
 #include "error.h"
-#include <string.h> // memcpy
+#include "allocator.h"
+#include <string.h> // strlen, strcpy, memset
 
 namespace crown
 {
@@ -81,4 +82,37 @@ void InputDevice::update()
 	memcpy(_last_state, _current_state, sizeof(uint8_t)*_num_buttons);
 }
 
+InputDevice* InputDevice::create_input_device(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes)
+{
+	const uint32_t size = 0
+		+ sizeof(InputDevice)
+		+ sizeof(uint8_t)*num_buttons*2
+		+ sizeof(Vector3)*num_axes
+		+ strlen(name) + 1;
+
+	InputDevice* id = (InputDevice*)a.allocate(size);
+
+	id->_connected = false;
+	id->_num_buttons = num_buttons;
+	id->_num_axes = num_axes;
+	id->_last_button = 0;
+
+	id->_last_state = (uint8_t*)&id[1];
+	id->_current_state = (uint8_t*)(id->_last_state + num_buttons);
+	id->_axis = (Vector3*)(id->_current_state + num_buttons);
+	id->_name = (char*)(id->_axis + num_axes);
+
+	memset(id->_last_state, 0, sizeof(uint8_t)*num_buttons);
+	memset(id->_current_state, 0, sizeof(uint8_t)*num_buttons);
+	memset(id->_axis, 0, sizeof(Vector3)*num_axes);
+	strcpy(id->_name, name);
+
+	return id;
+}
+
+void InputDevice::destroy_input_device(Allocator& a, InputDevice* id)
+{
+	a.deallocate(id);
+}
+
 } // namespace crown

+ 8 - 0
src/input/input_device.h

@@ -7,6 +7,7 @@
 
 #include "types.h"
 #include "math_types.h"
+#include "memory_types.h"
 
 namespace crown
 {
@@ -51,6 +52,8 @@ struct InputDevice
 
 	void update();
 
+public:
+
 	bool _connected;
 	uint8_t _num_buttons;
 	uint8_t _num_axes;
@@ -60,6 +63,11 @@ struct InputDevice
 	uint8_t* _current_state; // num_buttons
 	Vector3* _axis;          // num_axes
 	char* _name;             // strlen(name) + 1
+
+public:
+
+	static InputDevice* create_input_device(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes);
+	static void destroy_input_device(Allocator& a, InputDevice* id);
 };
 
 } // namespace crown

+ 26 - 37
src/input/input_manager.cpp

@@ -7,7 +7,6 @@
 #include "input_device.h"
 #include "memory.h"
 #include "vector3.h"
-#include <string.h> // strlen, strcpy, memset
 
 namespace crown
 {
@@ -17,12 +16,30 @@ InputManager::InputManager()
 	, _mouse(NULL)
 	, _touch(NULL)
 {
-	_keyboard = create_input_device("Keyboard", KeyboardButton::COUNT, 0);
-	_mouse = create_input_device("Mouse", MouseButton::COUNT, MouseAxis::COUNT);
-	_touch = create_input_device("Touch", TouchButton::COUNT, TouchButton::COUNT);
+	_keyboard = InputDevice::create_input_device(default_allocator()
+		, "Keyboard"
+		, KeyboardButton::COUNT
+		, 0
+		);
+	_mouse = InputDevice::create_input_device(default_allocator()
+		, "Mouse"
+		, MouseButton::COUNT
+		, MouseAxis::COUNT
+		);
+	_touch = InputDevice::create_input_device(default_allocator()
+		, "Touch"
+		, TouchButton::COUNT
+		, TouchButton::COUNT
+		);
 
 	for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
-		_joypad[i] = create_input_device("Joypad", JoypadButton::COUNT, JoypadAxis::COUNT);
+	{
+		_joypad[i] = InputDevice::create_input_device(default_allocator()
+			, "Joypad"
+			, JoypadButton::COUNT
+			, JoypadAxis::COUNT
+			);
+	}
 
 	_keyboard->set_connected(true);
 	_mouse->set_connected(true);
@@ -32,39 +49,11 @@ InputManager::InputManager()
 InputManager::~InputManager()
 {
 	for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
-		default_allocator().deallocate(_joypad[i]);
+		InputDevice::destroy_input_device(default_allocator(), _joypad[i]);
 
-	default_allocator().deallocate(_touch);
-	default_allocator().deallocate(_mouse);
-	default_allocator().deallocate(_keyboard);
-}
-
-InputDevice* InputManager::create_input_device(const char* name, uint8_t num_buttons, uint8_t num_axes)
-{
-	const uint32_t size = 0
-		+ sizeof(InputDevice)
-		+ sizeof(uint8_t)*num_buttons*2
-		+ sizeof(Vector3)*num_axes
-		+ strlen(name) + 1;
-
-	InputDevice* id = (InputDevice*)default_allocator().allocate(size);
-
-	id->_connected = false;
-	id->_num_buttons = num_buttons;
-	id->_num_axes = num_axes;
-	id->_last_button = 0;
-
-	id->_last_state = (uint8_t*)&id[1];
-	id->_current_state = (uint8_t*)(id->_last_state + num_buttons);
-	id->_axis = (Vector3*)(id->_current_state + num_buttons);
-	id->_name = (char*)(id->_axis + num_axes);
-
-	memset(id->_last_state, 0, sizeof(uint8_t)*num_buttons);
-	memset(id->_current_state, 0, sizeof(uint8_t)*num_buttons);
-	memset(id->_axis, 0, sizeof(Vector3)*num_axes);
-	strcpy(id->_name, name);
-
-	return id;
+	InputDevice::destroy_input_device(default_allocator(), _touch);
+	InputDevice::destroy_input_device(default_allocator(), _mouse);
+	InputDevice::destroy_input_device(default_allocator(), _keyboard);
 }
 
 InputDevice* InputManager::keyboard()