I’ve just started with game development and I am now making a tile based 2D game. I am using slick-util and lwjgl for my graphics. Right now it loads every texture from it’s own png file, but I would like to use a sprite sheet. Could anyone explain how to load a texture from a spritesheet using slick-util?
Using Spritesheets instead of Multiple Images isn’t that difficult, just change the Texcoords so that it only draws the parts of the Image you want
Thank you Seiya02 it works now
EDIT:
I made the tiles larger on the screen, but now there are lines between all tiles.
public void renderLevel() {
//terrain is my sprite sheet
terrain.bind();
//tiles is a 2 dimensional array which contains the tile placements
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[1].length; j++) {
//get the index of the tile that gets rendered now (for grass returns 0.0, 2.0 for water)
float xIndex = level.getTileIndexX(i, j);
float yIndex = level.getTileIndexY(i, j);
glPushMatrix();
{
glTranslatef(transX, transY, 0);
glBegin(GL_QUADS);
{
//32.0f is the width and height of a single texture on my sprite sheet, 512.0 is the width and height of my total sprite sheet
glTexCoord2d(xIndex * 32.0f / 512.0f, yIndex * 32.0f / 512.0f);
glVertex2f(j * tileSize, i * tileSize);
glTexCoord2d((xIndex * 32.0f + 32.0f) / 512.0f, yIndex * 32.0f / 512.0f);
glVertex2f(tileSize + j * tileSize, i * tileSize);
glTexCoord2d((xIndex * 32.0f + 32.0f) / 512.0f, (yIndex * 32.0f + 32.0f) / 512.0f);
glVertex2f(tileSize + j * tileSize, i * tileSize + tileSize);
glTexCoord2d(xIndex * 32.0f / 512.0f, (yIndex * 32.0f + 32.0f) / 512.0f);
glVertex2f(j * tileSize, i * tileSize + tileSize);
}
glEnd();
}
glPopMatrix();
}
}
}
When i run it the tiles get placed on the right places with the correct textures, but there are thin lines between each adjacent tiles. I’ve tried to change my sprite sheet, but it didn’t fix my problem. Can someone see what I’m doing wrong?
Sounds like you need to use some linear filtering:
http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml
And:
Good luck