Now we can open up a window and draw simple shapes on it, it would be a good idea to be able to interact with it. Things like clicking the close button would be nice, until its coded for, a program will keep right on running and might even ignore CTRL-C in the terminal too if you are unlucky enough to have an infinite loop.
SDL_Event event;
bool running=true;
while(running) { // read the event queue
if( SDL_PollEvent( &event ) != 0 ) { // if there is an event
if(event.type==SDL_QUIT) { running=false; } // if its a quit event
}
}
This structure can be extended to handle other events, like the mouse moving over the window, clicking buttons, and also keypresses. It is however slightly confusing.
while(running) {
if( SDL_PollEvent( &event ) != 0 ) {
if (event.type==SDL_KEYDOWN) {
if (event.key.keysym.sym==SDLK_LSHIFT) { ; }
}
if (event.type==SDL_KEYUP) {
if (event.key.keysym.sym==SDLK_LSHIFT) { ; } // watch out for these
}
if (event.type == SDL_MOUSEMOTION) {
mx=event.button.x; // button is the name of
my=event.button.y; // the event structure
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
btn = event.button.button; // also containing the
if (btn==1) { ; } // actual button data
if (btn==3) { ; }
}
if (event.type == SDL_MOUSEBUTTONUP) {
btn = event.button.button;
if (btn==1) { ; }
if (btn==3) { ; }
}
}
There is an extensive list of event codes in the documentation which there is little point duplicating here.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.