Thread: Poll event loop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    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 >

    Code:
    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.

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