SDL_PollEvent in a loop? why???

Started by
18 comments, last by rip-off 12 years ago
The quwestion is, why do i need the inner while loop. That's the code:

while( !done ) {
        SDL_Event event;

        while ( SDL_PollEvent(&event) ) {  //WHY THIS LOOP?
        if ( event.type == SDL_QUIT ) 
            done = true;
        if ( event.type == SDL_KEYDOWN )
            if ( event.key.keysym.sym == SDLK_ESCAPE ) done = true; 
        }
    
        keys = SDL_GetKeyState(NULL);
        
        if ( keys[SDLK_UP] )    ypos -= 1;
        if ( keys[SDLK_DOWN] )  ypos += 1;
        if ( keys[SDLK_LEFT] )  xpos -= 1;
        if ( keys[SDLK_RIGHT] ) xpos += 1;

        DrawScene();
    }
 
if i press the key arrows something moves in the screen. Why do we need the loop? Basicaly, we does it work? According to my reasoning it should work. And that's why: Suppose that i move the mouse all the time, then an event will be generated all the time, and the inside loop will never end, because every time a new event is generated, it it placed in the queue and PollEvent returns 1. That means that i can't get the state of the keybord, which means i can not move the image. However it works. Why? if you move the mouse (or do something similar) all the time ( and press the arrow key at the same time) the inside loop should be endless. However it is not. Why???? thanks [edited by - sdlprorammer on May 26, 2004 2:27:54 PM]
Advertisement

Well... suppose for example that the player was mashing 2 keys down per frame and you did not have the inner while loop. Every frame, 2 events would be appended to the queue, but only 1 would be taken off. If this happened, your game would get farther and farther behind on it's input processing. The while loop is there to process every event that has occured since the last frame, not just the first event that happens to be in the queue. I hope this helps to answer your question...


[edited by - evilsteve on May 26, 2004 3:16:31 PM]
I have a relating question.

How do you use SDL_PumpEvent() within your gameloop?
It doesn''t return anything nor does it accepts any arguements but yet its explicitly called by SDL_PollEvent(SDL_Event);

EvilSteve, ok it makes more sence now. Not for the particular example, but in another situation placing it in a loop would help. However the question about the endless loop is still there. Why the loop is not endless according to what i have said in the first post??
thanks

Vumaq,

I think that in most cases, you do not need to use SDL_PumpEvent, as it is used by SDL internally. It looks like SDL_PumpEvent does the work of querying the input devices, building the SDL_event structures, and placing them on the queue that is accessed by SDL_PollEvent. From the documentation, it sounds like the only time you would need to use SDL_PumpEvent yourself is if you were processing events using an "event filter" (see SDL_SetEventFilter) rather than SDL_PollEvent.
Because you don''t have an input device that can generate events fast enough to lock it up.
what about my wuestion ?

Travis is correct, but this is difficult to explain...

I believe the mouse itself is limited in how many events it can send to the computer in a certain amount of time. To see this, open up MSPaint, select the pencil tool, and draw a sharp curve as quickly as you can. You will notice that the curve is not smooth and is instead made up of several line segments. The mouse only had enough time to send as many movement events to the computer as there are line segments in that curve. If you had drawn the curve more slowly, there would be time for more mouse movement events to be sent to the computer, and therefore more line segments in the curve. So the inner loop will never loop indefinitly because the mouse itself is limited in how many events it can send per frame.


[edited by - evilsteve on May 26, 2004 3:50:40 PM]
SDL may have more than one event to return. For example, if I press "up arrow" and "left mouse button" at the same time, that is two events. In order to get all the information about every event that occured you must call SDL_PollEvents(...) until it returns 0, meaning that it has gotten every event.
ok thanks for all the help
One more question:
if i press 2 keys, and click my mouse too. In the event queue i''ll have trhee events waiting right?, or just the last one? I think i''ll have all the events. Am i right or wrong? If i am right, then i don''t need the inner loop cause the queue contains all the events...
so?

This topic is closed to new replies.

Advertisement