Active Rendering & AWT eventqueue

Hi,

I’m developing a game in windowed mode with active rendering and a Swing UI. I thought that when using active rendering, AWT events were also dispatched via the active rendering loop. However it seems that the events dispatched via the awt event queue are still dispatched in their own thread??? Given that Swing and AWT aren’t thread-safe, how is this supposed to work???

There seems to be very few info on this threading issues on Sun’s documentation or the Internet.

Any idea?

Thanks

Why active rendering in windowed mode? It is not the way of the force young Skywalker :wink:

Well to have hardware-accelerated painting? :slight_smile:

1.) Well what has active rendering to do with hardware accaleration. As far as I now Swing itself renderes into a BufferStrategy … but of course if you create your own one you definitivly have more choice.

2.) Sure you can let events be delivered by the EDT and do you rendering in your rendering thread, there’s nothing wrong with that.
You just need to make sure you don’t have race conditions. E.g. in the event-listeners (running on the EDT) you can modify your data-strcucturs which are painted by the active rendering thread. Don’t forget to use synchronization.
To make things more easy (however this has other negative impacts) you could define one “lock” object which can be either held by the rendering thread or by the edt.


Object lock = new Object();
void paint()
{
while(true)
{
synchronized(lock)
{
 //Here you can access you shared data structures
}

}

public void mousePressed()
{ 
synchronized(locker)
{
//Here too :-)
}
}
}

What has active rendering todo with windowed mode or not?

lg Clemens

Well now that I think back of it (I started this project in 2003), I think that the reasons to use active rendering were threefold:

  • use best hardware acceleration possible
  • avoid synchronization issues as everything would be done in the active rendering thread
  • have a fluid animation

However it seems now that at least point two is not avoidable…

Well you can easily avoid synchronization issues by using a typical game loop


while (running){
   processFrame();
   renderFrame();
   syncWait();
}

When you render the frame you can implement frame skip - i.e. you don’t copy the frame to the screen


while (running){
   processFrame();
   renderFrame();
   syncWait();
}

But why does it avoid synchronization?

syncWait queues itself up with a high precision timer that does nothing but call notify 60 times a second. You render to the buffer that will be drawn; and before syncWait is released your graphics engine will request for the buffer to be drawn to the screen. You can use two buffers to ensure there is no flicker.

basically avoiding synch is a bad idea for the reason that you’ll always face this problem if you have more than one leading thread, which is the often case with games. But another one would let off synch to perform the tests while it will start to synchronize its code at a later time.

With Swing applications like a Text Editor or a simple FTP manager, rendering is handled by the AWT EventQueue. That is you call repaint() which will notify the queue of AWT. Then AWT sends an update() callback to the components within the default timer-rate (usually unknown).
For “Active Rendering”, I’ve just been working on it with a simple Pong-like game where the programmer choose the moment the render will occur, that is AWT is not involved. Say you want a 60Hz refresh rate, so your timer will tick at 60Hz and your program will render the scene at 60Hz. This way brings the best results, but need quite a thought before to begin coding. Yes, you define a refresh-rate, but rendering can take more time to complete and will probably go over the next timer-tick. No synch is needed for a simple game like Pong, nor the simpliest Space Invaders. But if you want to full up the scene with some of your best Sprites, you may use synchronization. That is one of the main topic for Active Rendering.

Synching two Threads is not so easy but no post will replace a simple book about this topic. Plus, active Rendering is commonly meant to use Pipelines, e.g. the Direct3D pipeline or OpenGL pipeline. You might be facing the usual hard- and soft-rendering which are quite similar.

Let’s say if I had to write a book about Java AWT I’d include this well undiscovered topic! 8)

Well let us know when this book hits the market :wink: