[SDL] Input events problem (possibly something is wrong with the threads)

Started by
10 comments, last by paul_nicholls 17 years, 8 months ago
I have a huge problem I can not solve myself. It appears like event queue is not filled on every SDL_PumpEvents() or SDL_PoolEvent() function call. Keydown events appear on queue about 1 or 2 seconds after key was pressed. Like events were buffered somewhere, but when in main loop program sleeps for a short period of time, for example SDL_Delay(50), everything is OK.

while( !quit )
{
  //do scene rendering
  ...
  SDL_Delay(50); //why?

  while( SDL_PollEvent( &event ) )
  {
    switch( event.type )
    {
      //manage events
      ...
    }
  }
}


Maybe someone could tell me why is that? How can I prevent this? Maybe thread responsible for input gets no time for computations, because it is starved by rendering? How can I force event queue to be filled? I've written simple code in C++ using OpenGL and SDL on linux. Please see here: http://skowron.no-ip.org/kuba/gl/ There is a short source code, Makefile and linux binary. When you run this program one triangle should constantly rotate, and another triangle should rotate on mouse move. Mouse moves and keydowns appears late, unless you add SDL_Delay() in main loop. Thanks.
Advertisement
I tried out your program on my computer and i ran just fine... I have some suggestions for your source though that might make it run better for you:

* Check for errors from SDL_init and SDL_SetVideoMode
* Do the drawing and buffer swaps AFTER event polling
- Jezper Blixt
You say it works fine? I've tried this on two (fast) computers under SuSE and Knoppix, and the problem was present.

I don't know what to do with this, maybe someone else will come across this problem.

(I intentionally stripped the code from error checking for simplicity. There were no errors on my machine.)

Thanks a lot.
Did you try to move your rendering code after you check for events? I'm quite possitive that would solve your problems.
- Jezper Blixt

I've tried to move my rendering code. And I've also tried event checking using SDL_PumpEvents() and SDL_PeepEvents(), and also I've tried getting keyboard input by SDL_GetKeyState(). Always the same problem, input works, but works with lags, while rendering is smooth.

In every case SDL_Delay() on about 50 or 100 miliseconds solved input problems. I don't know -- maybe some thread synchronization is needed? maybe this problem lies in SDL libraries on my computer?

-- kskowron
On multitasking operating systems such as Linux, sometimes the device drivers for keyboard handling will be delayed if your program doesn't yield a timeslice with an SDL_Delay(0); to let the XServer and other processes have the CPU time.

This would also be a problem on the Amiga since the console.device is a separate task from everything else and may not have enough task priority to preempt the foreground task with a timer-based interrupt.

The lesson of the matter is always put an SDL_Delay(0); in your main loop to yield a timeslice to the operating system so it can update itself in a timely fashion.
Everything would be OK, but unfortuatly on my computer input works fine only when program sleeps in main loop for at least 20 or 50 milisecond. I've tried SDL_Delay(0), but it just don't have any effect, I don't know why. I have installed the newest SDL 1.2.11, but still I have the same problem.

This SDL_PollEvent() behavior is very wierd. Maybe it waits until some kind of buffer is filled up?

Anyone noticed anything like that on his machine?

Quote:Original post by kskowron

I have a huge problem I can not solve myself.
It appears like event queue is not filled on every SDL_PumpEvents() or SDL_PoolEvent() function call. Keydown events appear on queue about 1 or 2 seconds after key was pressed.
Like events were buffered somewhere, but when in main loop program sleeps for a short period of time, for example SDL_Delay(50), everything is OK.

*** Source Snippet Removed ***

Maybe someone could tell me why is that? How can I prevent this?

Maybe thread responsible for input gets no time for computations, because it is starved by rendering?
How can I force event queue to be filled?

I've written simple code in C++ using OpenGL and SDL on linux. Please see here:
http://skowron.no-ip.org/kuba/gl/

There is a short source code, Makefile and linux binary.

When you run this program one triangle should constantly rotate, and another triangle should rotate on mouse move. Mouse moves and keydowns appears late, unless you add SDL_Delay() in main loop.

Thanks.


I don't seem to have that issue with my SDL loop code. Here is mine so you can compare:

Procedure TSDL_Application.RunMainLoop;
Var
Event: TSDL_Event;
LastTicks: UInt32;
ThisTicks: UInt32;
TimeSlice: Single;
Begin
If(FKeepCursorCentred)Then
SDL_WarpMouse(FWidth Div 2, FHeight Div 2);

LastTicks := SDL_GetTicks;

TimeSlice := 1/60;

FFPSCount := 0;

FIsRunning := True;

While(FIsRunning)do
Begin
If(SDL_PollEvent(@Event) <> 0)Then
Begin
Case Event.type_ Of
SDL_QUITEV : FIsRunning := False;
SDL_KEYDOWN : HandleKeyDown (@Event.key.keysym, FIsRunning);
SDL_KEYUP : HandleKeyUp (@Event.key.keysym, FIsRunning);
SDL_MOUSEBUTTONUP : HandleMouseUp (Event.button, FIsRunning);
SDL_MOUSEBUTTONDOWN : HandleMouseDown (Event.button, FIsRunning);
SDL_MOUSEMOTION : HandleMouseMotion(Event.motion, FIsRunning);
// If there is a resize event.
SDL_VIDEORESIZE : Begin
// Request SDL to resize the window to the size -
// - and depth etc. that we specify.
If ( SDL_SetVideoMode( event.resize.w,
event.resize.h,
FBitsPerPixel,
FVideoFlags ) = Nil) Then
Begin
// Exit if there is an error.
FIsRunning := False;
End
Else
// Resize the OpenGL viewport.
If(FOpenGLEnabled)Then
ResizeGLViewport( event.resize.w, event.resize.h );
End;
End;
End;

// draw the scene
DrawScene(TimeSlice);

Case FOpenGLEnabled Of
False : SDL_Flip(FSurface);
True : SDL_GL_SwapBuffers;
End;

ThisTicks := SDL_GetTicks;

TimeSlice := (ThisTicks - LastTicks) * Inverse1000;

LastTicks := ThisTicks;
End;
End;

I don't know if this will help, but I hope so :)

Cheers,
Paul.
AHHRRRGGG!! How does one nicely format code when posting here?

Paul.
Quote:Original post by paul_nicholls
AHHRRRGGG!! How does one nicely format code when posting here?

Paul.


It's listed in the forum faq. Look under the search bar in the upper right-hand corner of the page between the search button and stats button. BTW I don't think the source tag for the forum works on Pascal.

This topic is closed to new replies.

Advertisement