3
\$\begingroup\$

The code I'm using is like so:

SDL_Event e;
SDL_PollEvent(&e);
switch (e.type)
{
    case SDL_MOUSEMOTION:
    {
        YCam += e.motion.yrel; 
    }
    break;
}

When I run the program, nothing happens, however, once I first move the mouse the SDL_MOUSEMOTION event keeps firing and the mouse keeps moving. This occurs even when I've stopped moving my mouse. Pressing any key including mouse keys stops the movement.

\$\endgroup\$
4
\$\begingroup\$

You're not checking the return value of SDL_PollEvent to see if there's a new event, which means that regardless of whether there is an event to handle, you're trying to handle one. In practice, this means that you're constantly handling the last event, even if it's no longer occurring. Try this:

SDL_Event e;
if (SDL_PollEvent(&e)) {
    switch (e.type)
    {
        case SDL_MOUSEMOTION:
        {
            YCam += e.motion.yrel; 
        }
        break;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you so much! This fixed the issue I highlighted in the question. One thing I'd like to add is that using if(SDL_PollEvent(&e)) causes the movement to lag behind when moving the mouse fast. Using while(SDL_PollEvent(&e)) seems to fix this. Any idea why? \$\endgroup\$ – LASER BEAR ASSAULT UNIT Dec 15 '18 at 11:00
  • \$\begingroup\$ @LASERBEARASSAULTUNIT while is a tighter loop; it handles all of the events before doing the rest of the stuff in that loop (which probably involves moving the camera and re-rendering). \$\endgroup\$ – wizzwizz4 Dec 15 '18 at 11:35

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.