Jump to content
  • Advertisement
Sign in to follow this  
rayli1107

how to check if SDL window is active

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
straight from the docs...

Name
SDL_GetAppState -- Get the state of the application
Synopsis

#include "SDL.h"

Uint8 SDL_GetAppState(void);

Description

This function returns the current state of the application. The value returned is a bitwise combination of:

SDL_APPMOUSEFOCUS The application has mouse focus.
SDL_APPINPUTFOCUS The application has keyboard focus
SDL_APPACTIVE The application is visible

EDIT: beaten by a second :(

Share this post


Link to post
Share on other sites
hmm not sure how SDL_APPACTIVE works... what exactly does it mean by visible?

I have a program doing animation, and I want it to pause whenever the program loses focus (by minimizing or alt-tab out). The SDL_APPACTIVE bit from the SDL_GetAppState function is always on, whether the program is in focus or not.

Share this post


Link to post
Share on other sites
This is what you basically want to do, sorry for the horrible indentation, it was a copy paste from some old code formatted for a dll project. I've just pasted the entire loop.


while(Done == 0)
{
if ( SDL_PollEvent(&Event) )
{
if ( Event.type == SDL_QUIT )
Done = 1;
if ( Event.type == SDL_ACTIVEEVENT) // It's an ACTIVE Event?
{
if(Event.active.state & SDL_APPACTIVE) // Activity Level Changed? (IE: Iconified?)
{
if(Event.active.gain) // Activity's Been Gained?
Visible = true; // If Yes, Set AppStatus.Visible
else // Otherwhise
Visible = false; // Reset AppStatus.Visible
}
if(Event.active.state & SDL_APPMOUSEFOCUS) // The Mouse Cursor Has Left/Entered The Window Space?
{
if(Event.active.gain) // Entered?
MouseFocus = true; // Report It Setting AppStatus.MouseFocus
else // Otherwhise
MouseFocus = false; // The Cursor Has Left, Reset AppStatus.MouseFocus
}
if(Event.active.state & SDL_APPINPUTFOCUS) // The Window Has Gained/Lost Input Focus?
{
if(Event.active.gain) // Gained?
KeyboardFocus = true; // Report It Where You Know (You Always Report, You're A Spy, Aren't You?!)
else // Otherwhise
KeyboardFocus = false; // Reset KeyboardFocus
}
}
}
else
{
if(!Visible) // If The Application Is Not Visible
SDL_WaitEvent(NULL); // Wait For An Event
else // Otherwhise
{
BeginDraw();
Update();
Draw();
EndDraw();
}
}
}





So after you declare your 3 variables, Visible, KeyboardFocus, and MouseFocus, all you would have to do is check to see if Visible was false, and if it was, then you'd pause your animation or do whatever you want to do. This code works, loses focus by alt-tab as well as just changing the active window from your SDL program. If you need any help integrating it into your code or need further explanations, feel free to ask!

This is just a working example of what thannett, Will F, and rip-off have mentioned [smile] I don't know why the SDL_GetAppState function is not working correctly for you. Are you using: bool Visible = (SDL_GetAppState() & SDL_APPACTIVE); ? (Note the bitwise & and not a && )

Share this post


Link to post
Share on other sites
I've tried using events triggering, but for some reason, my program receives SDL_ACTIVEEVENT with state = SDL_APPACTIVE and gain = TRUE when it gains focus, but it doesn't get any event whenever it loses focus.

Share this post


Link to post
Share on other sites
hmm that's weird... after playing around i've figured out that my SDL_APPACTIVE macro was defined incorrectly... don't know if it's something wrong with the SDL library...

anyways got it working now thx....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!