Real-time manipulation of tiles/pixels

public class PlayerView implements MapView {
private BufferedImage image; // tiles are rendered into this image
private int[] imagePixels;

public PlayerView( /* … */ ) {
// …
this.image = new BufferedImage( // …
this.imagePixels = // get pixels from this.image
}

// renders all visible tiles to this.image, then
// for each visible tile calls applyLight( tile.x, tile.y, light )
public void update( int x, int y, int maxRadius ) { /* … */ }

private void applyLight( int x, int y, float light ) {
// multiply the color values of this.imagePixels in range
// ( x -> x+tileSize, y -> y+tileSize ) with light
}

public void draw( Graphics g ) { g.drawImage( this.image, 0, 0, null ); }

// …
}

I’m wondering whether there are any better ways of doing this. Any ideas?

There a few different ways to do per pixel stuff. My fav is creating your own ImageProducer. Check out the java version of - http://www.gaffer.org/tinyptc/ it will explain how its done.

You can also get the DataBufferInt from BufferedImage.getRaster().getDataBuffer()

You might want to try a different method to light your map. Doing per pixel lighting will probally be slow :slight_smile:

I am grabbing the pixels via getRaster-getDataBuffer, apparently I chose to omit that tidbit.

I’d like to hear your ideas about other ways to implement lighting, I can’t think of anything other than prerendering the tiles, and that’s not an attractive option.

The rendering of the image seems pretty optimal 2 me,
as for the lighting shrug I know naaathing :smiley:

Its a hard problem, i havn’t seen any java2D games using real lighting. you might want to try lwjgl, which will get you hardware lighting with openGL.

The only thing i can see is to do all your lighting on one pixel buffer. Having one back buffer image, and one pixel buffer for the screen. Storing your tile images as int[] so you can do System.arrayCopy(tile,0,pixels,tilePos,tileSize) for each visible tile. Then do lighting on one pixel buffer, and update one image with the new pixels.

Or if it doesn’t need to be per pixel, just try images filled with black, with varying alpha. Basic but most 2D games dont need to much :smiley:

[quote]Or if it doesn’t need to be per pixel, just try images filled with black, with varying alpha. Basic but most 2D games dont need to much :smiley:
[/quote]
I’ve thought about this, but I haven’t been able to figure out how to do it efficiently. Seems to me that I’d have to create one AlphaComposite instance per level of lighting… I dunno, this J2D rendering business is still pretty much a mystery to me. :-/ :slight_smile: