Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game.IsActive is always true on launch, even if the window doesn't have focus #7992

Open
bwiklund opened this issue Feb 24, 2023 · 4 comments

Comments

@bwiklund
Copy link

bwiklund commented Feb 24, 2023

The repro steps for this are quick:

Make a new game from the desktopgl template:
dotnet new mgdesktopgl -o MyGame

On Game1.cs:45, change the drawing code so we can get some visual feedback on the state of Game.IsActive:
GraphicsDevice.Clear(IsActive ? Color.CornflowerBlue : Color.OrangeRed);

Launch the game (dotnet run or a build), then quickly at tab to some other window and click on it to give it focus.

Here it is behind a chrome window, for example:

image

It's blue, showing that IsActive is true even though we have never given the window focus.

Tabbing in and then out correctly turns the window red, as IsActive is now correctly false.

image

I've seen several google results for this bug going back years, but none of the commonly quoted solutions actually fix the problem (checking window handles, registering our own events, etc). it would be awesome if this could be fixed at the source

@Apostolique recommended including this SDL2 file, and doing this workaround:

https://github.com/flibitijibibo/SDL2-CS/blob/master/src/SDL2.cs

SDL_WindowFlags flags = (SDL_WindowFlags)SDL_GetWindowFlags(Window.Handle);
Console.WriteLine(flags.HasFlag(SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS));

This fixes the bug: is it possible monogame could check this way instead? Currently it's listening to events, which means it misses the initial loss of focus before the app has fully started.

case Sdl.Window.EventId.FocusGained:
IsActive = true;
break;
case Sdl.Window.EventId.FocusLost:
IsActive = false;
break;

  • MonoGame 3.8.1.303
  • Windows
  • DesktopGL
@bwiklund
Copy link
Author

bwiklund commented Oct 5, 2023

for anyone still tracking this issue, the SDL workaround fixed it BUT it made it so one players game simply never thought it had focus. i haven't been able to track down anything different about their setup, but this bug does still exist in 3.8.1.303

@AmasaDelano
Copy link

For anyone like me who finds this issue and follows @Apostolique's suggestion above to fix it: The SDL2.cs file linked has unsafe functions in it. I needed to add this to my .csproj file for it to compile without erroring:

<PropertyGroup>
  <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

@Lightning2X
Copy link

Lightning2X commented Dec 25, 2023

I've also encountered this issue window always starts focused even if its on a different screen

@mrhelmut
Copy link
Contributor

Here's an attempt to fix this issue #8123

Testing welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants