At the most, there will probably only be 4-5 lights at a time… the one over the player, and the ones created by Flares. I’m not going to do anything crazy like create a light for every Flamethrower fire particle… that would be nonsense. But how can I give the effect of the fire particles lighting up? It seems strange for fire not to light up the surrounding area.
Sorry I misread your question. I thought you implemented a custom compositer. Compositers just do math on pixel values in a raster image. It might not be particularly bad on the CPU. The distance function can be done entirely with integer math and no square root. If the scene doesn’t change much you could “cache” a light map as an image. Maybe you could even reduce the resolution. Or use a brushfire based algorithm.
I thought you had more done already and weren’t asking for general advice. Do look at other people’s advice. Ray casting might be better because you can create shadows. Just using a Composite class would be suitable for using illumination sources as a game mechanic (not necessarily a photorealistic aesthetic effect) or as a fog of war effect.
Hehe, if you want to create a light effect for each fire particle that is very easy. They key to getting java2d to perform is to JUST draw images. That is it. Particles are images. Draw your fire particles over your lightmap. That will give a nice effect. Or, have a light attached to each particle. The big slow down in the lighting for java2d is not drawing the light but finding the shadows. Right now I have a system that can do 200+ lights that are fully dynamic casting shadows. However, this is assuming everything blocking light is a square or circle. It works with arbitrary shapes but that is slow.
The moment you start doing any custom composites or grabbing pixels from a BI, everything comes to a halt. If your game is only 300x300 or so resolution then you can do some fancy stuff like left 4k dead.

public void render(Graphics g) { if(!LIGHT_ON) // mapg2d is the Graphics a a VolatileImage return; clear(); mapg2d.setColor(ambientColor); mapg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ambientLight)); mapg2d.fillRect(offX,offY,mapWidth,mapHeight); for(int i = 0; i <= lights.size()-1;i++) { lights.get(i).render(mapg2d); } Graphics2D g2d = (Graphics2D) g.create(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, colorBlend)); // the amount of color we want from the light map to be composited into the scene g2d.drawImage(lightmap,0,0,scrWidth,scrHeight,null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 1)); g2d.drawImage(lightmap,0,0,scrWidth,scrHeight,null); g2d.dispose(); } private void clear() { mapg2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // this line may not be necessary. mapg2d.setColor(BLANK); // a static blank Color of (0,0,0,0); mapg2d.fillRect(0, 0, mapWidth, mapHeight); }
I’ve tried integrating this technique into my little sample applet above, and it isn’t doing anything meaningful.
Could you better explain what precisely SRC_OVER, followed by a DST_ATOP achieves?
I can’t visualize the port-duff composition rules well enough to see how it’d do what the OP wants.
In your example what are typical values for ambientColor, ambientLight, and colorBlend?
Also, what alpha values do the light gradients in your example go from? d0[0,0,0,0f] -> d1[0,0,0,1f] like the example I posted above? or the reverse?
:edit:
AHHHHHHH.
Nevermind, I just read this explanation which made what you were doing click in my mind.
This is accomplished by rendering our lights with the “SRC_OVER” option which allows the image to be drawn over the Lightmap image then the Lightmap is draw over our tiles / background etc… (anything effected by light) using the “DST_ATOP” method which replaces our lights with the tiles / background (objects under our lights).
Wow people really are looking at that?
Basically, SCR_OVER is the color and DIST_ATP is the alpha. This is just one way that I know works. There may be other better ones. But right now, with my current lighting code, I can have over 200 lights that are rather large and stay at 60 fps. You could easily attach a light to all projectiles. Maybe not to every particle in the game but flame thrower doesn’t really shoot particles it shoots flames which are projectiles so that could still have a light on each projectile.