[英]Loading tile sheet in Libgdx
I'm using Libgdx and I'm still kind of a noob with it (not programming, just Libgdx). 我正在使用Libgdx,但仍然有点菜鸟(不是编程的,只是Libgdx)。 I have one of those large png tile sheets, and I also have the individual tiles.
我有其中一个较大的png磁贴纸,也有单个磁贴。 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. 这很好用,但是这也意味着我的fileNames数组必须明确地指定每个已知的资产。 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
. 您可以将
Atlas
与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. 您应该节省用于贴图渲染的已使用纹理的数量(最好的选择只是一个大纹理),因为
SpriteBatch
缓存会为相同的纹理渲染调用。
Place your tiles in some forlder (eg tiles/tile-1.png
, tiles/tile-2.png
), and convert them all into altas via batch script: 将您的图块放置在某个forlder中(例如
tiles/tile-1.png
, tiles/tile-2.png
2.png),然后通过批处理脚本将它们全部转换为altas:
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
. TexturePacker2
打包将在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: 现在,您可以通过调用
atlas.getRegions()
获得加载的图块,或者使用Skin
来保持它们:
skin.addRegions(atlas);
Now you can simply get the tile ( TextureRegion
, not Texture
) by calling: 现在,您只需调用以下
TextureRegion
即可获取图块( TextureRegion
而不是Texture
):
skin.getRegion("tile-1")
I hope this helps you. 我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.