0
votes

I am testing mouse events in SDL2. I want to print "button" to the console window whenever a mouse button is pressed (right, left and middle button). However, when I move my mouse without pressing any button, the message is also printed. Why is that?

while (!quit)
    {

        SDL_WaitEvent(&event);


        switch (event.type)
        {
        case SDL_QUIT:
            quit = true;
            break;

        default:

            if (event.type = SDL_MOUSEBUTTONDOWN)
            {
                std::cout << "button\n";
            }
            break;
    }
1

1 Answers

1
votes

You assign value instead of comparison. It must be:

 if (event.type == SDL_MOUSEBUTTONDOWN) // was '='

One of the ways to avoid such errors is using of Yoda notation:

if (SDL_MOUSEBUTTONDOWN == event.type)