Просмотр исходного кода

Update CURSOR_DELTA once per frame

Daniele Bartolini 8 лет назад
Родитель
Сommit
adca5f7e12
4 измененных файлов с 14 добавлено и 19 удалено
  1. 2 18
      src/device/device.cpp
  2. 1 1
      src/device/device.h
  3. 9 0
      src/device/input_manager.cpp
  4. 2 0
      src/device/input_manager.h

+ 2 - 18
src/device/device.cpp

@@ -214,7 +214,7 @@ Device::Device(const DeviceOptions& opts, ConsoleServer& cs)
 {
 }
 
-bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y, bool vsync)
+bool Device::process_events(bool vsync)
 {
 	InputManager* im = _input_manager;
 	bool exit = false;
@@ -259,11 +259,6 @@ bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16&
 				{
 				case InputDeviceType::MOUSE:
 					im->mouse()->set_axis(ev.axis_num, vector3(ev.axis_x, ev.axis_y, ev.axis_z));
-					if (ev.axis_num == MouseAxis::CURSOR)
-					{
-						mouse_x = (s16)ev.axis_x;
-						mouse_y = (s16)ev.axis_y;
-					}
 					break;
 
 				case InputDeviceType::JOYPAD:
@@ -312,12 +307,6 @@ bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16&
 		}
 	}
 
-	const s16 dt_x = mouse_x - mouse_last_x;
-	const s16 dt_y = mouse_y - mouse_last_y;
-	im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
-	mouse_last_x = mouse_x;
-	mouse_last_y = mouse_y;
-
 	if (reset)
 		bgfx::reset(_width, _height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
 
@@ -448,15 +437,10 @@ void Device::run()
 
 	logi(DEVICE, "Initialized");
 
-	s16 mouse_x = 0;
-	s16 mouse_y = 0;
-	s16 mouse_last_x = 0;
-	s16 mouse_last_y = 0;
-
 	s64 last_time = os::clocktime();
 	s64 curr_time;
 
-	while (!process_events(mouse_x, mouse_y, mouse_last_x, mouse_last_y, _boot_config.vsync) && !_quit)
+	while (!process_events(_boot_config.vsync) && !_quit)
 	{
 		curr_time = os::clocktime();
 		const s64 time = curr_time - last_time;

+ 1 - 1
src/device/device.h

@@ -65,7 +65,7 @@ struct Device
 	f32 _last_delta_time;
 	f64 _time_since_start;
 
-	bool process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y, bool vsync);
+	bool process_events(bool vsync);
 
 public:
 

+ 9 - 0
src/device/input_manager.cpp

@@ -6,6 +6,7 @@
 #include "input_device.h"
 #include "input_manager.h"
 #include "memory.h"
+#include "vector3.h"
 
 namespace crown
 {
@@ -171,6 +172,8 @@ InputManager::InputManager(Allocator& a)
 	, _keyboard(NULL)
 	, _mouse(NULL)
 	, _touch(NULL)
+	, _mouse_last_x(0)
+	, _mouse_last_y(0)
 {
 	_keyboard = input_device::create(*_allocator
 		, "Keyboard"
@@ -249,7 +252,13 @@ InputDevice* InputManager::joypad(u8 i)
 void InputManager::update()
 {
 	_keyboard->update();
+
+	const Vector3 cursor = _mouse->axis(MouseAxis::CURSOR);
+	_mouse->set_axis(MouseAxis::CURSOR_DELTA, vector3((s16)cursor.x - _mouse_last_x, (s16)cursor.y - _mouse_last_y, 0.0f));
+	_mouse_last_x = (s16)cursor.x;
+	_mouse_last_y = (s16)cursor.y;
 	_mouse->update();
+
 	_touch->update();
 
 	for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)

+ 2 - 0
src/device/input_manager.h

@@ -22,6 +22,8 @@ struct InputManager
 	InputDevice* _mouse;
 	InputDevice* _touch;
 	InputDevice* _joypad[CROWN_MAX_JOYPADS];
+	s16 _mouse_last_x;
+	s16 _mouse_last_y;
 
 	/// Constructor.
 	InputManager(Allocator& a);