SDL_Delay isn't good enough; I need my window to stay open!

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…
-Michael Sullivan-

The most efficient and “correct” way (depending on your application, of
course, but generally) is probably to just sit in a loop with SDL_WaitEvent(),
waiting for an SDL_QUIT event.On Saturday 12 March 2011, at 20.20.28, Michael Sullivan wrote:

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…
-Michael Sullivan-

You need an event loop. See http://wiki.libsdl.org/moin.cgi/CategoryEvents
for the basics of SDL_Event handling. Basically, you put your program into
an infinite loop that breaks when you receive a quit event and processes all
other events appropriately. After the quit event, it’s safe to close.

Mason>----- Original Message ----

From: Michael Sullivan
Subject: [SDL] SDL_Delay isn’t good enough; I need my window to stay open!

Is this not right?

int main()
{
drawBattle mybattle = drawBattle();
mybattle.init();

mybattle.drawScreen();

SDL_Event event;

bool quit = false;
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
exit(0);
}
quit = true;
}

// SDL_Delay(5000);

return 0;
}

It stops suddenly.On Sat, 2011-03-12 at 20:25 +0100, David Olofson wrote:

On Saturday 12 March 2011, at 20.20.28, Michael Sullivan <@Michael_Sullivan> wrote:

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…

The most efficient and “correct” way (depending on your application, of
course, but generally) is probably to just sit in a loop with SDL_WaitEvent(),
waiting for an SDL_QUIT event.

I should say that the window stays open for half a second and then
closes.On Sat, 2011-03-12 at 14:37 -0600, Michael Sullivan wrote:

On Sat, 2011-03-12 at 20:25 +0100, David Olofson wrote:

On Saturday 12 March 2011, at 20.20.28, Michael Sullivan <@Michael_Sullivan> wrote:

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…

The most efficient and “correct” way (depending on your application, of
course, but generally) is probably to just sit in a loop with SDL_WaitEvent(),
waiting for an SDL_QUIT event.

Is this not right?

int main()
{
drawBattle mybattle = drawBattle();
mybattle.init();

mybattle.drawScreen();

SDL_Event event;

bool quit = false;
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
exit(0);
}
quit = true;
}

// SDL_Delay(5000);

return 0;
}

It stops suddenly.

Nevermind. The stupid quit variable gets set to true regardless of
whether or not the signal is received. I moved that line into the case
condition and it works correctly now…On Sat, 2011-03-12 at 14:40 -0600, Michael Sullivan wrote:

On Sat, 2011-03-12 at 14:37 -0600, Michael Sullivan wrote:

On Sat, 2011-03-12 at 20:25 +0100, David Olofson wrote:

On Saturday 12 March 2011, at 20.20.28, Michael Sullivan <@Michael_Sullivan> wrote:

Is there a way for my SDL window to stay open indefinitely or until the
close button is clicked? Right now I’m using SDL Delay, but I do not
know how long the window will need to stay open…

The most efficient and “correct” way (depending on your application, of
course, but generally) is probably to just sit in a loop with SDL_WaitEvent(),
waiting for an SDL_QUIT event.

Is this not right?

int main()
{
drawBattle mybattle = drawBattle();
mybattle.init();

mybattle.drawScreen();

SDL_Event event;

bool quit = false;
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
exit(0);
}
quit = true;
}

// SDL_Delay(5000);

return 0;
}

It stops suddenly.

I should say that the window stays open for half a second and then
closes.