Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL_PollEvent() problem

Started by andrern2000 Mar 29, 2009 at 7:22 AM 2 replies 2.6k views
Original Post
andrern2000
andrern2000
Hi, nobody answers my thread in For Beginner section, so I post it here ----------------- Has anyone experienced SDL_PollEvent(&event) in a function quits its while loop when it's executed? while(SDL_PollEvent(&event)) ...in this code: int title_screen() { bool quit = false; Title theTitle; while( quit == false ) { //While there's events to handle while( SDL_PollEvent( &event ) ) { theTitle.handle_input(); if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //Apply surfaces apply_surface ( 0, 0, title, screen ); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } } return 0; } I'm using Multithreaded DLL code generation. The first time title_screen()executed it's normal. That function title_screen() is called within main(). But second time it's executed (the flow is after title_screen() -> main_menu() -> title_screen(), so this function is called again) the SDL_PollEvent() returns false, avoiding the handle_input() function. The main_menu() uses SDL_PollEvent() too and experiencing same problem: First time called it's normal, second call PollEvent() quits irresponsibly. What possibly happening? --------- Btw, this is my main() function: main(...) { while (quit=false) { if mode=1 { title_screen(); } else { mainmenu(); } //While there's events to handle while( SDL_PollEvent( &event ) ) <-- Could it be collides with &event here? { if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } }
rip-off
rip-off
It is hard to say.

The only thing I find strange is that the only way "title_screen" can return is if quit is true, which indicates that a SDL_QUIT event occurred. It is usual to exit the program in this case, I don't know whether SDL does something internally to the event queue once a QUIT signal has been generated. It might conceivably disassociate itself from the windows or something.

Have you tried using an alternative quit method, such as maybe the escape button?

A minor point: is "event" global? It should be pretty trivial to make it local in all cases. Simply pass it as a parameter to any functions that need access to it.
JParishy
JParishy
[SOURCE]main(...){	while (quit=false) // Problem 1	{		if mode=1		{			title_screen();		}		else		{			mainmenu();		}		//While there's events to handle		while( SDL_PollEvent( &event ) ) <-- Could it be collides with &event here?		{			if( event.type == SDL_QUIT )			{ 				//Quit the program				quit = true;			}		}	}}[/SOURCE]


Is the above code (with added formatting) actual code, or pseudo-code? If the former, your first while statement is assigning the variable 'quit' to false, then using it to test the while loop, which would immediately evaluate to false, and the code in that scope would never be ran. If not, than I would try what rip-off said, and test out quitting through a different button, such as the escape button like rip-off suggested.
andrern2000
andrern2000
Quote:
Original post by rip-off
It is hard to say.

The only thing I find strange is that the only way "title_screen" can return is if quit is true, which indicates that a SDL_QUIT event occurred. It is usual to exit the program in this case, I don't know whether SDL does something internally to the event queue once a QUIT signal has been generated. It might conceivably disassociate itself from the windows or something.

Have you tried using an alternative quit method, such as maybe the escape button?

A minor point: is "event" global? It should be pretty trivial to make it local in all cases. Simply pass it as a parameter to any functions that need access to it.


I think I'll try that escape button exit method for the exit.
Event is global. Yes, I think I'm going to change it to local to avoid internal collision with multithreading compilation. Thanks.

@JParishy: it's the actual code. Sorry I didn't write the bool quit = false in the init. But everything went well. It enters the while loop and execute title_screen().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.