Single Minecraft-like tileset

I am making a game with a grid based world.
I already have the code for running around and whatnot working.
However, there’s something I want to learn how to do.

Different maps of my world require different tiles
So a beach might require sand tiles and palm tree tiles, while a forest might require grass and maple tree tiles
The thing is, sand tiles are functionally identical to grass tiles as a “floor”, and palm trees are the same as maple trees as a “wall”
So I was wondering if there were ways to “clean up” some of my code

I know that in Minecraft, all of the block textures are derived from one picture

http://www.imgjoe.com/x/terrain.png

I assume that in the code, this singular picture is broken up into many different smaller textures
I’d like to do that

My grand idea is that I’d have one tileset for a beach, one for a forest, one for a dungeon etc.
So… how can I do this? How have people done it in the past?
(I’m using the standard LWJGL and Slick-Util)

Thanks

This is very easy, it just takes a bit of understanding of how OpenGL textures and texture coordinates work.

In short, if you’re rendering a textured quad (a pretty standard way of drawing sprites using Slick-Util) then you will probably be specifying texture coordinates [0,1] or something similar. If you only want to render a region of the whole texture, you have to specify different texture coordinates for each vertex of your quad.

I’d suggest making yourself an Image or TextureRegion type of class. Example here:

This is done not just for the sake of organization, but also performance. Texture binds are relatively expensive in OpenGL, so the fewer binds you need per frame, the more performant your game will be. LibGDX has some good texture packing tools; either for use offline (i.e. pack separate images into a single PNG file) or in game, on the fly (i.e. at startup, load and pack multiple images into the same OpenGL texture).

EDIT: Regarding your question of different tilesets for different environments; if you organize your sprite types to be in the same spot between sheets (i.e. ground type in cell (0, 0), tree type in cell (1, 0), etc) then you can use the same texture coordinates between sprite sheets. The only thing that needs to change is the texture you bind before rendering.

If you are using slick is very simple:

Load your image and make a spritesheet:



public class Resources{
//definde image
private Image img;
//define spritesheet
public static SpriteSheet tiles;

public static void load(){
//load image (file location,image flipped?,filter)
img=new Image("data/images/tiles.png",false,Image.FILTER_NEAREST);
//load Spritesheet (image ,width of the tile,height of the tile)
tiles= new SpriteSheet(img,16,16);
}
}

Call it in the main class:


public class Main extends BasicGame{
//blablabalbala

public void init(GameContainer gc){
Resources.load();
}

//blabalbalbala
}

And for draw one image of the spritesheet is very simple. Once you have loaded it you can call it from wherever you want:


public void render(Graphics g,GameContainer gc){
//x=xtile of the sheet y= ytile of the sheet
g.drawImage(Resources.tiles.getSubImage(x,y),0,0);
}

I hope it’s understandable :slight_smile:

Thanks to both of you! I spent some time, and looking at your guys’ example code, I got it to work!
It also simplified my code tremendously
Thanks again