I am working on recreating the space invaders game and it shouldn’t be too difficult if I can get all this working haha. I have read through the tutorials on the main page of the lwjgl wiki.
So the idea is I have an abstract class called Entity which needs a sprite (which includes a Texture) and x and y coordinates.
That works fine, so I created the EntityPlayer class which will be the space ship. It is loading the .png file fine (no errors), but it is only displaying as a single pixel.
I’ll include the code I think you’ll need to help me solve this issue below:
The texture loading in the Sprite class:
[spoiler]
public void init(String path) {
if (path != null) {
try {
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
[/spoiler]
The draw method in the Sprite class:
[spoiler]
public void draw(int x, int y) {
// store the current model matrix
glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
glTranslatef(x, y, 0);
// draw a quad textured to match the sprite
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
// restore the model view matrix to prevent contamination
glPopMatrix();
}
[/spoiler]
The draw method in the Entity class:
[spoiler]
public void draw() {
sprite.draw((int)x, (int)y);
}
[/spoiler]
The initialization code for the only instance of EntityPlayer:
[spoiler]
public static final EntityPlayer player;
static {
player = new EntityPlayer(new Sprite("res/player.png"), 100, 100);
}
[/spoiler]
Hopefully there is just something with lwjgl I’m doing wrong. If not, maybe I can pastebin my code for someone to help me. I’m pretty familiar with java, so It shouldn’t be anything outside of lwjgl.
Please let me know if you see anything wrong with the above code or if you have any tips, that would be much appreciated as well!