Unable to poll for mouse click event in SDL
I have the code
int userinput()
{
while(hasquit == false)
{
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
开发者_运维问答 {
hasquit = true;
}
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
hasquit = true;
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
//do something
}
}
}
}
}
}
which is pretty much an event structure I copied from these tutorials. I can get the SDL_QUIT and SDLK_ESCAPE events, but if I try to make
hasquit = true
with either of the mousebutton if statements, nothing happens.
You have the
if(event.type == SDL_MOUSEBUTTONDOWN)
inside the
if ( event.type == SDL_KEYDOWN )
block. It should be separate.
This should work:
int userinput()
{
while(hasquit == false)
{
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
{
hasquit = true;
}
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
hasquit = true;
}
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
//do something
}
}
}
}
}
精彩评论