Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  Applet main loop  (Read 4085 times)
0 Members and 1 Guest are viewing this topic.
Offline stefanha

Senior Newbie




I love YaBB 1G - SP1!


« Posted 2002-10-18 13:08:54 »

I know this must have been covered in various places in the past.  However, I would be interested in your opinions and alternatives for the "Applet main loop" problem; How to structure the main loop of a real-time applet?

I think we all more or less agree on:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public class RealTimeApplet extends Applet implements Runnable
{
Thread thread; // Runner thread

public void start () {
thread = new Thread ( this );
}

public void stop () {
thread = null;
}

public void run ()
{
while ( thread != null )
{
// run main loop
}
}

public void update ( Graphics g )
{ // override because origional cleared screen
paint ( g );
}
}


Now comes the interesting part.  How do we update logic and then repaint in the main loop.  These are some ideas/approaches I've seen:

1. Just grab the Applet's Graphics context and use it however you like (bypassing the paint() function).  This is not very friendly, and I'm not sure have supported it is by various AWT implementations.
2. In the main loop, wait() after calling repaint().  The paint() function then calls notifyAll() when it is done.  When running on a fast computer, the applet can eat up too many resources (ie. freeze the system by taking almost all time).  This means some performance manager must be implemented to avoid this :(.
3. Just use Thread.sleep (1L) at the end of the main loop.  This is kind of a hack :(.  It avoids eating too many resources though.

I would like to know what other approaches there are, because these all have some drawback(s) which I don't like.

Also, who uses ToolKit.vsync () and where should it be used.  Who uses Thread.yield () and where should it be used.

You see I have many questions ;).  Sorry guys, but I'm looking to sort this out once and for all.

Thanks.
from, Stefan
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2002-10-18 21:37:44 »

Applet animation is kind of a black art.  The shrot answer is in the limited 1.1.x world of Applets there really isn't a 'right' way to do it, those who write applet games have found all sorts of hacks to make the wrong ways function all right.

In applciations, use JDk1.4 BufferStratagey  to get the graphics to paint do and do all your processing and painting in a bgi loop in a single thread (totally bypassing AWT paint() ), thats the way "real' games have done it for a long long time in other languages.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Chaoslab

Junior Newbie




Mutant


« Reply #2 - Posted 2002-10-19 08:51:07 »

Well regarding 1.1 stuff....

I always set the thread priority to min else the applet will hog to much os time when the applet is not focus on WinNT / Win2K boxs.

And then just make empty update / paint methods and grab store a Graphics ( g_ref = this.getGraphics )reference in the applet init() method to use for drawing to the applet canvas, and when image builder store use an MemoryImageSource and newPixels the image (and if you are not using the alpha channel which is done by dither and looks ugly use a color mode that is only 24 bit ( DirectColorModel  cmr_img_cm = new DirectColorModel(32,0xff0000,0xff00,0xff); ) and use that when you create the MemoryImageSource and the image gets built about 30% faster!

Smiley hope that helps....


Regards
 Cam Mckechnie http://www.eldian.com
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 (12 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (182 views)
2014-04-01 02:16:10
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

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!