SDL not responding to events

I’m trying to setup a basic SDL program but it won’t respond to quit, keyboard events or anything. I have to open the task manager to close it. I’m not sure what’s wrong.

Here’s the code:

Code:
int main( int argc, char *argv[] )
{
// setup system shutdown handler
atexit( SDL_Quit );

// create window
Window window( 640, 400, false );
window.initialize();

// create event manager
SDL_Event event;

// create clock
Clock clock;

// main loop
while( active )
{
	// begin loop iteration
	clock.start();

	// handle events
	while( SDL_PollEvent( &event ) )
	{
		if( event.type == SDL_QUIT )
		{
			active = false;
		}
		if( event.type == SDL_KEYDOWN )
		{
			if( event.key.keysym.sym == SDLK_ESCAPE )
			{
				active = false;
			}
		}
	}
	// end loop iteration
	clock.pause();
}

// shut down
SDL_Quit();
return 0;

And the window class:

Code:
Window::Window( int widthIN, int heightIN, bool fullscreenIN )
{
width = widthIN;
height = heightIN;
fullscreen = fullscreenIN;
}
/// window destructor
Window::~Window()
{
}
/// window initialization function
void Window::initialize()
{
// initialize SDL video subsystem
if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
{
// if initialization fails, shut down
SDL_Quit();
}
// set video subsystem parameters
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );

// create OpenGL window
Uint32 video_flags = SDL_OPENGL;
display = SDL_SetVideoMode( 640, 400, 16, video_flags );
if( display == NULL )
{
	// if window creation fails, shut down
	SDL_Quit();
}

// initialize OpenGL
glEnable( GL_TEXTURE_2D );

// set OpenGL window parameters
glViewport( 0, 0, 640, 400 );
glOrtho( 0.0f, 640, 400, 0.0f, -1.0f, 1.0f) ;

// set OpenGL general parameters
glClear( GL_COLOR_BUFFER_BIT );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

// set OpenGL matrix modes
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

}

Hi.

Where is “active” defined? Might it start out as false?

I’m just wondering about your error handling code in your Window class.
SDL_Quit() just shuts SDL down, it doesn’t terminate the process or
anything. It is part of, but not a complete solution to, error handling.

Try using the SDL_GetError() function to retrieve a human readable string of
what the error was. You can print this to a log file or display it in a
message box. Then you would need to call exit(1) if the error is
non-recoverable, or throw an exception / give “initialise” a return value so
you can propagate the error back to main().

Finally, you might consider placing a debug breakpoint inside the loops (or
print statements) that can verify if they are running.

– BrianOn Sat, Oct 31, 2009 at 7:11 AM, Sigvatr wrote:

I’m trying to setup a basic SDL program but it won’t respond to quit,
keyboard events or anything. I have to open the task manager to close it.
I’m not sure what’s wrong.

Hi,

I suggest a ‘break’ statement after the ‘active=false’.
It may not explain why the app never quits but as soon as you’ve caught a quit event of any kind,
you may not want to continue reading the event queue. Moreover, supposing something is
producing SDL events continuously, then your program would never leave the “while” loop.

Also, you may replace the if(…) if(…) by a switch() … case because in your code, you’re testing for 'SDL_KEYDOWN’
even if you detected a ‘SDL_QUIT’ previously, which is a useless test to perform. A ‘break’ avoids this, and a switch…case
is even cleaner. This is not significant for catching 2 event types but if your application is growing, think about it.
Look at the examples in the SDL doc, it’s a very good place to start writing efficient code.

Also, try putting a printf/cout inside the event loop to be sure it’s the location where the program
is looping (and not in a previous function like in ‘window.initialize’).

Once you find where it is looping, fix the problem.

Regards,
Julien CLEMENT

I’m trying to setup a basic SDL program but it won’t respond to quit, keyboard events or anything. I have to open the task manager to close it. I’m not sure what’s wrong.

Here’s the code:

Code:
int main( int argc, char *argv[] )
{
// setup system shutdown handler
atexit( SDL_Quit );

// create window
Window window( 640, 400, false );
window.initialize();

// create event manager
SDL_Event event;

// create clock
Clock clock;

// main loop
while( active )
{
    // begin loop iteration
    clock.start();

    // handle events
    while( SDL_PollEvent( &event ) )
    {
        if( event.type == SDL_QUIT )
        {
            active = false;
        }
        if( event.type == SDL_KEYDOWN )
        {
            if( event.key.keysym.sym == SDLK_ESCAPE )
            {
                active = false;
            }
        }
    }
    // end loop iteration
    clock.pause();
}

// shut down
SDL_Quit();
return 0;