Tommy asked this and I have coding buzz so it was fast.
I just edited alphaMapTest with minor modifications. Alphamap.png was too ugly with this effect so I made new one. With this approach we could do easy to use light effect class to Slick.
Code:
package org.newdawn.slick.tests;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
/**
* A test to demonstrate alphamap light effects
*
* @author kalle hämäläinen aka pitbuller
*/
public class AlphaMapTest extends BasicGame {
/** The alpha map being applied */
private Image alphaMap;
/** The texture to apply over the top */
private Image sky;
/** the horizontal coordinate */
private float x;
/** the vertical coordinate */
private float y;
/** boolean array for all the keys */
private boolean keys[] = new boolean[256];
private final static float speed = 0.2f;
/**
* Create a new tester for the clip plane based clipping
*/
public AlphaMapTest() {
super("AlphaMapLight Test");
}
/**
* @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
*/
public void init(GameContainer container) throws SlickException {
alphaMap = new Image("testdata/alphamini.png");
sky = new Image("testdata/sky.jpg");
}
/**
* @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer,
* int)
*/
public void update(GameContainer container, int delta)
throws SlickException {
if (keys[Input.KEY_UP])
y -= speed * delta;
if (keys[Input.KEY_DOWN])
y += speed * delta;
if (keys[Input.KEY_LEFT])
x -= speed * delta;
if (keys[Input.KEY_RIGHT])
x += speed * delta;
if (keys[Input.KEY_ESCAPE])
container.exit();
}
/**
* @see org.newdawn.slick.Game#render(org.newdawn.slick.GameContainer,
* org.newdawn.slick.Graphics)
*/
public void render(GameContainer container, Graphics g)
throws SlickException {
/** first draw everything as normal */
sky.draw();
/** then add lights */
lights(g, 4);
/** then draw UI */
g.setColor(Color.white);
g.drawString("AlphaMapTest", 270, 10);
}
private final void lights(Graphics g, int size) {
/** Scaling stuff so we don't have to use big alpha map image */
float invSize = 1f / size;
g.scale(size, size);
/** setting alpha channel ready so lights add up instead of clipping */
g.clearAlphaMap();
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
/** actual lights, coordinates are transformed to scaled coordinates */
alphaMap.drawCentered(x * invSize, y * invSize);
alphaMap.drawCentered(200 * invSize, 200 * invSize);
/** Scaling back stuff so we don't have to use big alpha map image */
g.scale(invSize, invSize);
/**
* setting alpha channel for clearing everything but light maps just
* added
*/
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_DST_ALPHA);
/** paint everything else with black */
g.fillRect(0, 0, 400, 400);
/** setting drawing mode back to normal */
g.setDrawMode(Graphics.MODE_NORMAL);
}
/**
* @see org.newdawn.slick.BasicGame#keyPressed(int, char)
*/
public void keyPressed(int key, char c) {
keys[key] = true;
}
public void keyReleased(int key, char c) {
keys[key] = false;
}
/**
* Entry point to our test
*
* @param argv
* The arguments to pass into the test
*/
public static void main(String[] argv) {
try {
AppGameContainer container = new AppGameContainer(
new AlphaMapTest());
container.setDisplayMode(400, 400, false);
container.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}