floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.08k stars 498 forks source link

[sokol_app] Alt-tabbing issues on windows in a FPS game on a multi-monitor setup #1056

Open jakubtomsu opened 5 months ago

jakubtomsu commented 5 months ago

I'm making a 3d FPS game with sokol_app and sokol_gfx, and I use sapp.lock_mouse(true) and sapp.show_mouse(false) to get the correct hidden FPS cursor behavior. However after alt-tabbing out of the app and focusing back it is broken. The mouse is hidden, but it can exit the clipping rectangle and move to another monitor.

I tried to call lock_mouse and show_mouse both only in init_cb or every frame, both approaches don't work in slightly different ways.

Maybe the issue is with my code, I haven't had time to investigate further. But any help is appreciated :)

floooh commented 5 months ago

Hmm yeah I'm seeing the same thing in the "doom-sokol" example (on Windows, haven't tested other platforms).

One workaround seems to be to disable, and then enable again the mouse lock in the FOCUSED event like this:

https://github.com/floooh/doom-sokol/blob/e87eb25c648f6cc4731fc1a6ba3f72df24abd826/src/doomgeneric_sokol.c#L552-L555

...the explicit disabling is necessary because of this check:

https://github.com/floooh/sokol/blob/f30824cebaed82600d5d524259a9fc4af5f7a69e/sokol_app.h#L7096-L7098

I seem to remember that this was necessary because there's a Windows routine somewhere in there which has something like a reference counter.

Maybe the same is necessary when going from iconified back to restored.

Btw: I'll be travelling from tomorrow until Thursday, I may not respond during that time.

PS: I should really tackle that higher level "sokol_input.h" header which tracks input events, provides a polling API and takes care of little warts like this that are hard to do right down in sokol_app.h with its "stateless" input events.

jakubtomsu commented 5 months ago

Thank you, I'll try that out. The sokol_input header would be cool, especially if there was also support for gamepads (or have a separate sokol_gamepad which I've seen mentioned)