Here's the code sample. Sorry about the length.
Paul
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.nio.*;
import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
public class MouseTest {
protected GL gl;
protected GLU glu;
protected boolean running;
protected boolean mousedown;
protected int mousex = 0;
protected int mousey = 0;
protected Cursor mouseupcursor;
protected Cursor mousedowncursor;
public static void main(String[] args) {
try {
MouseTest test = new MouseTest();
test.run();
test.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
public MouseTest() throws Exception {
gl = new GL("Mouse Test", 50, 50, 640, 480, 16, 0, 0, 0);
gl.create();
glu = new GLU(gl);
gl.viewport(0, 0, 640, 480);
gl.matrixMode(GL.PROJECTION);
gl.loadIdentity();
glu.ortho2D(0, 640, 480, 0);
gl.matrixMode(GL.MODELVIEW);
gl.loadIdentity();
gl.shadeModel(GL.SMOOTH);
gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.clearDepth(1.0f);
gl.enable(GL.DEPTH_TEST);
gl.depthFunc(GL.LEQUAL);
gl.hint(GL.PERSPECTIVE_CORRECTION_HINT, GL.NICEST);
Keyboard.create();
Mouse.create();
mouseupcursor = createNativeCursor(0, 0, 255);
mousedowncursor = createNativeCursor(255, 255, 0);
Mouse.setNativeCursor(mouseupcursor);
}
public void destroy() {
mouseupcursor.destroy();
mousedowncursor.destroy();
Mouse.destroy();
Keyboard.destroy();
gl.destroy();
}
public void stop() {
running = false;
}
public void run() throws Exception {
running = true;
while(running) {
render();
gl.paint();
gl.tick();
processKeyboard();
processMouse();
if (gl.isCloseRequested()) {
stop();
}
}
}
public void processKeyboard() {
if (Keyboard.isCreated()) {
Keyboard.poll();
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
stop();
}
}
}
public void processMouse() throws Exception {
if (Mouse.isCreated()) {
Mouse.poll();
mousex += Mouse.dx;
mousey += Mouse.dy;
if (mousedown && !Mouse.isButtonDown(0)) {
Mouse.setNativeCursor(mouseupcursor);
} else if (!mousedown && Mouse.isButtonDown(0)) {
Mouse.setNativeCursor(mousedowncursor);
}
mousedown = Mouse.isButtonDown(0);
}
}
private void render() {
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
gl.loadIdentity();
gl.translatef(0f, 0f, -1f);
if (mousedown) {
gl.color3f(1f, 0f, 0f);
} else {
gl.color3f(0f, 1f, 0f);
}
gl.begin(GL.QUADS);
{
gl.vertex2i(mousex-32, mousey-32);
gl.vertex2i(mousex-32, mousey+32);
gl.vertex2i(mousex+32, mousey+32);
gl.vertex2i(mousex+32, mousey-32);
}
gl.end();
}
private Cursor createNativeCursor(int r, int g, int b) throws Exception {
if ((Mouse.getNativeCursorCaps() & Mouse.CURSOR_ONE_BIT_TRANSPARANCY) == 0)
{
System.out.println("No HW cursor support!");
return null;
}
BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.getGraphics();
graphics.setColor(new Color(r, g, b));
graphics.fillOval(0, 0, 32, 32);
graphics.dispose();
IntBuffer cursorData =
ByteBuffer.allocateDirect(32*32*4).order(ByteOrder.nativeOrder()).asIntBuffer();
Raster raster = image.getRaster();
int[] intData = (int[])raster.getDataElements(0, 0, 32, 32, null);
for (int i=0;i<intData.length;i++) {
cursorData.put(intData[i]);
}
return new Cursor(32, 32, 16, 16, 1, Sys.getDirectBufferAddress(cursorData),
Sys.NULL);
}
}