2013Asker
2013Asker

Reputation: 2068

Why this code is skipping points while moving the mouse fast?

I started learning the SDL library yesterday and, after some reading and asking, made a very simple program that draws a block when the left button is down.

The problem is that it skips points when the mouse is moved fast, so you get a bunch of squares instead of a line, the following screenshot shows one line made moving the mouse with regular speed and one while moving it fast:

enter image description here

What is causing it to skip all those points?

Here's the code:

//keep the window open
while(running){

    //handle events
    while(SDL_PollEvent(&event)){
        switch(event.type){ 

            case SDL_MOUSEBUTTONDOWN:
                //left button down draws black block
                if(event.button.button == SDL_BUTTON_LEFT) boxColor = black;

                //right button "erases" a point
                else
                  if(event.button.button == SDL_BUTTON_RIGHT) boxColor = blue;

                //middle button clears the screen
                else {
                    clearScreen(display,blue);
                    break;
                }

                //where to draw
                drawing = 1;
                boxRect.x = event.button.x - BOX_WIDTH / 2;
                boxRect.y = event.button.y - BOX_HEIGHT / 2;
            break;

            case SDL_MOUSEMOTION:
                //keep drawing if the button is pressed
                if(drawing == 1){
                    boxRect.x = event.motion.x - BOX_WIDTH / 2;
                    boxRect.y = event.motion.y - BOX_HEIGHT / 2;
                }
            break;

            //stop drawing when the button is no longer pressed
            case SDL_MOUSEBUTTONUP:
                drawing = 0;
            break;

            //quit if window closing button is pressed
            case SDL_QUIT:
                running = 0;
            break;

        }
    }
    //draw
    if(drawing == 1){
        SDL_FillRect(display,&boxRect,boxColor);
        SDL_Flip(display);
    }
}

Upvotes: 2

Views: 289

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

Because the system doesn't actually get the points as a continuous stream, it has to poll for the mouse position. This means that if you move the mouse too fast, the difference between two polls will be big enough for there to be a gap.

Upvotes: 3

Related Questions