From 4d382867d94081f7d0127e93ea70af4515ffb6a8 Mon Sep 17 00:00:00 2001 From: lilacLunatic <8488221+lilacLunatic@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:43:53 -0300 Subject: [PATCH 1/9] mouse raw input test --- src/graphic/Fast3D/gfx_dxgi.cpp | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index f997df40c..61c43a574 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -1,4 +1,4 @@ -#if defined(ENABLE_DX11) || defined(ENABLE_DX12) +#if defined(ENABLE_DX11) || defined(ENABLE_DX12) || true #include #include @@ -93,6 +93,9 @@ static struct { LARGE_INTEGER previous_present_time; bool is_mouse_captured; bool in_focus; + RAWINPUTDEVICE raw_input_device[1]; + POINT mouse_pos; + POINT mouse_delta; void (*on_fullscreen_changed)(bool is_now_fullscreen); bool (*on_key_down)(int scancode); @@ -424,6 +427,19 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par break; case WM_MOUSEWHEEL: dxgi.mouse_wheel[1] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA; + break; + case WM_INPUT: + uint32_t size = sizeof(RAWINPUT); + static RAWINPUT raw[sizeof(RAWINPUT)]; + GetRawInputData((HRAWINPUT)lparam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); + + if (raw->header.dwType == RIM_TYPEMOUSE) { + dxgi.mouse_delta.x += raw->data.mouse.lLastX - dxgi.mouse_pos.x; + dxgi.mouse_delta.y += raw->data.mouse.lLastY - dxgi.mouse_pos.y; + dxgi.mouse_pos.x = raw->data.mouse.lLastX; + dxgi.mouse_pos.y = raw->data.mouse.lLastY; + } + break; case WM_DROPFILES: DragQueryFileA((HDROP)w_param, 0, fileName, 256); @@ -527,6 +543,19 @@ void gfx_dxgi_init(const char* game_name, const char* gfx_api_name, bool start_i } DragAcceptFiles(dxgi.h_wnd, TRUE); + + // Mouse init + #ifndef HID_USAGE_PAGE_GENERIC + #define HID_USAGE_PAGE_GENERIC ((unsigned short) 0x01) + #endif + #ifndef HID_USAGE_GENERIC_MOUSE + #define HID_USAGE_GENERIC_MOUSE ((unsigned short) 0x02) + #endif + + dxgi.raw_input_device[0].usUsagePage = HID_USAGE_PAGE_GENERIC; + dxgi.raw_input_device[0].usUsage = HID_USAGE_GENERIC_MOUSE; + dxgi.raw_input_device[0].dwFlags = RIDEV_INPUTSINK; + dxgi.raw_input_device[0].hwndTarget = dxgi.h_wnd; } static void gfx_dxgi_set_fullscreen_changed_callback(void (*on_fullscreen_changed)(bool is_now_fullscreen)) { @@ -564,8 +593,7 @@ static void gfx_dxgi_set_mouse_pos(int32_t x, int32_t y) { } static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { - POINT p; - GetCursorPos(&p); + POINT p = dxgi.mouse_pos; ScreenToClient(dxgi.h_wnd, &p); *x = p.x; *y = p.y; @@ -573,19 +601,14 @@ static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) { if (dxgi.is_mouse_captured && dxgi.in_focus) { - POINT p; - GetCursorPos(&p); - ScreenToClient(dxgi.h_wnd, &p); - int32_t centerX, centerY; - centerX = dxgi.current_width / 2; - centerY = dxgi.current_height / 2; - *x = p.x - centerX; - *y = p.y - centerY; - SetCursorPos(dxgi.posX + centerX, dxgi.posY + centerY); + *x = dxgi.mouse_delta.x; + *y = dxgi.mouse_delta.y; } else { *x = 0; *y = 0; } + dxgi.mouse_delta.x = 0; + dxgi.mouse_delta.y = 0; } static void gfx_dxgi_get_mouse_wheel(float* x, float* y) { From dd839deb8864b48e96ef34063333354189e167bb Mon Sep 17 00:00:00 2001 From: lilacLunatic <8488221+lilacLunatic@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:58:34 -0300 Subject: [PATCH 2/9] fixes --- src/graphic/Fast3D/gfx_dxgi.cpp | 55 ++++++++++++++++----------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index 61c43a574..101c8ca97 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -1,4 +1,4 @@ -#if defined(ENABLE_DX11) || defined(ENABLE_DX12) || true +#if defined(ENABLE_DX11) || defined(ENABLE_DX12) #include #include @@ -94,7 +94,6 @@ static struct { bool is_mouse_captured; bool in_focus; RAWINPUTDEVICE raw_input_device[1]; - POINT mouse_pos; POINT mouse_delta; void (*on_fullscreen_changed)(bool is_now_fullscreen); @@ -428,19 +427,17 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par case WM_MOUSEWHEEL: dxgi.mouse_wheel[1] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA; break; - case WM_INPUT: - uint32_t size = sizeof(RAWINPUT); - static RAWINPUT raw[sizeof(RAWINPUT)]; - GetRawInputData((HRAWINPUT)lparam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); - - if (raw->header.dwType == RIM_TYPEMOUSE) { - dxgi.mouse_delta.x += raw->data.mouse.lLastX - dxgi.mouse_pos.x; - dxgi.mouse_delta.y += raw->data.mouse.lLastY - dxgi.mouse_pos.y; - dxgi.mouse_pos.x = raw->data.mouse.lLastX; - dxgi.mouse_pos.y = raw->data.mouse.lLastY; - } - + case WM_INPUT: { + uint32_t size = sizeof(RAWINPUT); + static RAWINPUT raw[sizeof(RAWINPUT)]; + GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); + + if (raw->header.dwType == RIM_TYPEMOUSE) { + dxgi.mouse_delta.x = raw->data.mouse.lLastX; + dxgi.mouse_delta.y = raw->data.mouse.lLastY; + } break; + } case WM_DROPFILES: DragQueryFileA((HDROP)w_param, 0, fileName, 256); Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, fileName); @@ -545,17 +542,18 @@ void gfx_dxgi_init(const char* game_name, const char* gfx_api_name, bool start_i DragAcceptFiles(dxgi.h_wnd, TRUE); // Mouse init - #ifndef HID_USAGE_PAGE_GENERIC - #define HID_USAGE_PAGE_GENERIC ((unsigned short) 0x01) - #endif - #ifndef HID_USAGE_GENERIC_MOUSE - #define HID_USAGE_GENERIC_MOUSE ((unsigned short) 0x02) - #endif - - dxgi.raw_input_device[0].usUsagePage = HID_USAGE_PAGE_GENERIC; - dxgi.raw_input_device[0].usUsage = HID_USAGE_GENERIC_MOUSE; - dxgi.raw_input_device[0].dwFlags = RIDEV_INPUTSINK; - dxgi.raw_input_device[0].hwndTarget = dxgi.h_wnd; + #ifndef HID_USAGE_PAGE_GENERIC + #define HID_USAGE_PAGE_GENERIC ((unsigned short) 0x01) + #endif + #ifndef HID_USAGE_GENERIC_MOUSE + #define HID_USAGE_GENERIC_MOUSE ((unsigned short) 0x02) + #endif + + dxgi.raw_input_device[0].usUsagePage = HID_USAGE_PAGE_GENERIC; + dxgi.raw_input_device[0].usUsage = HID_USAGE_GENERIC_MOUSE; + dxgi.raw_input_device[0].dwFlags = RIDEV_INPUTSINK; + dxgi.raw_input_device[0].hwndTarget = dxgi.h_wnd; + RegisterRawInputDevices(dxgi.raw_input_device, 1, sizeof(dxgi.raw_input_device[0])); } static void gfx_dxgi_set_fullscreen_changed_callback(void (*on_fullscreen_changed)(bool is_now_fullscreen)) { @@ -593,7 +591,8 @@ static void gfx_dxgi_set_mouse_pos(int32_t x, int32_t y) { } static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { - POINT p = dxgi.mouse_pos; + POINT p; + GetCursorPos(&p); ScreenToClient(dxgi.h_wnd, &p); *x = p.x; *y = p.y; @@ -601,8 +600,8 @@ static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) { if (dxgi.is_mouse_captured && dxgi.in_focus) { - *x = dxgi.mouse_delta.x; - *y = dxgi.mouse_delta.y; + *x = dxgi.mouse_delta.x * 80; + *y = dxgi.mouse_delta.y * 80; } else { *x = 0; *y = 0; From 385b90db4b2e9070bfa2059a581bcaebd27c489d Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 07:55:16 +0700 Subject: [PATCH 3/9] focus fix --- src/graphic/Fast3D/gfx_dxgi.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index 101c8ca97..ba9612699 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -428,13 +428,15 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par dxgi.mouse_wheel[1] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA; break; case WM_INPUT: { - uint32_t size = sizeof(RAWINPUT); - static RAWINPUT raw[sizeof(RAWINPUT)]; - GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); - - if (raw->header.dwType == RIM_TYPEMOUSE) { - dxgi.mouse_delta.x = raw->data.mouse.lLastX; - dxgi.mouse_delta.y = raw->data.mouse.lLastY; + if (dxgi.in_focus) { + uint32_t size = sizeof(RAWINPUT); + static RAWINPUT raw[sizeof(RAWINPUT)]; + GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); + + if (raw->header.dwType == RIM_TYPEMOUSE) { + dxgi.mouse_delta.x = raw->data.mouse.lLastX; + dxgi.mouse_delta.y = raw->data.mouse.lLastY; + } } break; } From 5094656627ffad904ad5420dfc4bb23a3e72dc37 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 07:56:10 +0700 Subject: [PATCH 4/9] input buffering fix --- src/graphic/Fast3D/gfx_dxgi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index ba9612699..120b2d672 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -434,8 +434,8 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); if (raw->header.dwType == RIM_TYPEMOUSE) { - dxgi.mouse_delta.x = raw->data.mouse.lLastX; - dxgi.mouse_delta.y = raw->data.mouse.lLastY; + dxgi.mouse_delta.x += raw->data.mouse.lLastX; + dxgi.mouse_delta.y += raw->data.mouse.lLastY; } } break; From 52202854cccc7868c113f39ae32ddca88f970d88 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 07:59:42 +0700 Subject: [PATCH 5/9] remove multiplyer --- src/graphic/Fast3D/gfx_dxgi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index 120b2d672..a0bd94e51 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -602,8 +602,8 @@ static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) { if (dxgi.is_mouse_captured && dxgi.in_focus) { - *x = dxgi.mouse_delta.x * 80; - *y = dxgi.mouse_delta.y * 80; + *x = dxgi.mouse_delta.x; + *y = dxgi.mouse_delta.y; } else { *x = 0; *y = 0; From 33abb74b324169a1fce2e2a8b55a1ab90a695cc9 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 08:12:52 +0700 Subject: [PATCH 6/9] use raw input for mouse capture only & compatability fix --- src/graphic/Fast3D/gfx_dxgi.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index a0bd94e51..bb3367759 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -94,7 +94,7 @@ static struct { bool is_mouse_captured; bool in_focus; RAWINPUTDEVICE raw_input_device[1]; - POINT mouse_delta; + POINT raw_mouse_delta; void (*on_fullscreen_changed)(bool is_now_fullscreen); bool (*on_key_down)(int scancode); @@ -428,14 +428,14 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par dxgi.mouse_wheel[1] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA; break; case WM_INPUT: { - if (dxgi.in_focus) { + if (dxgi.is_mouse_captured && dxgi.in_focus) { uint32_t size = sizeof(RAWINPUT); static RAWINPUT raw[sizeof(RAWINPUT)]; GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); if (raw->header.dwType == RIM_TYPEMOUSE) { - dxgi.mouse_delta.x += raw->data.mouse.lLastX; - dxgi.mouse_delta.y += raw->data.mouse.lLastY; + dxgi.raw_mouse_delta.x += raw->data.mouse.lLastX; + dxgi.raw_mouse_delta.y += raw->data.mouse.lLastY; } } break; @@ -601,15 +601,21 @@ static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) { } static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) { - if (dxgi.is_mouse_captured && dxgi.in_focus) { - *x = dxgi.mouse_delta.x; - *y = dxgi.mouse_delta.y; - } else { + if (!dxgi.in_focus) { *x = 0; *y = 0; + } else if (dxgi.is_mouse_captured) { + *x = dxgi.raw_mouse_delta.x; + *y = dxgi.raw_mouse_delta.y; + } else { + static int32_t prev_x = 0, prev_y = 0; + int32_t current_x, current_y; + gfx_dxgi_get_mouse_pos(¤t_x, ¤t_y); + *x = current_x - prev_x; + *y = current_y - prev_y; + prev_x = current_x; + prev_y = current_y; } - dxgi.mouse_delta.x = 0; - dxgi.mouse_delta.y = 0; } static void gfx_dxgi_get_mouse_wheel(float* x, float* y) { From d524835333f1f2ec785d1db6b36d2b4031d78880 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 08:37:59 +0700 Subject: [PATCH 7/9] fix --- src/graphic/Fast3D/gfx_dxgi.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index bb3367759..e7975e033 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -94,7 +94,7 @@ static struct { bool is_mouse_captured; bool in_focus; RAWINPUTDEVICE raw_input_device[1]; - POINT raw_mouse_delta; + POINT raw_mouse_delta_buf; void (*on_fullscreen_changed)(bool is_now_fullscreen); bool (*on_key_down)(int scancode); @@ -434,8 +434,8 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par GetRawInputData((HRAWINPUT)l_param, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)); if (raw->header.dwType == RIM_TYPEMOUSE) { - dxgi.raw_mouse_delta.x += raw->data.mouse.lLastX; - dxgi.raw_mouse_delta.y += raw->data.mouse.lLastY; + dxgi.raw_mouse_delta_buf.x += raw->data.mouse.lLastX; + dxgi.raw_mouse_delta_buf.y += raw->data.mouse.lLastY; } } break; @@ -605,8 +605,10 @@ static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) { *x = 0; *y = 0; } else if (dxgi.is_mouse_captured) { - *x = dxgi.raw_mouse_delta.x; - *y = dxgi.raw_mouse_delta.y; + *x = dxgi.raw_mouse_delta_buf.x; + *y = dxgi.raw_mouse_delta_buf.y; + dxgi.raw_mouse_delta_buf.x = 0; + dxgi.raw_mouse_delta_buf.y = 0; } else { static int32_t prev_x = 0, prev_y = 0; int32_t current_x, current_y; From dadfaada49ced9ad17af9dbae3a570a176ac2bf9 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 09:20:00 +0700 Subject: [PATCH 8/9] clang format --- src/graphic/Fast3D/gfx_dxgi.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index e7975e033..84521768c 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -543,13 +543,13 @@ void gfx_dxgi_init(const char* game_name, const char* gfx_api_name, bool start_i DragAcceptFiles(dxgi.h_wnd, TRUE); - // Mouse init - #ifndef HID_USAGE_PAGE_GENERIC - #define HID_USAGE_PAGE_GENERIC ((unsigned short) 0x01) - #endif - #ifndef HID_USAGE_GENERIC_MOUSE - #define HID_USAGE_GENERIC_MOUSE ((unsigned short) 0x02) - #endif +// Mouse init +#ifndef HID_USAGE_PAGE_GENERIC +#define HID_USAGE_PAGE_GENERIC ((unsigned short)0x01) +#endif +#ifndef HID_USAGE_GENERIC_MOUSE +#define HID_USAGE_GENERIC_MOUSE ((unsigned short)0x02) +#endif dxgi.raw_input_device[0].usUsagePage = HID_USAGE_PAGE_GENERIC; dxgi.raw_input_device[0].usUsage = HID_USAGE_GENERIC_MOUSE; From aa48488e1858ed1785cdded820eb77d8e808ecbe Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 25 Jan 2025 09:24:03 +0700 Subject: [PATCH 9/9] small moving around --- src/graphic/Fast3D/gfx_dxgi.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index 84521768c..77fa27a67 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -44,6 +44,13 @@ #define NANOSECOND_IN_SECOND 1000000000 #define _100NANOSECONDS_IN_SECOND 10000000 +#ifndef HID_USAGE_PAGE_GENERIC +#define HID_USAGE_PAGE_GENERIC ((unsigned short)0x01) +#endif +#ifndef HID_USAGE_GENERIC_MOUSE +#define HID_USAGE_GENERIC_MOUSE ((unsigned short)0x02) +#endif + using namespace Microsoft::WRL; // For ComPtr static struct { @@ -543,14 +550,7 @@ void gfx_dxgi_init(const char* game_name, const char* gfx_api_name, bool start_i DragAcceptFiles(dxgi.h_wnd, TRUE); -// Mouse init -#ifndef HID_USAGE_PAGE_GENERIC -#define HID_USAGE_PAGE_GENERIC ((unsigned short)0x01) -#endif -#ifndef HID_USAGE_GENERIC_MOUSE -#define HID_USAGE_GENERIC_MOUSE ((unsigned short)0x02) -#endif - + // Mouse init dxgi.raw_input_device[0].usUsagePage = HID_USAGE_PAGE_GENERIC; dxgi.raw_input_device[0].usUsage = HID_USAGE_GENERIC_MOUSE; dxgi.raw_input_device[0].dwFlags = RIDEV_INPUTSINK;