I am trying to learn SDL in C. The tutorial I found online is http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php. I'm running Ubuntu in VMware.
I downloaded the sample code(.cpp files) from the tutorial website. The C++ files worked perfectly. However, if I change the C++ to C files with a bit modification, the program can no longer detect any events. (This is a school project that requires to program in C).
The main function including the initialization of SDL_event:
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
//This is the loop that the program got stuck on:
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) )
{
//The program never comes here
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}
//Apply the image
SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
}
}
}
//Free resources and close SDL
close();
return 0;
}
The program is supposed to look for a user input of closing the window. The SDL_PollEvent was always 0, though I did click the 'X' in the window. And the program did not respond any more after the first click. The CPU usage was quite high before I killed the program. This does not only happen to closing the window, any actions that require SDL_PollEvent will result in the program non-response.
The command I used to compile is:
gcc -o test test.c `sdl2-config --cflags --libs`
Since c++ worked fine, I'm wondering if I missed any changes in C which made SDL_PollEvent not working? or the command line I used to compile was not correct? I have googled for several hours but still could not find the answer. I will be much appreciated it if someone can save me. Thank you!