cleanup and fixes

This commit is contained in:
Brian Matzon 2004-12-07 21:44:57 +00:00
parent d8d223964c
commit 8f4b0eb455
1 changed files with 341 additions and 353 deletions

View File

@ -52,357 +52,345 @@ import org.lwjgl.opengl.glu.GLU;
* @version $Revision$ * @version $Revision$
*/ */
public class HWCursorTest { public class HWCursorTest {
/** The native cursor */ /** The native cursor */
private static Cursor[] cursor = null; private static Cursor[] cursor = null;
/** The mouse cursor position */ /** The mouse cursor position */
private static int mouse_x; private static int mouse_x;
private static int mouse_y; private static int mouse_y;
private static int mouse_btn = 0; private static int mouse_btn = 0;
/** /**
* Executes the test * Executes the test
*/ */
public void execute() { public void execute() {
initialize(); initialize();
mainLoop(); mainLoop();
cleanup(); cleanup();
} }
/** /**
* Sets the display mode for fullscreen mode * Sets the display mode for fullscreen mode
*/ */
protected boolean setDisplayMode() { protected boolean setDisplayMode() {
// get modes // get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60); DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
try { try {
org.lwjgl.util.Display.setDisplayMode(dm, new String[] { org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640, "width=" + 640,
"height=" + 480, "height=" + 480,
"freq=" + 60, "freq=" + 60,
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel() "bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
}); });
return true; return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; return false;
} }
/** /**
* Initializes the test * Initializes the test
*/ */
private void initialize() { private void initialize() {
try { try {
// start of in windowed mode // start of in windowed mode
setDisplayMode(); setDisplayMode();
Display.create(); Display.create();
glInit(); glInit();
initNativeCursors(); initNativeCursors();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void initNativeCursors() throws Exception { private static void initNativeCursors() throws Exception {
if ((Mouse.getNativeCursorCaps() & Mouse.CURSOR_ONE_BIT_TRANSPARENCY) == 0) { if ((Mouse.getNativeCursorCaps() & Mouse.CURSOR_ONE_BIT_TRANSPARENCY) == 0) {
System.out.println("No HW cursor support!"); System.out.println("No HW cursor support!");
System.exit(0); System.exit(0);
} }
cursor = new Cursor[3]; cursor = new Cursor[3];
int cursorImageCount = 1; int cursorImageCount = 1;
int cursorWidth = Mouse.getMaxCursorSize(); int cursorWidth = Mouse.getMaxCursorSize();
int cursorHeight = cursorWidth; int cursorHeight = cursorWidth;
IntBuffer cursorImages; IntBuffer cursorImages;
IntBuffer cursorDelays; IntBuffer cursorDelays;
// Create a single cursor // Create a single cursor
// ================================== // ==================================
cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer(); cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer();
cursorDelays = null; cursorDelays = null;
for(int j=0; j<cursorWidth; j++) { for(int j=0; j<cursorWidth; j++) {
for(int l=0; l<cursorHeight; l++) { for(int l=0; l<cursorHeight; l++) {
cursorImages.put(0xffffffff); cursorImages.put(0xffffffff);
} }
} }
cursorImages.flip(); cursorImages.flip();
cursor[0] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays); cursor[0] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays);
// ---------------------------------- // ----------------------------------
// Create 3 piece animation // Create 3 piece animation
// ================================== // ==================================
cursorImageCount = 3; cursorImageCount = 3;
cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer(); cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer();
cursorDelays = ByteBuffer.allocateDirect(cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer(); cursorDelays = ByteBuffer.allocateDirect(cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer();
for(int i=0; i<cursorImageCount; i++) { for(int i=0; i<cursorImageCount; i++) {
// make a colored square with a chocolate center // make a colored square with a chocolate center
int offColor = 0x00000000; int offColor = 0x00000000;
int onColor = 0xffff0000; int onColor = 0xffff0000;
// change color according to cursor // change color according to cursor
if(i == 1) { if(i == 1) {
onColor = 0xff00ff00; onColor = 0xff00ff00;
} else if (i == 2) { } else if (i == 2) {
onColor = 0xff0000ff; onColor = 0xff0000ff;
} }
// calculate size of center // calculate size of center
int centerSize = (cursorWidth / 5) * (i + 1); int centerSize = (cursorWidth / 5) * (i + 1);
int centerLeft = cursorWidth / 2 - centerSize / 2; int centerLeft = cursorWidth / 2 - centerSize / 2;
int centerRight = cursorWidth / 2 + centerSize / 2; int centerRight = cursorWidth / 2 + centerSize / 2;
// go! // go!
for(int j=0; j<cursorWidth; j++) { for(int j=0; j<cursorWidth; j++) {
for(int l=0; l<cursorHeight; l++) { for(int l=0; l<cursorHeight; l++) {
if(j >= centerLeft && j < centerRight && l >= centerLeft && l < centerRight) { if(j >= centerLeft && j < centerRight && l >= centerLeft && l < centerRight) {
cursorImages.put(offColor); cursorImages.put(offColor);
} else { } else {
cursorImages.put(onColor); cursorImages.put(onColor);
} }
} }
} }
} }
cursorDelays.put(2000).put(2000).put(2000); cursorDelays.put(2000).put(2000).put(2000);
cursorDelays.flip(); cursorDelays.flip();
cursorImages.flip(); cursorImages.flip();
cursor[1] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays); cursor[1] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays);
// ---------------------------------- // ----------------------------------
// Create a 20 piece animation // Create a 20 piece animation
// ================================== // ==================================
cursorImageCount = 20; cursorImageCount = 20;
cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer(); cursorImages = ByteBuffer.allocateDirect(cursorWidth*cursorHeight*cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer();
cursorDelays = ByteBuffer.allocateDirect(cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer(); cursorDelays = ByteBuffer.allocateDirect(cursorImageCount*4).order(ByteOrder.nativeOrder()).asIntBuffer();
cursorDelays.put( cursorDelays.put(
new int[] { new int[] {
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100 100, 100, 100, 100, 100
}); });
float step = 0xffffffff / 20.0f; float step = 0xffffffff / 20.0f;
for(int i=0; i<cursorImageCount; i++) { for(int i=0; i<cursorImageCount; i++) {
for(int j=0; j<cursorWidth; j++) { for(int j=0; j<cursorWidth; j++) {
for(int l=0; l<cursorHeight; l++) { for(int l=0; l<cursorHeight; l++) {
cursorImages.put((int)step); cursorImages.put((int)step);
} }
} }
step += step; step += step;
} }
cursorImages.flip(); cursorImages.flip();
cursorDelays.flip(); cursorDelays.flip();
cursor[2] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays); cursor[2] = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, cursorImageCount, cursorImages, cursorDelays);
// ---------------------------------- // ----------------------------------
Mouse.setNativeCursor(cursor[0]); Mouse.setNativeCursor(cursor[0]);
} }
/** /**
* Runs the main loop of the "test" * Runs the main loop of the "test"
*/ */
private void mainLoop() { private void mainLoop() {
while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)
&& !Display.isCloseRequested()) { && !Display.isCloseRequested()) {
// allow subsystem to get a chance to run too // allow subsystem to get a chance to run too
Display.update(); Display.update();
if (Display.isVisible()) { if (Display.isVisible()) {
// check keyboard input // check keyboard input
processKeyboard(); processKeyboard();
processMouse(); processMouse();
render(); render();
} else { } else {
// no need to render/paint if nothing has changed (ie. window dragged over) // no need to render/paint if nothing has changed (ie. window dragged over)
if (Display.isDirty()) { if (Display.isDirty()) {
render(); render();
} }
// don't waste cpu time, sleep more // don't waste cpu time, sleep more
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException inte) { } catch (InterruptedException inte) {
} }
} }
} }
} }
/** /**
* Performs the logic * Performs the logic
*/ */
private void render() { private void render() {
//clear background //clear background
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// draw white quad // draw white quad
GL11.glPushMatrix(); GL11.glPushMatrix();
{ {
GL11.glTranslatef(mouse_x, mouse_y, 0); GL11.glTranslatef(mouse_x, mouse_y, 0);
GL11.glColor3f(1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_QUADS); GL11.glBegin(GL11.GL_QUADS);
{ {
GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glVertex2i(-50, -50); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glVertex2i(-50, -50);
GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex2i(50, -50); GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex2i(50, -50);
GL11.glColor3f(0.0f, 0.0f, 1.0f); GL11.glVertex2i(50, 50); GL11.glColor3f(0.0f, 0.0f, 1.0f); GL11.glVertex2i(50, 50);
GL11.glColor3f(1.0f, 0.0f, 1.0f); GL11.glVertex2i(-50, 50); GL11.glColor3f(1.0f, 0.0f, 1.0f); GL11.glVertex2i(-50, 50);
} }
GL11.glEnd(); GL11.glEnd();
} }
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
private void processMouse() { private void processMouse() {
int dx = Mouse.getDX(); mouse_x = Mouse.getX();
int dy = Mouse.getDY(); mouse_y = Mouse.getY();
if (dx != 0 || dy != 0) { while(Mouse.next()) {
//mouse_x += dx; int button = Mouse.getEventButton();
//mouse_y += dy; if(button >= 0 && button < 3 && Mouse.getEventButtonState()) {
} mouse_btn = Mouse.getEventButton();
mouse_x = Mouse.getX(); }
mouse_y = Mouse.getY(); }
}
while(Mouse.next()) {
int button = Mouse.getEventButton(); /**
if(button >= 0 && button < 3 && Mouse.getEventButtonState()) { * Processes keyboard input
mouse_btn = Mouse.getEventButton(); */
} private void processKeyboard() {
} //check for fullscreen key
} if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
/** try {
* Processes keyboard input try {
*/ Mouse.setNativeCursor(null);
private void processKeyboard() { } catch (Exception e) {
//check for fullscreen key e.printStackTrace();
if (Keyboard.isKeyDown(Keyboard.KEY_F)) { System.exit(1);
}
try { for(int i=0; i<cursor.length; i++) {
try { cursor[i].destroy();
Mouse.setNativeCursor(null); }
} catch (Exception e) { Display.setFullscreen(true);
e.printStackTrace();
System.exit(1); glInit();
}
for(int i=0; i<cursor.length; i++) { initNativeCursors();
cursor[i].destroy(); } catch (Exception e) {
} e.printStackTrace();
Display.setFullscreen(true); }
}
glInit();
//check for window key
initNativeCursors(); if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
} catch (Exception e) { try {
e.printStackTrace(); try {
} Mouse.setNativeCursor(null);
} } catch (Exception e) {
e.printStackTrace();
//check for window key System.exit(1);
if (Keyboard.isKeyDown(Keyboard.KEY_W)) { }
try { for(int i=0; i<cursor.length; i++) {
try { cursor[i].destroy();
Mouse.setNativeCursor(null); }
} catch (Exception e) { Display.setFullscreen(false);
e.printStackTrace(); glInit();
System.exit(1);
} initNativeCursors();
for(int i=0; i<cursor.length; i++) { } catch (Exception e) {
cursor[i].destroy(); e.printStackTrace();
} }
Display.setFullscreen(false); }
glInit();
if (Keyboard.isKeyDown(Keyboard.KEY_M)) {
initNativeCursors(); try {
} catch (Exception e) { Mouse.setNativeCursor(null);
e.printStackTrace(); } catch (Exception e) {
} e.printStackTrace();
} }
}
if (Keyboard.isKeyDown(Keyboard.KEY_M)) {
try { if (Keyboard.isKeyDown(Keyboard.KEY_N)) {
Mouse.setNativeCursor(null); try {
} catch (Exception e) { Mouse.setNativeCursor(cursor[mouse_btn]);
e.printStackTrace(); } catch (Exception e) {
} e.printStackTrace();
} }
}
if (Keyboard.isKeyDown(Keyboard.KEY_N)) {
try { while(Keyboard.next()) {
Mouse.setNativeCursor(cursor[mouse_btn]); if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) {
} catch (Exception e) { Mouse.setGrabbed(!Mouse.isGrabbed());
e.printStackTrace(); }
} }
} }
while(Keyboard.next()) { /**
if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) { * Cleans up the test
Mouse.setGrabbed(!Mouse.isGrabbed()); */
//mouse_x = Mouse.getX(); private void cleanup() {
//mouse_y = Mouse.getY(); try {
} Mouse.setNativeCursor(null);
} } catch (Exception e) {
} e.printStackTrace();
System.exit(1);
/** }
* Cleans up the test for(int i=0; i<cursor.length; i++) {
*/ cursor[i].destroy();
private void cleanup() { }
try { Display.destroy();
Mouse.setNativeCursor(null); }
} catch (Exception e) {
e.printStackTrace(); /**
System.exit(1); * Initializes OGL
} */
for(int i=0; i<cursor.length; i++) { private void glInit() {
cursor[i].destroy(); // Go into orthographic projection mode.
} GL11.glMatrixMode(GL11.GL_PROJECTION);
Display.destroy(); GL11.glLoadIdentity();
} GLU.gluOrtho2D(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
/** GL11.glLoadIdentity();
* Initializes OGL GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
*/
private void glInit() { //set clear color to black
// Go into orthographic projection mode. GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); //sync frame (only works on windows)
GLU.gluOrtho2D(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight()); Display.setVSyncEnabled(true);
GL11.glMatrixMode(GL11.GL_MODELVIEW); }
GL11.glLoadIdentity();
GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight()); /**
* Test entry point
//set clear color to black */
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); public static void main(String[] args) {
System.out.println("Change between fullscreen and windowed mode, by pressing F and W respectively. Enable hw cursor with N and disable it with M.");
//sync frame (only works on windows) HWCursorTest cursorTest = new HWCursorTest();
Display.setVSyncEnabled(true); cursorTest.execute();
} }
/**
* Test entry point
*/
public static void main(String[] args) {
System.out.println(
"Change between fullscreen and windowed mode, by pressing F and W respectively. Enable hw cursor with N and disable it with M.");
System.out.println(
"Move quad using arrowkeys, and change rotation using +/-");
HWCursorTest cursorTest = new HWCursorTest();
cursorTest.execute();
}
} }