Jump to content
  • Advertisement
Sign in to follow this  
LGAB

Have an SDL app do nothing but wait

This topic is 3607 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

Hey! I'm writing an application which draws its GUI with OpenGL via SDL. I need this app to be able to live in the background without stealing resources from other apps running. The SDL loop makes the app hover at around 4-6% cpu usage on this computer which isn't that bad, but I want it to completely halt when there is no user interaction. I've tried simple stuff like only drawing when there's mouse movement and using SDL_Delay(), but no combination of if's has worked very well to reduce cpu usage. The only way so far to bring it down to zero is to use a high delay, such as 100 milliseconds, which makes the interface stutter of course. A reasonable solution for me would be to have a loop of very long delays whenever the SDL window isn't active, i.e. when the user tabbed to another application. I'd rather not make it platform specific but it could be worth implementing. Is there a way to see if the SDL window is the active one? Any other ideas on how to make it not use resources when there's no interaction? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by LGAB
Is there a way to see if the SDL window is the active one?

Yes, SDL_ACTIVEEVENT.

Quote:
Any other ideas on how to make it not use resources when there's no interaction?

SDL_Delay() with 150 or so when the mouse is not over the window, otherwise SDL_Delay() with 20. That's the best I know of.

Here's how to use 'SDL_ACTIVEEVENT'.

while(SDL_PollEvent(&event))
{
switch(event.type)
case SDL_ACTIVEEVENT:
{
Uint8 state = event.active.state;

if(state & SDL_APPMOUSEFOCUS)
{
if(event.active.gain == true)
> The mouse is now over the window.
else
> The mouse is no longer over the window.
}

if(state & SDL_APPINPUTFOCUS)
{
if(event.active.gain == true)
> The SDL window is the currently selected window.
else
> The SDL window is no longer the selected window.
}

if(state & SDL_APPACTIVE)
{
if(event.active.gain == true)
> The window has been un-minimized.
else
> The window has been minimized.
}
}
break;
...

...
default: break;
}


You can also use SDL_GetAppState to query the state.

Share this post


Link to post
Share on other sites
SDL_ACTIVEEVENT did the trick! Once the mouse is off the SDL window, CPU goes down to zero, or very close to zero. I could increase the delay of course too [grin]
I'll have a look at GetAppState too.

A million thanks!

Share this post


Link to post
Share on other sites
Two more things:

(1) SDL_WaitEvent() is your friend
(2) SDL_VIDEOEXPOSE event is your friend as well

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!