Hello I’m learning and playing around with LWJGL, and I was following the tutorial by the guy from thecodinguniverse on youtube (specifically episode 29 about how to load from a sprite sheet) and it worked, I got my sprite loaded, and even found out how to get it facing the right way and switching sprites when you press the A key. I’m trying to figure out how I would go about making the sprite move as well. I was looking at other tutorials that made sprites move around (such as the LWGJL rpg tutorial on youtube) and I could easily get the little “player” from that to move, but I cannot seem to get this one to move at all. Here is the code I have so far:
package com.dipndawtz.ld25;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import com.dipndawtz.ld25.Util.ImageTools;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.lwjgl.opengl.ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB;
import static org.lwjgl.opengl.GL11.*;
public class Main {
private int x;
private int y;
private static int posX;
private static int posY;
private static final String WINDOW_TITLE = "NecroLock";
private static final int[] WINDOW_DIMENSIONS = { 640, 480 };
private static int spritesheet;
private static Map<String, Sprite> spriteMap = new HashMap<String, Sprite>();
private static Sprite currentSprite;
private static final String SPRITESHEET_IMAGE_LOCATION = "res/spritesheet.png";
private static final String SPRITESHEET_XML_LOCATION = "res/spritesheet.xml";
public Main() {
setUpDisplay();
setUpSpriteSheets();
setUpStates();
setUpMatrices();
enterGameLoop();
cleanUp(false);
}
private static void render() {
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, spritesheet);
int x = currentSprite.getX();
int y = currentSprite.getY();
int x2 = currentSprite.getX() + currentSprite.getWidth();
int y2 = currentSprite.getY() + currentSprite.getHeight();
posX = Display.getWidth() / 2;
posY = Display.getHeight() / 2;
int width = currentSprite.getWidth();
int height = currentSprite.getHeight();
System.out.println(posX + ", " + posY);
// 28x42
glBegin(GL_QUADS);
glTexCoord2f(x2, y2);
// glVertex2f(-0.1f, 0.1f);
glVertex2f(posX, posY);
glTexCoord2f(x, y2);
// glVertex2f(-0.1f, -0.1f);
glVertex2f(posX + width, posY);
glTexCoord2f(x, y);
// glVertex2f(0.1f, -0.1f);
glVertex2f(posX + width, posY + height);
glTexCoord2f(x2, y);
// glVertex2f(0.1f, 0.1f);
glVertex2f(posX, posY + height);
glEnd();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
}
private static void logic() {
}
private void input() {
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
currentSprite = spriteMap.get("fl.png");
}
}
private static void cleanUp(boolean asCrash) {
glDeleteTextures(spritesheet);
Display.destroy();
System.exit(asCrash ? 1 : 0);
}
private static void setUpMatrices() {
glMatrixMode(GL_PROJECTION);
glOrtho(0, 0, 640, 640, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
private static void setUpSpriteSheets() {
spritesheet = ImageTools.glLoadLinearPNG(SPRITESHEET_IMAGE_LOCATION);
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build(new File(SPRITESHEET_XML_LOCATION));
Element root = document.getRootElement();
for (Object spriteObject : root.getChildren()) {
Element spriteElement = (Element) spriteObject;
String name = spriteElement.getAttributeValue("n");
int x = spriteElement.getAttribute("x").getIntValue();
int y = spriteElement.getAttribute("y").getIntValue();
int w = spriteElement.getAttribute("w").getIntValue();
int h = spriteElement.getAttribute("h").getIntValue();
Sprite sprite = new Sprite(name, x, y, w, h);
spriteMap.put(sprite.getName(), sprite);
}
} catch (JDOMException e) {
e.printStackTrace();
cleanUp(true);
} catch (IOException e) {
e.printStackTrace();
cleanUp(true);
}
currentSprite = spriteMap.get("fc.png");
}
private static void setUpStates() {
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
currentSprite = spriteMap.get("fc.png");
}
private static void update() {
Display.update();
Display.sync(60);
}
private void enterGameLoop() {
while (!Display.isCloseRequested()) {
render();
logic();
input();
update();
}
}
private static void setUpDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WINDOW_DIMENSIONS[0], WINDOW_DIMENSIONS[1]));
Display.setTitle(WINDOW_TITLE);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
cleanUp(true);
}
}
public static void main(String[] args) {
new Main();
}
}
The imageTools class,
package com.dipndawtz.ld25.Util;
import de.matthiasmann.twl.utils.PNGDecoder;
import org.lwjgl.BufferUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB;
import static org.lwjgl.opengl.GL11.*;
public class ImageTools {
public static int glLoadLinearPNG(String location) {
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
InputStream in = null;
try {
in = new FileInputStream(location);
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
buffer.flip();
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
} catch (FileNotFoundException e) {
System.err.println("Texture file could not be found.");
return -1;
} catch (IOException e) {
System.err.print("Failed to load the texture file.");
return -1;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
return texture;
}
public static int glLoadPNG(String location) {
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texture);
InputStream in = null;
try {
in = new FileInputStream(location);
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
buffer.flip();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
} catch (IOException e) {
e.printStackTrace();
return -1;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
}
and the sprite class
package com.dipndawtz.ld25;
public final class Sprite {
private final String name;
private final int x;
private final int y;
private final int w;
private final int h;
public Sprite(String name, int x, int y, int w, int h) {
this.name = name;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Sprite sprite = (Sprite) o;
if (h != sprite.h)
return false;
if (w != sprite.w)
return false;
if (x != sprite.x)
return false;
if (y != sprite.y)
return false;
if (name != null ? !name.equals(sprite.name) : sprite.name != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + x;
result = 31 * result + y;
result = 31 * result + w;
result = 31 * result + h;
return result;
}
@Override
public String toString() {
return "Sprite{" + "name='" + name + '\'' + ", x=" + x + ", y=" + y + ", w=" + w + ", h=" + h + '}';
}
public String getName() {
return new String(name);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
}
in the commented out lines of Main you can see what code originally got it showing the sprite in the first place, but when I tried to give it a new variable instead of those constants it wouldn’t draw at all. Any help would be appreciated.