new test, based on MouseTest

This commit is contained in:
Brian Matzon 2003-05-08 21:27:53 +00:00
parent b7a151c48b
commit c04fe10bac
1 changed files with 150 additions and 123 deletions

View File

@ -13,7 +13,7 @@
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'Light Weight Java Game Library' nor the names of * * Neither the name of 'Lightweight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived * its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
* *
@ -31,149 +31,176 @@
*/ */
package org.lwjgl.test.input; package org.lwjgl.test.input;
import java.awt.*; import org.lwjgl.DisplayMode;
import java.awt.event.*;
import org.lwjgl.input.Controller; import org.lwjgl.input.Controller;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLU;
import org.lwjgl.vector.Vector2f;
/** /**
* $Id$ * $Id$
* <br> * <br>
* Controller test, hwich shows a awt window, printing Controller state * Controller test
* *
* @author Brian Matzon <brian@matzon.dk> * @author Brian Matzon <brian@matzon.dk>
* @version $Revision$ * @version $Revision$
*/ */
public class ControllerTest extends Panel { public class ControllerTest {
private int counter = 0; /** OpenGL instance */
private GL gl;
public Thread animationThread; /** GLU instance */
private GLU glu;
/** Creates a new instance of ControllerTest */ /** position of quad to draw */
public ControllerTest() { private Vector2f position = new Vector2f(320.0f, 240.0f);
try {
Controller.create(); /** Display mode selected */
} catch (Exception e) { private DisplayMode displayMode;
e.printStackTrace();
return;
}
animationThread = new Thread() { /** Creates a new instance of ControllerTest */
public void run() { public ControllerTest() {
while (true) { }
paint(getGraphics());
try { private void initialize() {
Thread.sleep(250); // create display and opengl
} catch (InterruptedException inte) { setupDisplay(false);
inte.printStackTrace();
}
}
}
};
animationThread.setDaemon(true);
}
public void paint(Graphics g) { try {
if (g == null) { Keyboard.create();
return; } catch (Exception e) {
} e.printStackTrace();
System.exit(-1);
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
int y = 100;
int x = 100;
Controller.poll();
g.setColor(Color.blue);
g.drawString("Buttoncount: " + Controller.buttonCount, x, y);
y += 20;
g.drawString("-----------------------------------------------", x, y);
y += 20;
if(Controller.hasXAxis) {
g.drawString("x : " + Controller.x, x, y);
y += 20;
} }
}
if(Controller.hasRXAxis) {
g.drawString("rx : " + Controller.rx, x, y); private void setupDisplay(boolean fullscreen) {
y += 20; try {
} gl = new GL("ControllerTest", 50, 50, 640, 480, 16, 0, 0, 0);
gl.create();
if(Controller.hasYAxis) {
g.drawString("y : " + Controller.y, x, y);
y += 20;
}
if(Controller.hasRYAxis) {
g.drawString("ry : " + Controller.ry, x, y);
y += 20;
}
if (Controller.hasZAxis) {
g.drawString("z : " + Controller.z, x, y);
y += 20;
}
if (Controller.hasRZAxis) {
g.drawString("rz : " + Controller.rz, x, y);
y += 20;
}
if (Controller.hasPOV) {
g.drawString("pov: " + Controller.pov, x, y);
y += 20;
}
if (Controller.hasSlider) { glu = new GLU(gl);
g.drawString("slder: " + Controller.slider, x, y); } catch (Exception e) {
y += 20; e.printStackTrace();
System.exit(-1);
} }
//paint buttons initializeOpenGL();
g.drawString("btn: ", x, y); }
x += g.getFontMetrics().stringWidth("btn: ");
for (int i = 0; i < Controller.buttonCount; i++) {
if (Controller.isButtonDown(i)) {
g.drawString(i + ", ", x, y);
x += 15;
}
}
}
public void update(Graphics g) { private void initializeOpenGL() {
paint(g); gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
} glu.ortho2D(0.0, 640, 0, 480);
}
public void disposing() { public void executeTest() {
Controller.destroy(); initialize();
}
/** createController();
* @param args the command line arguments
*/
public static void main(String[] args) {
final ControllerTest p = new ControllerTest();
final Frame f = new Frame();
f.setLayout(null);
f.setSize(640, 480);
f.setLocation(100, 100);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.hide();
p.disposing();
f.dispose();
}
});
p.setSize(640, 480); wiggleController();
p.setLocation(0, 0);
p.setBackground(Color.RED);
f.add(p); Controller.destroy();
f.show(); Keyboard.destroy();
p.animationThread.start(); gl.destroy();
} }
}
private void createController() {
try {
Controller.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private void wiggleController() {
while (!gl.isCloseRequested()) {
gl.tick();
if(gl.isMinimized()) {
try {
Thread.sleep(100);
} catch (InterruptedException inte) {
inte.printStackTrace();
}
continue;
}
Controller.poll();
Keyboard.poll();
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
return;
}
if (Controller.x > 200) {
position.x += 1;
}
if (Controller.x < -200) {
position.x -= 1;
}
if (Controller.y < -200) {
position.y += 1;
}
if (Controller.y > 200) {
position.y -= 1;
}
if(position.x<0) {
position.x = 0;
} else if (position.x>640-60) {
position.x = 640-60;
}
if(position.y < 0) {
position.y = 0;
} else if (position.y>480-30) {
position.y = 480-30;
}
render();
gl.paint();
}
}
private void render() {
gl.clear(GL.COLOR_BUFFER_BIT);
gl.begin(GL.POLYGON);
{
float color = 1.0f;
int buttonDown = 0;
for(int i=0;i<Controller.buttonCount; i++) {
if(Controller.isButtonDown(i)) {
color = (1.0f / Controller.buttonCount) * (i+1);
System.out.println("Button " + i + " down");
}
}
gl.color3f(color, color, color);
gl.vertex2f(position.x + 0.0f, position.y + 0.0f);
gl.vertex2f(position.x + 0.0f, position.y + 30.0f);
gl.vertex2f(position.x + 40.0f, position.y + 30.0f);
gl.vertex2f(position.x + 60.0f, position.y + 15.f);
gl.vertex2f(position.x + 40.0f, position.y + 0.0f);
}
gl.end();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ControllerTest ct = new ControllerTest();
ct.executeTest();
}
}