Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Active Rendering & AWT eventqueue  (Read 2469 times)
0 Members and 1 Guest are viewing this topic.
Offline galapiat

Senior Newbie




Java games rock!


« Posted 2007-06-27 10:04:01 »

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

--
Christophe Keller
http://kec.inter-land.net
Offline keldon85

Senior Member


Medals: 1



« Reply #1 - Posted 2007-06-27 10:45:02 »

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

Offline galapiat

Senior Newbie




Java games rock!


« Reply #2 - Posted 2007-06-27 13:21:42 »

Well to have hardware-accelerated painting?  Smiley

--
Christophe Keller
http://kec.inter-land.net
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Linuxhippy

Senior Member


Medals: 1


Java games rock!


« Reply #3 - Posted 2007-06-27 13:58:55 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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 :-)
}
}
}


Quote
Why active rendering in windowed mode? It is not the way of the force young Skywalker
What has active rendering todo with windowed mode or not?

lg Clemens
Offline galapiat

Senior Newbie




Java games rock!


« Reply #4 - Posted 2007-06-27 15:13:20 »

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

--
Christophe Keller
http://kec.inter-land.net
Offline keldon85

Senior Member


Medals: 1



« Reply #5 - Posted 2007-06-27 18:46:26 »

Well you can easily avoid synchronization issues by using a typical game loop
1  
2  
3  
4  
5  
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

Offline Linuxhippy

Senior Member


Medals: 1


Java games rock!


« Reply #6 - Posted 2007-06-27 20:38:56 »

1  
2  
3  
4  
5  
while (running){
   processFrame();
   renderFrame();
   syncWait();
}

But why does it avoid synchronization?
Offline keldon85

Senior Member


Medals: 1



« Reply #7 - Posted 2007-06-27 20:47:54 »

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.

Offline broumbroum

Junior Member





« Reply #8 - Posted 2007-06-27 21:03:32 »

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! Cool

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline galapiat

Senior Newbie




Java games rock!


« Reply #9 - Posted 2007-06-28 15:21:36 »

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

Well let us know when this book hits the market  Wink

--
Christophe Keller
http://kec.inter-land.net
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (48 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!