开发者

Unable to poll for mouse click event in SDL

开发者 https://www.devze.com 2023-03-29 23:17 出处:网络
I have the code int userinput() { while(hasquit == false) { while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT )

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

精彩评论

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