From f1da74918e554f6a1f796f4bdf6036f8dcc09b2c Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Fri, 2 Aug 2024 00:55:31 +0200 Subject: [PATCH] Only pass mouse/keyboard to dethrace when the current window is DethRace This is useful when opening a 2nd SDL window to avoid events of the other window leaking into carmageddon (and the reverse). --- src/harness/CMakeLists.txt | 2 +- src/harness/platforms/sdl2.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/harness/CMakeLists.txt b/src/harness/CMakeLists.txt index dc3ec120..bf66b9ec 100644 --- a/src/harness/CMakeLists.txt +++ b/src/harness/CMakeLists.txt @@ -28,7 +28,7 @@ if(NOT MSVC) -Wno-unused-parameter ) add_compile_flags_if_supported(harness - -Wstrict-prototypes + $<$:-Wstrict-prototypes> ) else() target_compile_definitions(harness PRIVATE -D_CRT_SECURE_NO_WARNINGS) diff --git a/src/harness/platforms/sdl2.c b/src/harness/platforms/sdl2.c index 6ba8ad8c..1a9417d5 100644 --- a/src/harness/platforms/sdl2.c +++ b/src/harness/platforms/sdl2.c @@ -90,6 +90,9 @@ static int get_and_handle_message(MSG_* msg) { switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: + if (event.key.windowID != SDL_GetWindowID(window)) { + continue; + } if (event.key.keysym.sym == SDLK_RETURN) { if (event.key.type == SDL_KEYDOWN) { if ((event.key.keysym.mod & (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_GUI))) { @@ -115,6 +118,15 @@ static int get_and_handle_message(MSG_* msg) { directinput_key_state[dinput_key] = (event.type == SDL_KEYDOWN ? 0x80 : 0); break; + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_CLOSE) { + if (SDL_GetWindowID(window) == event.window.windowID) { + msg->message = WM_QUIT; + return 1; + } + } + break; + case SDL_QUIT: msg->message = WM_QUIT; return 1; @@ -128,6 +140,11 @@ static void get_keyboard_state(unsigned int count, uint8_t* buffer) { } static int get_mouse_buttons(int* pButton1, int* pButton2) { + if (SDL_GetMouseFocus() != window) { + *pButton1 = 0; + *pButton2 = 0; + return 0; + } int state = SDL_GetMouseState(NULL, NULL); *pButton1 = state & SDL_BUTTON_LMASK; *pButton2 = state & SDL_BUTTON_RMASK; @@ -136,6 +153,9 @@ static int get_mouse_buttons(int* pButton1, int* pButton2) { static int get_mouse_position(int* pX, int* pY) { float lX, lY; + if (SDL_GetMouseFocus() != window) { + return 0; + } SDL_GetMouseState(pX, pY); SDL_RenderWindowToLogical(renderer, *pX, *pY, &lX, &lY);