views:

51

answers:

1

I'm writing a 2D game in Java and I have a problem with input events (mouse, keyboard) not firing when they should. It works as expected in win2k & xp.

As it's a game, I have a main loop which

  1. Coalesces input events (various listeners populating buffered state)
  2. Advances the scene
  3. Renders the scene
  4. Waits if necessary to maintain stable framerate (a sleep)
  5. Swaps buffers

The render of the scene hammers java2D quite heavily - it uses a BufferStrategy and intensively uses the non-scaling variant of drawImage(). The framerate is artificially locked at around 60fps for the time being.

There appears to be a delay (up to several seconds, occasionally) between any mouse or keyboard event occurring and the JVM picking it up - initially I thought it may be the event dispatch thread not getting enough time (despite the framerate sleep)- I tested this by adding a custom event containing a frame token, and waiting for it on frame end, but it fires when expected (- it should only do so after all other prior events have fired, which suggests to me it's an x-windows-to-jdk problem, as opposed to one internally within the jdk?)

What's particularly strange is that it can be mitigated if java is run with a nice of +19 (so lower priority...). It also seems to get worse if I have a more heavily populated scene- running top shows that Xorg (and not java) is taking around 80% of CPU, which I suspect is something to do with it...

I've also tried tinkering with thread priorities, but it makes no discernible difference...

I'm totally stumped on this one- has anyone else seen similar behaviour, and if so, how did you rectify it? Am I barking up the wrong tree??

I'm running 1.6.0 OpenJDK and fedora 11.

Apologies for the verbosity, and thanks in advance!

+1  A: 

I just tried re-nicing Xorg to -19 (so it's realtime) and the problem goes away...

I'm guessing java rendering lots of pixmaps through Xorg causes X itself to swamp the CPU, so be penalised by the scheduler and not have time to process events? Oddness.

Rick