diff options
| author | mooff <[email protected]> | 2024-02-29 17:30:38 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-29 18:30:38 +0100 |
| commit | 94c79917e3b68fbed3ff4a3920edc9d093b11c4f (patch) | |
| tree | c14edf356bb394471091b2da245d54d1303c0bf8 /src | |
| parent | 6589311a0b7a217ec270000eb3ab2a5172992efa (diff) | |
| download | raylib-94c79917e3b68fbed3ff4a3920edc9d093b11c4f.tar.gz raylib-94c79917e3b68fbed3ff4a3920edc9d093b11c4f.zip | |
Fix SDL multitouch tracking (#3810)
The fingerId from SDL was used as an index into the CORE.Input.Touch
arrays, but it's an opaque / arbitrary int64, way bigger
than MAX_TOUCH_POINTS, so the first non-simulated touch event would segfault.
Diffstat (limited to 'src')
| -rw-r--r-- | src/platforms/rcore_desktop_sdl.c | 44 |
1 files changed, 20 insertions, 24 deletions
diff --git a/src/platforms/rcore_desktop_sdl.c b/src/platforms/rcore_desktop_sdl.c index f567119e..1b6304d2 100644 --- a/src/platforms/rcore_desktop_sdl.c +++ b/src/platforms/rcore_desktop_sdl.c @@ -971,6 +971,23 @@ void SetMouseCursor(int cursor) CORE.Input.Mouse.cursor = cursor; } +static void UpdateSDLTouchPoints(SDL_TouchFingerEvent event) +{ + CORE.Input.Touch.pointCount = SDL_GetNumTouchFingers(event.touchId); + + for (int i=0; i<CORE.Input.Touch.pointCount; i++) + { + SDL_Finger *finger = SDL_GetTouchFinger(event.touchId, i); + CORE.Input.Touch.pointId[i] = finger->id; + CORE.Input.Touch.position[i].x = finger->x * CORE.Window.screen.width; + CORE.Input.Touch.position[i].y = finger->y * CORE.Window.screen.height; + CORE.Input.Touch.currentTouchState[i] = 1; + } + + for (int i=CORE.Input.Touch.pointCount; i<MAX_TOUCH_POINTS; i++) + CORE.Input.Touch.currentTouchState[i] = 0; +} + // Register all input events void PollInputEvents(void) { @@ -1009,15 +1026,7 @@ void PollInputEvents(void) // Register previous touch states for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i]; - // Reset touch positions - // TODO: It resets on target platform the mouse position and not filled again until a move-event, - // so, if mouse is not moved it returns a (0, 0) position... this behaviour should be reviewed! - //for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 }; - // Map touch position to mouse position for convenience - // WARNING: If the target desktop device supports touch screen, this behavious should be reviewed! - // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch - // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition; int touchAction = -1; // 0-TOUCH_ACTION_UP, 1-TOUCH_ACTION_DOWN, 2-TOUCH_ACTION_MOVE @@ -1207,34 +1216,21 @@ void PollInputEvents(void) touchAction = 2; } break; - // Check touch events - // NOTE: These cases need to be reviewed on a real touch screen case SDL_FINGERDOWN: { - const int touchId = (int)event.tfinger.fingerId; - CORE.Input.Touch.currentTouchState[touchId] = 1; - CORE.Input.Touch.position[touchId].x = event.tfinger.x * CORE.Window.screen.width; - CORE.Input.Touch.position[touchId].y = event.tfinger.y * CORE.Window.screen.height; - + UpdateSDLTouchPoints(event.tfinger); touchAction = 1; realTouch = true; } break; case SDL_FINGERUP: { - const int touchId = (int)event.tfinger.fingerId; - CORE.Input.Touch.currentTouchState[touchId] = 0; - CORE.Input.Touch.position[touchId].x = event.tfinger.x * CORE.Window.screen.width; - CORE.Input.Touch.position[touchId].y = event.tfinger.y * CORE.Window.screen.height; - + UpdateSDLTouchPoints(event.tfinger); touchAction = 0; realTouch = true; } break; case SDL_FINGERMOTION: { - const int touchId = (int)event.tfinger.fingerId; - CORE.Input.Touch.position[touchId].x = event.tfinger.x * CORE.Window.screen.width; - CORE.Input.Touch.position[touchId].y = event.tfinger.y * CORE.Window.screen.height; - + UpdateSDLTouchPoints(event.tfinger); touchAction = 2; realTouch = true; } break; |
