为什么此代码在快速移动鼠标时跳过点?

时间:2013-08-09 12:06:42

标签: c drawing sdl

我昨天开始学习SDL库,经过一些阅读和询问,制作了一个非常简单的程序,当左键按下时,它会绘制一个块。

问题在于,当鼠标快速移动时它会跳过点,所以你得到一堆正方形而不是一条线,下面的屏幕截图显示了一条线以常规速度移动鼠标,一条移动快速移动鼠标: / p>

enter image description here

是什么导致它跳过所有这些点?

以下是代码:

//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);
    }
}

1 个答案:

答案 0 :(得分:3)

因为系统实际上并不将点作为连续流获取,所以它必须轮询鼠标位置。这意味着如果你移动鼠标太快,两次民意调查之间的差异将足够大,以便有一个间隙。