Skip to content

Commit

Permalink
Only pass mouse/keyboard to dethrace when the current window is DethR…
Browse files Browse the repository at this point in the history
…ace (#404)

This is useful when opening a 2nd SDL window to avoid events of the
other window leaking into carmageddon (and the reverse).
  • Loading branch information
madebr authored Sep 12, 2024
1 parent a25ee50 commit cd64d99
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ option(DETHRACE_WERROR "Treat warnings as errors")
option(DETHRACE_FIX_BUGS "Fix Dethrace bugs" ON)

function(add_compile_flag_if_supported TARGET FLAG)
cmake_parse_arguments(ARGS "" "" "LANGUAGES" ${ARGN})
if(NOT ARGS_LANGUAGES)
set(ARGS_LANGUAGES C CXX)
endif()
string(MAKE_C_IDENTIFIER "${FLAG}" FLAG_TO_IDENTIFIER)
set(HAVE_FLAG_VARIABLE_NAME "HAVE_${FLAG_TO_IDENTIFIER}")
check_c_compiler_flag("${FLAG}" "${HAVE_FLAG_VARIABLE_NAME}")
if(${HAVE_FLAG_VARIABLE_NAME})
target_compile_options("${TARGET}" PRIVATE "${FLAG}")
target_compile_options("${TARGET}" PRIVATE "$<$<COMPILE_LANGUAGE:${ARGS_LANGUAGES}>:${FLAG}>")
endif()
endfunction()

Expand Down
4 changes: 1 addition & 3 deletions src/harness/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ if(NOT MSVC)
-Wextra
-Wno-unused-parameter
)
add_compile_flags_if_supported(harness
-Wstrict-prototypes
)
add_compile_flag_if_supported(harness -Wstrict-prototypes LANGUAGES C)
else()
target_compile_definitions(harness PRIVATE -D_CRT_SECURE_NO_WARNINGS)
endif()
Expand Down
20 changes: 20 additions & 0 deletions src/harness/platforms/sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit cd64d99

Please sign in to comment.