Why is my event loop only looping when I have an event?

Hi, I have a main event loop which also paints my screen. I’ve just noticed that my screen only paints when some sort of event is received so it appeared that SDL_PollEvent is blocking… but… I also tried a loop above the code below which just calls DrawGLScene()/SDL_GL_SwapBuffers()/SDL_Delay and the same thing is happening.

Now I am using an audio callback as well. The reason I have never noticed this until now is that I have been so busy working on my audio that I’ve been ignoring my screen painting until now.

Does anybody know why my calls to DrawGLScene() are only being called when I have events please??

Code:
SDL_Event event;
BOOL running = TRUE;

/*
// Same thing…
while( running )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
*/

while( running )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

    case SDL_QUIT:
      running = false;
      break;
  }

  DrawGLScene();
  SDL_GL_SwapBuffers();
}

SDL_Delay(1);

}

I’m assuming that DrawGLScene() is blocking, and not SDL_PollEvent() - if
you can post that code, we might be able to help you out a bit more.
-AlexOn Mon, Sep 19, 2011 at 2:57 PM, SparkyNZ wrote:

**
Hi, I have a main event loop which also paints my screen. I’ve just noticed
that my screen only paints when some sort of event is received so it
appeared that SDL_PollEvent is blocking… but… I also tried a loop above
the code below which just calls DrawGLScene()/SDL_GL_SwapBuffers()/SDL_Delay
and the same thing is happening.

Now I am using an audio callback as well. The reason I have never noticed
this until now is that I have been so busy working on my audio that I’ve
been ignoring my screen painting until now.

Does anybody know why my calls to DrawGLScene() are only being called when
I have events please??

Code:

SDL_Event event;

BOOL running = TRUE;

/*
// Same thing…
while( running )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
*/

while( running )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

    case SDL_QUIT:
      running = false;
      break;
  }

  DrawGLScene();
  SDL_GL_SwapBuffers();
}

SDL_Delay(1);

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You haven’t given us the implementation of DrawGLScene.? First thing that occurs to me is, are there any event checking calls in there?________________________________
From: pj74@xtra.co.nz (SparkyNZ)
Subject: [SDL] Why is my event loop only looping when I have an event?

Hi, I have a main event loop which also paints my screen. I’ve just noticed that my screen only paints when some sort of event is received so it appeared that SDL_PollEvent is blocking… but… I also tried a loop above the code below which just calls DrawGLScene()/SDL_GL_SwapBuffers()/SDL_Delay and the same thing is happening.

Now I am using an audio callback as well. The reason I have never noticed this until now is that I have been so busy working on my audio that I’ve been ignoring my screen painting until now.

Does anybody know why my calls to DrawGLScene() are only being called when I have events please??

Code:
? ?SDL_Event event;
? BOOL? ? ? running = TRUE;

/*
? // Same thing…
? while( running )
? {
? ? DrawGLScene();
? ? SDL_GL_SwapBuffers();
? ? SDL_Delay(1);
? }
*/

? while( running )
? {
? ? while( SDL_PollEvent( &event ) )
? ? {
? ? ? switch( event.type )
? ? ? {
? ? ? ? case SDL_KEYUP:
? ? ? ? case SDL_KEYDOWN:
? ? ? ? case SDL_MOUSEBUTTONDOWN:
? ? ? ? case SDL_MOUSEMOTION:
? ? ? ? ? …
? ? ? ? ? break;

?
? ? ? ? case SDL_QUIT:
? ? ? ? ? running = false;
? ? ? ? ? break;
? ? ? }

? ? ? DrawGLScene();
? ? ? SDL_GL_SwapBuffers();
? ? }

? ? SDL_Delay(1);
? }


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Code looks OK, assuming DrawGLScene() doesn’t take unusually long.

Try replacing DrawGLScene() with something that does glClear() to a
different color each frame. Maybe rand() color or a smoothly increasing
gradient like:

loop() {
static float x = 0.0f;
static float dir = 1.0f;

x += dir * 0.00125f;

if(x > 1.0) {
x = 1.0;
dir = -dir;
}

if(x < 0.0) {
x = 0.0;
dir = - dir;
}

glClearColor(x, 0, 0, 1);
glClear( GL_COLORBUFFER_BIT );

}On Mon, Sep 19, 2011 at 1:57 PM, SparkyNZ wrote:

**
Hi, I have a main event loop which also paints my screen. I’ve just noticed
that my screen only paints when some sort of event is received so it
appeared that SDL_PollEvent is blocking… but… I also tried a loop above
the code below which just calls DrawGLScene()/SDL_GL_SwapBuffers()/SDL_Delay
and the same thing is happening.

Now I am using an audio callback as well. The reason I have never noticed
this until now is that I have been so busy working on my audio that I’ve
been ignoring my screen painting until now.

Does anybody know why my calls to DrawGLScene() are only being called when
I have events please??

Code:

SDL_Event event;

BOOL running = TRUE;

/*
// Same thing…
while( running )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
*/

while( running )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

    case SDL_QUIT:
      running = false;
      break;
  }

  DrawGLScene();
  SDL_GL_SwapBuffers();
}

SDL_Delay(1);

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

[…]

while( running )
{
while( SDL_PollEvent( &event ) )
{

[…]

  DrawGLScene();
  SDL_GL_SwapBuffers();
}

SDL_Delay(1);

}

Well, this will of course only update once per received event - but why the
other version behaves the same beats me… As others have suggested, sounds
like you have event processing calls in DrawGLScene() as well.On Monday 19 September 2011, at 20.57.42, “SparkyNZ” wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Hello !

Does anybody know why my calls to DrawGLScene() are only being called when I have events please??

Code:
SDL_Event event;
BOOL running = TRUE;

/*
// Same thing…
while( running )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
*/

while( running )
{
while( SDL_PollEvent(&event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

     case SDL_QUIT:
       running = false;
       break;
   }

   DrawGLScene();
   SDL_GL_SwapBuffers();
 }

Simple, because it is inside the while (PollEvent Loop.

 SDL_Delay(1);

}

Do it that way :

while( running )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

     case SDL_QUIT:
       running = false;
       break;
   }
  }
   
  DrawGLScene();
  SDL_GL_SwapBuffers();
 
 SDL_Delay(1);

}

Hello !

Does anybody know why my calls to DrawGLScene() are only being called when

I have events please??

Code:
SDL_Event event;
BOOL running = TRUE;

/*
// Same thing…
while( running )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
*/

while( running )
{
while( SDL_PollEvent(&event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

    case SDL_QUIT:
      running = false;
      break;
  }

  DrawGLScene();
  SDL_GL_SwapBuffers();
}

Simple, because it is inside the while (PollEvent Loop.

He did a loop with just DrawGLScene() / SwapBuffers(), Delay() that did the
same thing. It might be the OS that doesn’t redraw unless events are
handled, though (“not responding” in Win32).On Mon, Sep 19, 2011 at 2:45 PM, Torsten Giebl wrote:

 SDL_Delay(1);

}

Do it that way :

while( running )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:

break;

     case SDL_QUIT:
     running = false;
     break;
 }
}
      DrawGLScene();
SDL_GL_SwapBuffers();
   SDL_Delay(1);

}
_____________**
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/**listinfo.cgi/sdl-libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks for all your replies everybody. I’ve tried DrawGLScene() using Patrick’s suggestion and the same thing is happening - that is, I have to move the mouse around or press buttons before the color starts to change…

Code:
void DrawGLScene( void )
{
static float x = 0.0f;
static float dir = 1.0f;

x += dir * 0.00500f;

if(x > 1.0) {
x = 1.0;
dir = -dir;
}

if(x < 0.0) {
x = 0.0;
dir = - dir;
}
glClearColor(x, 0, 0, 1);
glClear( GL_COLOR_BUFFER_BIT );
}

But… the below tight loop works now… So perhaps when I tried the tight loop earlier I had some other problem with my own code.

Code:
while( 1 )
{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}

So… obviously I want to get rid of the tight loop above. I thought SDL_PollEvent() was just that - a poll for an event rather than a “wait for event” like Windows GetMessage(). Should I be using SDL_PeepEvents() instead to keep the loop going around when there are no events?

My program isn’t actually a game so in all honesty it doesn’t need to thrash around the main loop. I could actually post a user event to do the updates when required but I would like to get my head around how to do a proper game loop. I honestly thought my SDL_PollEvent implementation was OK but perhaps not…??

Torsten Giebl wrote:

Hello !
Simple, because it is inside the while (PollEvent Loop.

Torsten! You are correct! :slight_smile: The annoying thing is that I did notice this earlier on and tried it too and it still didn’t work… but… if I found the tight loop wasn’t working for me either, then clearly there is something else wrong in my DrawGLScene code.

So Torsten - you have actually answered my second question too (ie. whats the correct way to do a game loop with SDL_PollEvent)…

Thanks for all your help guys. You’re all awesome!

I recently ported an SDL-based game to the Wii and noticed that it called
SDL_PollEvent(NULL), which is a non-blocking call that simply returns 1 if
an event is pending, or 0 if not.

You might want to try using this trick to skip the blocking
SDL_PollEvent(&event) call when no event is pending.On Mon, Sep 19, 2011 at 1:24 PM, SparkyNZ wrote:

**
Thanks for all your replies everybody. I’ve tried DrawGLScene() using
Patrick’s suggestion and the same thing is happening - that is, I have to
move the mouse around or press buttons before the color starts to change…

Code:

void DrawGLScene( void )

{
static float x = 0.0f;
static float dir = 1.0f;

x += dir * 0.00500f;

if(x > 1.0) {
x = 1.0;
dir = -dir;
}

if(x < 0.0) {
x = 0.0;
dir = - dir;
}
glClearColor(x, 0, 0, 1);
glClear( GL_COLOR_BUFFER_BIT );
}

But… the below tight loop works now… So perhaps when I tried the tight
loop earlier I had some other problem with my own code.

Code:

while( 1 )

{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}

So… obviously I want to get rid of the tight loop above. I thought
SDL_PollEvent() was just that - a poll for an event rather than a “wait for
event” like Windows GetMessage(). Should I be using SDL_PeepEvents() instead
to keep the loop going around when there are no events?

My program isn’t actually a game so in all honesty it doesn’t need to
thrash around the main loop. I could actually post a user event to do the
updates when required but I would like to get my head around how to do a
proper game loop. I honestly thought my SDL_PollEvent implementation was OK
but perhaps not…??


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Also, SDL_PollEvent() is supposed to always be non-blocking. SDL_WaitEvent()
is supposedly a blocking version. I’m not sure why SDL_PollEvent() is
hanging on you when there are no events.On Mon, Sep 19, 2011 at 2:03 PM, Benjamin Shadwick <@Benjamin_Shadwick>wrote:

I recently ported an SDL-based game to the Wii and noticed that it called
SDL_PollEvent(NULL), which is a non-blocking call that simply returns 1 if
an event is pending, or 0 if not.

You might want to try using this trick to skip the blocking
SDL_PollEvent(&event) call when no event is pending.

On Mon, Sep 19, 2011 at 1:24 PM, SparkyNZ wrote:

**
Thanks for all your replies everybody. I’ve tried DrawGLScene() using
Patrick’s suggestion and the same thing is happening - that is, I have to
move the mouse around or press buttons before the color starts to change…

Code:

void DrawGLScene( void )

{
static float x = 0.0f;
static float dir = 1.0f;

x += dir * 0.00500f;

if(x > 1.0) {
x = 1.0;
dir = -dir;
}

if(x < 0.0) {
x = 0.0;
dir = - dir;
}
glClearColor(x, 0, 0, 1);
glClear( GL_COLOR_BUFFER_BIT );
}

But… the below tight loop works now… So perhaps when I tried the tight
loop earlier I had some other problem with my own code.

Code:

while( 1 )

{
DrawGLScene();
SDL_GL_SwapBuffers();
SDL_Delay(1);
}

So… obviously I want to get rid of the tight loop above. I thought
SDL_PollEvent() was just that - a poll for an event rather than a “wait for
event” like Windows GetMessage(). Should I be using SDL_PeepEvents() instead
to keep the loop going around when there are no events?

My program isn’t actually a game so in all honesty it doesn’t need to
thrash around the main loop. I could actually post a user event to do the
updates when required but I would like to get my head around how to do a
proper game loop. I honestly thought my SDL_PollEvent implementation was OK
but perhaps not…??


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

HunterZ wrote:

Also, SDL_PollEvent() is supposed to always be non-blocking. SDL_WaitEvent() is supposedly a blocking version. I’m not sure why SDL_PollEvent() is hanging on you when there are no events.

I don’t believe it is hanging - I think there’s some bad code in my drawing routine somewhere. Everything works fine with the test DrawGLScene code I tried from Patrick.

Cheers