Skip to content

Commit

Permalink
Only pass mouse/keyboard to dethrace when the current window is DethRace
Browse files Browse the repository at this point in the history
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 committed Aug 1, 2024
1 parent 361eca4 commit f1da749
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/harness/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if(NOT MSVC)
-Wno-unused-parameter
)
add_compile_flags_if_supported(harness
-Wstrict-prototypes
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>
)
else()
target_compile_definitions(harness PRIVATE -D_CRT_SECURE_NO_WARNINGS)
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 f1da749

Please sign in to comment.