Low-hanging graphics programming fruits?

Hi,
I’m currently working on a tile-based game in Java2D, and I was thinking of adding some eye candy.

For example, maybe I could implement a simple particle system (maybe something like this) for explosions and/or smoke.

Do you have any suggestion for relatively easy to program effects that wouldn’t require drawing a lot (or at all) of new art?

Tutorials and code samples for said effects would also be most welcome!

-Ido.

PS - if absolutely necessary I could switch to something like LWJGL/JOGL or even Slick - but I rather stay with Java2D.

Hi,

I’d be interested to learn any tricks like that too.

One thing that’s pretty easy to do is to add layers, such as transparent trees over the top, or parallax scrolling in the background.

Pulpcore has some cool little effects, check out the applets:
http://www.interactivepulp.com/pulpcore/particles/
http://www.interactivepulp.com/pulpcore/

Those examples are pretty cool.

A particle system is reasonably easy, make a particle class that stores velocity and position in X and Y, provide a simple update() method that applies the motion and gravity etc. Provide a paint() method that draws the particle… in the simplest case, a particle uses just a single image. With the standard java Image / Swing libraries, you can load up a semitransparent PNG if you want to make smoke, and you can use rendering hints to adjust transparency (eg. to make it fade away at the top). one picture, but a nice fluid effect.

Some code to spawn many of these, add them to a list as you create them, remove them from the list as they die.

I will post some example code when i get a chance.

Edit: Here you go:

http://www.juddman.com.au/java/Particle.jar

It’s nowhere near as flashy as the above, but it’s using nothing but java libraries.
Unzip it to get source the code.

Java2D has good performance since JRE1.6.0_10. Not as great as those dedicated libraries, but translucent stuff pushes it the most, and it does ok with that as you should see from the example. (actually, when they’re rotated it gets a bit chunky)

  • Judd

FYI, your example actually runs non-hw accelerated because you requested a 4 buffer BufferStrategy.

Run it with -Dsun.java2d.trace=count to see what primitives are used.

Then change the line that creates the BS to createBufferStragey(2), and re-run, see the difference.
You should see something like this:


752 calls to D3DFillRect
2 calls to sun.java2d.d3d.D3DSwToSurfaceTransform::TransformBlit(IntArgb, AnyAlpha, "D3D Surface")
2 calls to sun.java2d.d3d.D3DSwToTextureBlit::Blit(IntArgb, SrcNoEa, "D3D Texture")
16796 calls to sun.java2d.d3d.D3DTextureToSurfaceTransform::TransformBlit("D3D Texture", AnyAlpha, "D3D Surface")
17552 total calls to 4 different primitives

plus a MUCH better performance with near 0 cpu utilization.

Dmitri

2 runs very slowly on my Linux box, 3 runs pretty smooth.

no idea at all why i put a 4 there. must have been a typo or a forgotten change i was playing around with.
From what i can see, it really doesn’t like graphics that are both translucent and rotated. one or the other is fine, but both bogs it down. i will change it and see how it affects it… 3 vs 4 Seems to make no difference on Linux, but this JVM is 1.6.0_07 so probably has HW accelleration off by default.

That worked. Thanks.

http://www.juddman.com.au/java/Particle2.jar

You can do a good impression of the smoke-monster from Lost with this one :slight_smile:

Any idea if additive blending is possible?

No hw acceleration on linux unless you enable the opengl pipeline with -Dsun.java2d.opengl=True (and the reliability depends on your drivers very much), and I suggest to install 6u10+ as there were some important fixes in that area.

Regarding smoothness on linux when running with the default pipeline. Sometimes it helps to add Toolkit.getDefaultToolkit().sync() after bs.show().

There’s no way to have hw accelerated additive blending in Java2D unfortunately. You could implement your own composite, but it won’t be accelerated.