I’m just studying how to display images on a screen and make it move using a gamepad.
I’ve done that, but for some reason, when the object moves, it leaves a trail where it passed.
Anyone have any idea why? Here’s my code. Thanks!
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import net.java.games.input.*;
public class FuzzyMover {
Controller[] ca;
int gp=-1; // To store controller number of the gamepad
private Texture texture; // The texture that will hold the image details
float xMove=0, yMove=0, xPos=100,yPos=100;
public void start(){
/* Get all controller instances attached to the machine */
ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
/* Get the name of each controller and pick the gamepad out of all controllers */
for(int i =0;i<ca.length;i++){
System.out.println(ca[i].getName());
System.out.println("Type: "+ca[i].getType().toString());
if(ca[i].getType().toString().indexOf("Gamepad")>-1){
gp=i;
}
}
initGL(800, 600); // initialize Open GL
init(); // initialize other resources
/* Do the following repeatedly until the app window is closed */
while (!Display.isCloseRequested()) {
render();
pollInput();
Display.update();
}
Display.destroy();
}
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.setLocation(43, 12);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
/* Enable alpha blending */
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public void render() {
Color.white.bind();
texture.bind();
xPos=xPos+xMove;
yPos=yPos+yMove;
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(xPos,yPos);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(xPos+texture.getImageWidth(),yPos);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(xPos+texture.getImageWidth(),yPos+texture.getImageHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(xPos,yPos+texture.getImageHeight());
GL11.glEnd();
}
public void init() {
try {
// load texture from PNG file
texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("img/fuzzy.jpg"));
System.out.println("Texture loaded: "+texture);
System.out.println(">> Image width: "+texture.getImageWidth());
System.out.println(">> Image height: "+texture.getImageHeight());
System.out.println(">> Texture width: "+texture.getTextureWidth());
System.out.println(">> Texture height: "+texture.getTextureHeight());
System.out.println(">> Texture ID: "+texture.getTextureID());
} catch (IOException e) {
e.printStackTrace();
}
}
public void pollInput() {
if(gp>-1){
ca[gp].poll();
EventQueue queue = ca[gp].getEventQueue();
Event event = new Event();
while(queue.getNextEvent(event)) {
StringBuffer buffer = new StringBuffer(ca[gp].getName());
buffer.append(" at ");
buffer.append(event.getNanos()).append(", ");
Component comp = event.getComponent();
buffer.append(comp.getName()).append(comp.getIdentifier()).append(" changed to ");
float value = event.getValue();
if(comp.isAnalog()) {
buffer.append(value);
} else {
if(value==1.0f) {
buffer.append("On");
} else {
buffer.append("Off");
}
}
if(comp.getName().equals("X Axis")){
System.out.println(buffer.toString());
if(event.getValue()>0.1 || event.getValue()<-0.1)
xMove=event.getValue();
else
xMove=0;
} else if(comp.getName().equals("Y Axis")){
if(event.getValue()>0.1 || event.getValue()<-0.1)
yMove=event.getValue();
else
yMove=0;
}
}
}
}
public static void main(String args[]){
FuzzyMover fm = new FuzzyMover();
fm.start();
}
}