I'm using Libgdx and I'm still kind of a noob with it (not programming, just Libgdx). I have one of those large png tile sheets, and I also have the individual tiles. What I'd like to do is make it easy on myself how I load these things. Right now, I'm loading them individually by passing a filename like this:
final String[] fileNames =
{ "data/brick_dark1.png", "data/dngn_open_door.png",
"data/cobble_blood1.png", "data/misc_box.png", "data/brick_dark6.png",
"data/stone_stairs_down.png", "data/stone_stairs_up.png",
"data/dngn_unseen.png", "data/dirt1.png", "data/dragon1.png", };
for (String s : fileNames)
{
texture = new Texture(Gdx.files.internal(s));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
// TextureRegion region =
// new TextureRegion(texture, 0, 0, spriteWidth, spriteHeight);
sprite = new Sprite(texture);
thingRepo.add(sprite);
}
This works just fine, but it also means that my fileNames array has to have every asset known to man explicitly specified. So, can I do it one of these ways, or am I doing it right already?
Bonus brownie points for other improvement ideas!
You could use Atlas
with TexturePacker2
. You should economize number of used textures for tile rendering (best choose is only one big texure), because SpriteBatch
caches render calls for the same texture.
Place your tiles in some forlder (e.g. tiles/tile-1.png
, tiles/tile-2.png
), and convert them all into altas via batch script:
java -cp /path-to-the/gdx.jar;/path-to-the/gdx-tools.jar com.badlogic.gdx.tools.imagepacker.TexturePacker2 %source_folder%/tiles %destination_folder%/tiles
TexturePacker2
pack all textures that will be found into Atlas
. Now you could load it:
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("tiles/yourAtlas.atlas"), false);
Now you could get loaded tiles by calling atlas.getRegions()
, or, use Skin
to hold them:
skin.addRegions(atlas);
Now you can simply get the tile (TextureRegion
, not Texture
) by calling:
skin.getRegion("tile-1")
I hope this helps you.