开发者

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
                }
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜