another question about sdl.

i do realise that i have asked a way to many questions lately, but thats in a way how i learned so please bear with me XD.

this is more of a question than a problem. here is my sdl code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "SDL.h"

int main( int argc, char* args[] )
{
    SDL_Init (SDL_INIT_EVERYTHING);
	SDL_SetVideoMode (640,480, 32, SDL_SWSURFACE);
	SDL_WM_SetCaption ("hello world!", NULL);

	SDL_Delay ( 4000 );

    SDL_Quit();
    return 0;    
}


as you can see i use the SDL_Delay(); to make the program stay open for 4 seconds, but how can i make it stay open untill the user presses the exit button in the top right corner of the program?
You need to have an event loop.

Basically you wait for events (I think the function you want is SDL_WaitEvent, or SDL_PollEvent -- I forget the exact names) and then process them as they're received. When the user closes the program it will sent an SDL_Quit event. Once you receive that event you can exit the loop and thus the program.

There are examples in the SDL docs. Just look for SDL_WaitEvent.
something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "SDL.h"

int main( int argc, char* args[] )
{
    SDL_Init (SDL_INIT_EVERYTHING);
	SDL_SetVideoMode (640,480, 32, SDL_SWSURFACE);
	SDL_WM_SetCaption ("hello world!", NULL);
	SDL_Event event;

	while (SDL_PollEvent (&event)){
		
	}
	

    SDL_Quit();
    return 0;    
}


if so then how do i go from there?
1
2
3
4
5
6
7
8
9
10
11
12
13
SDL_Event event;
bool carry_on = true;

while ( carry_on )
{
    while ( SDL_PollEvent(&event) )
    {
        if ( event.type == SDL_QUIT )
        {
            carry_on = false;
        } // if
    } // while
} // while 


You'll exit that outer loop when you close the window :)
it kinda works but the program freezes when i open it. any idea why?
Hi
Eeesh, you're having some problems with that SDL stuff :(
All I can say is these examples are all working perfectly on my system and I can't see anything wrong with any of the source code. For the record, that's CrunchBang Linux (32-bit) with g++ version 4.4.5.
Do you have a Linux install kicking around anywhere so you could try that? I wonder if the problem lies somewhere with your compiler/SDL libraries setup.
the program kinda works on my system as well(windows 7). but it kinda freezes for the first 3-4 seconds and after that it does not respond. but i can exit the window using the exit thing. but it still dont respond in a way just pops up a window saying it doesen't respond an ask me if i want to exit (like any other program that does not respond.

i am starting to suspect that i have not installed sdl correctly, or that i have a wrong version installed.
i am using visual studio 2010 compiler and the 2005 version of sdl. (i could not find any version older than that)
nevermind, it worked on my laptop so the problem was caused by my computer and not my code. thanks for all the help :D
Topic archived. No new replies allowed.