Thread: Poll event loop

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK

    Poll event loop

    it was confusing me how the snippet below worked, i have stepped through it and would like to know if i am now right about how it works >

    while(Mainloop == 0)
        SDL_Event event;
        while ( SDL_PollEvent(&event) )                
          if ( event.type == SDL_QUIT )  {  Mainloop = 1;  }
          if ( event.type == SDL_KEYDOWN )
            if ( event.key.keysym.sym == SDLK_ESCAPE ) { Mainloop = 1; }
           if( event.type == SDL_MOUSEBUTTONDOWN ) {  newscene = true; }
      newscene = DrawScene(screen, newscene);
    as i understand it >
    control enters the 1st while loop, an object 'event' is created,

    the second while loop condition is tested by calling function SDL_PollEvent() to see if any event has occured,
    if true it checks to see if the event is any of the kinds i have included.
    it then drops back into the first while loop, if no event occurs the second while loop is ignored, etc etc.

    my problem understanding really is that when an event occurs i watch the inner loop go around a few times before exiting, whether a condition in the loop is met or not, why does this happen? How come it does not just drop out straight away once the IF statements are checked ?
    the description in the header file for this function is as follows >

    extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event *event);
    Polls for currently pending events, and returns 1 if there are any pending
    events, or 0 if there are none available. If 'event' is not NULL, the next
    event is removed from the queue and stored in that area.
    i mean if it finds an event has occured the condition is 'true', fair enough, but i dont see how that condition lasts for more than one iteration, because next time around it again calls SDL_PollEvent(); but nothing will have happend that time?
    Last edited by rogster001; 09-17-2009 at 02:12 AM.

  2. #2
    Registered User
    Join Date
    Aug 2009
    It will execute inner while loop until SDL_PollEvent(&event) is false.

    In the If conditions you are assigning values, you are not branching out.

    BTW, are you sure that event.type will always be one of {SDL_QUIT, SDL_KEYDOWN, SDL_MOUSEBUTTONDOWN}

    Last edited by 14341; 09-17-2009 at 03:10 AM.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK


    i see that, this is why i cannot understand, if i click on the mouse button and step through with the debugger i see it pickup the mouseclick but then proceeds to do four more loops before exiting into the mainloop, why does this happen when the mouse event is over? or rather th way i see it, a click of the mouse button on the screen = true event so do whatever, then go back to the top of the loop, check, the mouse button is no longer pressed and nothing else is happening so why does it not just leave it there?

    event type will always be one of etc...

    at the moment thats all required yes, its just an add on really. I might need more and then will probably regret not building them in straight away of course..

    i could not get the quote tags to work at all here..
    Last edited by rogster001; 09-17-2009 at 04:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM