Window Focus

I’m getting pretty smooth animation using SDL_Delay, and the game runs at a
constant speed. This changes when the window goes out of focus, the game
speeds way up in the backgound. Is there a way I can remedy this? Or
maybe detect thatthe window has gone out of focus and pause the game?

  • Mik Mifflin

What you’re looking for is the SDL_VIDEORESIZE event. When you get the
event, you’ll want to set some boolean/control var to stop drawing the
screen.
Check out http://sdldoc.csn.ul.ie/sdlresizeevent.phpOn 2001.07.19 20:43 Mik Mifflin wrote:

I’m getting pretty smooth animation using SDL_Delay, and the game runs at
a
constant speed. This changes when the window goes out of focus, the game

speeds way up in the backgound. Is there a way I can remedy this? Or
maybe detect thatthe window has gone out of focus and pause the game?

  • Mik Mifflin

Actually, what you need is the SDL_ACTIVEEVENT event.
Look for gain == 0 and state & SDL_APPINPUTFOCUS
http://sdldoc.csn.ul.ie/sdlactiveevent.php

What you’re looking for is the SDL_VIDEORESIZE event. When you get the
event, you’ll want to set some boolean/control var to stop drawing the
screen.
Check out http://sdldoc.csn.ul.ie/sdlresizeevent.php

I’m getting pretty smooth animation using SDL_Delay, and the game runs at
a
constant speed. This changes when the window goes out of focus, the game

speeds way up in the backgound. Is there a way I can remedy this? Or
maybe detect thatthe window has gone out of focus and pause the game?

  • Mik Mifflin
-Sam Lantinga, Lead Programmer, Loki Software, Inc.> On 2001.07.19 20:43 Mik Mifflin wrote:

What you’re looking for is the SDL_VIDEORESIZE event. When you get the
event, you’ll want to set some boolean/control var to stop drawing the
screen.
Check out http://sdldoc.csn.ul.ie/sdlresizeevent.php

I’m getting pretty smooth animation using SDL_Delay, and the game runs at
a
constant speed. This changes when the window goes out of focus, the game

speeds way up in the backgound. Is there a way I can remedy this? Or
maybe detect thatthe window has gone out of focus and pause the game?

Or, you can also use the SDL_ACTIVEEVENT event, which tells you your app has
lost focus. I use it in Tux Typing in a way similar to this:

if (SDL_PollEvent(&event) > 0) {
switch (event.type) {
case SDL_KEYDOWN:
{
/* Do something with key down /
}
case SDL_QUIT:
{
/
Do something with quit /
}
case SDL_ACTIVEEVENT:
{
/
App focus loss */
}
}
}

I think you can also use SDL_SysWMEvent for things like this (though I’ve not
tried so I could be wrong):
http://sdldoc.csn.ul.ie/sdlsyswmevent.phpOn Friday 20 July 2001 02:56am, Ti Leggett wrote:

On 2001.07.19 20:43 Mik Mifflin wrote:


Sam “Criswell” Hart <@Sam_Hart> AIM, Yahoo!:
Homepage: < http://www.geekcomix.com/snh/ >
PGP Info: < http://www.geekcomix.com/snh/contact/ >
Advogato: < http://advogato.org/person/criswell/ >

Doh! I have no idea what I was thinking… I looked at my code, saw
SDL_ACTIVEEVENT and wrote SDL_VIDEORESIZE. Sorry for the confusion.On Fri, 20 Jul 2001, Sam Lantinga wrote:

Actually, what you need is the SDL_ACTIVEEVENT event.
Look for gain == 0 and state & SDL_APPINPUTFOCUS
http://sdldoc.csn.ul.ie/sdlactiveevent.php

What you’re looking for is the SDL_VIDEORESIZE event. When you get the
event, you’ll want to set some boolean/control var to stop drawing the
screen.
Check out http://sdldoc.csn.ul.ie/sdlresizeevent.php

On 2001.07.19 20:43 Mik Mifflin wrote:

I’m getting pretty smooth animation using SDL_Delay, and the game runs at
a
constant speed. This changes when the window goes out of focus, the game

speeds way up in the backgound. Is there a way I can remedy this? Or
maybe detect thatthe window has gone out of focus and pause the game?

  • Mik Mifflin

-Sam Lantinga, Lead Programmer, Loki Software, Inc.

I’m getting pretty smooth animation using SDL_Delay, and the game
runs at a constant speed. This changes when the window goes out of
focus, the game speeds way up in the backgound. Is there a way I
can remedy this?

This seems to be something wrong with the timing itself, rather than
rendering in the background… Even if the rendering load goes away,
at least the control system should maintain the speed the game is
designed for, or you’ll have problems with computers that are faster
or slower than you development system.

If you want to maintain a truly constant animation speed (or rather,
control system speed, if you have moving objects and stuff going on
as well), you should base the timing on total frame duration,
rather than “total frame duration - rendering time”, as you do if you
use only SDL_Delay(). Some lines from one of the scrolling examples:

—8<----------------------------------------------------------------
long tick1, tick2;
float dt;

...

/* get initial tick for time calculation */
tick1 = SDL_GetTicks();

while (SDL_PollEvent(&event) >= 0)
{

	...

	/* calculate time since last update */
	tick2 = SDL_GetTicks();
	dt = (tick2 - tick1) * 0.001f;
	tick1 = tick2;

	/* Update foreground position */
	tl_animate(&foreground_layer, dt);

	...

}

---------------------------------------------------------------->8—

“dt” is the time (in seconds) since the last time the SDL_GetTicks()
code was executed, regardless of how much time is spent doing other
stuff (such as rendering) in the main loop.

Now, if you want to put an upper limit to the frame rate (for targets
without retrace sync), that’s a separate issue. You could use
SDL_Delay() to throttle, based on “dt” from the above code snippet,
or just throw in an SDL_Delay(10) to avoid using 100% CPU.

Or maybe detect thatthe window has gone out of
focus and pause the game?

Well, that’s usually a very good idea anyway, and you’ve already
received a number of replies on that topic.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Friday 20 July 2001 03:43, Mik Mifflin wrote: