Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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, Swing, custom threading, peripherals  (Read 1221 times)
0 Members and 1 Guest are viewing this topic.
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Posted 2006-03-06 20:35:43 »

I have seen a lot of people troubled by the stuff mentioned in the topic, and while I understand the problems entirely I won't claim to have any particularly viable solution. A forum search reveals a number of threads which basically say 'you can do this that way', but I have not been able to find any explicit solutions to the stuff presented below. Sorry if it has nonetheless been rehashed indefinitely already.

As you all know, most games work with only one main thread and executes everything from there. In most games there are some performance-critical parts, such as a main display, which may be actively rendered. However there are frequently information panels that would benefit from a more swing-like model, and in this context it becomes difficult to avoid threading problems. Another point is that mouse and keyboard input managed through eventlisteners generally run from the event dispatch thread.

I believe JInput offers a solution to the peripheral input problem. But if you wish to use Swing components, it is still difficult to avoid trouble unless the Event Dispatch Thread is used as the main thread for both logic and any mix of passive or active rendering. As far as I can see, doing everything from the Event Dispatch Thread is the only easy solution, but I have the feeling that this is not a common thing to do.

Another option is to get all peripheral input asynchronously, like I assume JInput would - or possibly by manually queueing all of it for execution in the other Thread. Now, the ability to perform passive rendering is - as far as I can see - lost.

Which structure do you guys use? Do you have any rants or opinions regarding this topic? What is the square root of 517? And what is the meaning of life?
Offline SluX

Junior Devvie





« Reply #1 - Posted 2006-03-07 10:55:40 »

Well using event dispatch thread i modify states of global variables: e.g. keys[600]-this can store all key strokes. Also i store mouse actions inside booleans.

Then in that main thread you've mentioned i poll for states of these keys-.e.g isPressed(KeyEvent.VK_LEFT) -then all logic....

I guess that it solves the problem to some point-there are still 2 threads- but hey-u also have garbage collector thread(3)- u cant escape them in Java...
You should check out Andrew Davidson's book "Killer game programming in Java"-there are good examples.

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #2 - Posted 2006-03-07 11:25:46 »

Quote
As far as I can see, doing everything from the Event Dispatch Thread is the only easy solution, but I have the feeling that this is not a common thing to do.
Swing is not thread save. doing stuff on the Event Dispatch Thread is not only common, sun even advises it.

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
http://java.sun.com/developer/JDCTechTips/2005/tt0419.html#1
etc

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2006-03-07 23:18:24 »

You can go to active rendering to get control of the render in a game-like manner.

Mike Martak's notes on it remain the definitive reference/tutorial:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

You can even use Swing compnents ina ctive render as long as you disable automatci rendering (tehre is call for that on Component,I forget what it is but I believe it is mentioned in the tutorial above.

After that, you paint by calling the paint() method directly.

Note that thsi isnt used a  lot. msot games woudl ratehr do their own UIs anyway, so there are probably lots of little details youll need to work thorugh if you want to do it.

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 Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #4 - Posted 2006-03-08 00:28:52 »

Quote
Well using event dispatch thread i modify states of global variables: e.g. keys[600]-this can store all key strokes. Also i store mouse actions inside booleans.

That is how I store keystrokes too. The primary beef I have with doing the same thing for mouse input is that mouse input is usually handled by multiple listeners in different panels, and if I have a complex Swing component (for example a JTree or JTable) it is extremely inconvenient to force input to be handled in another Thread (or more accurately I have no idea how).

Quote
Swing is not thread save. doing stuff on the Event Dispatch Thread is not only common, sun even advises it.

Yeah, Sun advices it. I just had the impression that everyone here was doing something else, but couldn't see why. If that isn't the case then - well - that clears things up. It is quite nice to have automatic collapsing of subsequent repaint events, for example, if the game starts flickering.

Right now I use a java.awt.Canvas + BufferStrategy for painting, except from the information panels, which use passive rendering. The Canvas is convenient because the BufferStrategy does not apply to the other panels, whereas the alternative would be to equip the entire JFrame with a BufferStrategy applying to all components (since I cannot modify directly the BufferStrategy of a JComponent). This would be bad as far as I can see, because it's only a certain portion of the screen which actually has to be rendered every frame, and would result in lots of unneeded buffer activity. However it is quite hack-like to use an AWT component just in order to avoid attaching a BufferStrategy to all the other components.

One alternative is to simply rely on Swing default double buffering, but since everyone use BufferStrategy (or something even more manual) I assume this approach would be ill-fated somehow - people say it is more efficient to write your own (not that I have made any tests).
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2006-03-08 03:18:10 »


Yeah, Sun advices it. I just had the impression that everyone here was doing something else, but couldn't see why.

Most folks here either use active rendering or go aroudn ti competely by usign an OGL binding.

The reasons are performance and control.

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

Mr.CodeIt (6 views)
2014-12-23 03:34:11

rwatson462 (38 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!