We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4606

Summary: mouse & touch coordinates being distorted by SDL_RenderSetScale
Product: SDL Reporter: Anthony @ POW Games <ant>
Component: eventsAssignee: Sam Lantinga <slouken>
Status: RESOLVED WORKSFORME QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.9   
Hardware: All   
OS: Android (All)   

Description Anthony @ POW Games 2019-04-24 01:39:58 UTC
I'm getting mouse and touch coordinates that are out of range when changing scale factor with SDL_RenderSetScale or SDL_RenderSetLogicalSize each frame.

This works in Windows 10, but on Android what seems to happen is that some mouse & touch events seem to be affected by a temporary change in scale factor / logical size. I'm guessing it's because mouse and touch events are processed asynchronously on Android and some of these events will hit when the logical size has been changed before being changed back again. Whereas on Windows 10 these events are processed at the end / beginning when the logical size is consistent? (Sorry if that doesn't make sense, I don't know how to better explain it).

Another way to explain:

My game sets a logical size of (10000, 10000). Draws game graphics. Then sets back to a scale factor of (1,1) to draw some debug text on the screen.

This causes random (about 1 in 5 times) mouse and touch coordinates to be wildly off by the time they come through as events. But it only affects Android and not Windows 10.

It may be more of a feature than a bug. If various scale factors are used each frame then which one should be used to scale the mouse and touch coordinates?

Thoughts? possible solutions?
Comment 1 Anthony @ POW Games 2019-04-24 14:13:14 UTC
Deffo a bug. Nothing should be producing touch events above the range of 1.0f, or mouse coordinates larger than the window size.

Why should setting logical scale affect coordinates on Android but not Win10?
Comment 2 Sam Lantinga 2019-04-24 15:02:03 UTC
You're completely correct in your diagnosis. Normally mouse and touch events are scaled so they are in the coordinate space of the logical viewport. On Windows events come in synchronously with your event loop, but on Android they come in asynchronously.

I can add a hint to disable the scaling behavior, would that help?
Comment 3 Anthony @ POW Games 2019-04-24 16:45:57 UTC
My diagnosis was correct? I'm getting good at this, lol! 

I'm still confused though: if mouse coordinates are supposed to be always scaled relative to the window (I assume the window's pixel size?), and touch coordinates always 0.0f to 1.0f, what would the hint do? Why would logical scale influence these coordinates? Logical scale affects 2D drawing coordinates, where user input comes in through pixel coordinates, bypassing logical scaling surely?
Comment 4 Anthony @ POW Games 2020-12-09 19:20:47 UTC
I managed to get around this problem by changing the order in which I change the logical scaling per frame. But since SDL_HINT_MOUSE_RELATIVE_SCALING (thanks Sam) was added, this shouldn't cause a problem in any other situation.