0
\$\begingroup\$

Running Ubuntu 11.04, SDL 1.2 trying to pickup mouse wheel up/down movement with this (stripped down) code:

int main( int argc, char **argv )
{
    SDL_MouseButtonEvent *mousebutton = NULL;

    while ( !done )
    {
        if(mousebutton != NULL && mousebutton->button == SDL_BUTTON_LEFT)
            yrot += 0.75f;
        else if(mousebutton != NULL && mousebutton->button == SDL_BUTTON_RIGHT)
            yrot -= 0.75f;
        else if(mousebutton != NULL && mousebutton->button == SDL_BUTTON_WHEELUP){
            xrot += 0.75f;
        }else if(mousebutton != NULL && mousebutton->button == SDL_BUTTON_WHEELDOWN){
            xrot -= 0.75f;
        }

        while ( SDL_PollEvent( &event ) )
        {
            switch( event.type )
            {
                case SDL_MOUSEBUTTONDOWN:
                    mousebutton = &event.button; 
                    break;
                case SDL_MOUSEBUTTONUP:
                    mousebutton = NULL;
                    break;
                default:
                    break;
             }
        }
    }
    return 0;
}

strange thing is, scrolling with the mouse button does nothing, but if I hold down a mouse button or two and then move the mouse it hits the SDL_BUTTON_WHEEL code occasionally. This honestly reeks of a pointer issue, which would make sense since I've been spoiled with C# for the past couple years, but I am just not seeing it.

How do i correctly find mouse scroll events in SDL?

\$\endgroup\$
3
\$\begingroup\$

I'd recommend going through these two tutorials:

If you still have problems after going through these tutorials, let me know - I'd be glad to help you out :)

\$\endgroup\$
  • \$\begingroup\$ Assassin: This code was hacked together from a NeHe tutorial, those LazyFoo tutorials, and some API documentation. I will definitely go through those two tutorials again and see if it sheds some light, after work. \$\endgroup\$ – Chris Mar 30 '12 at 12:44
  • \$\begingroup\$ Aha! that was an insidious bug. For [mouse wheels] (www.libsdl.org/cgi/docwiki.cgi/SDL_MouseButtonEvent) it places two events into the event queue. SDL_PollEvent reads until the queue is empty, so my code was hitting the inner loop twice, setting wheel button to null before i hit the yrot code. \$\endgroup\$ – Chris Mar 30 '12 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.