SDL_PollEvent() skips events

290 Views Asked by At

Very Simple program, for drawing on screen (like with pen in Paint)

I'm using SDL 1.2. Only events I'm proccesing are mouse motion, mouse left click and quiting program. My problem is that SDL_MOUSEMOTION events are 'skipped' when I'm moving mouse fast (when I say fast I mean faster than 1 pixel/second)

Why this happens?

screen shots:

http://postimg.org/image/gcb87v9zr/

http://postimg.org/image/i5e4w6v6f/

#include <SDL/SDL.h>

SDL_Event event;
SDL_Surface* screen;
bool clicked = false;
Uint32 whiteColor;
int W = 200; // screen width
int H = 200; // screen height
Uint32* screenPixels;

bool handleInput();

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(W,H,32,SDL_SWSURFACE);
    whiteColor = SDL_MapRGB(screen->format,255,255,255);

    screenPixels = (Uint32*) screen->pixels;

    while(handleInput())
    {

    }
    SDL_Quit();
    return 0;
}

bool handleInput()
{
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:{
                return false;
                break;
            }
            case SDL_MOUSEBUTTONDOWN:{
                clicked = true;
                break;
            }
            case SDL_MOUSEBUTTONUP:{
                clicked = false;
                break;
            }
            case SDL_MOUSEMOTION:{
                if(clicked)
                {
                    int P_x = event.motion.x;
                    int P_y = event.motion.y;
                    screenPixels[P_y * W + P_x] = whiteColor;
                    SDL_Flip(screen);
                }
                break;
            }
        }
    }
    return true;
}
1

There are 1 best solutions below

2
vladon On BEST ANSWER

It is because you SDL_Flip for all your screen, it takes too many time.

Better call SDL_Flip in another thread (using std::async, for example).

Or you may update not the all surface, but only the part which is changed color to white.