简体   繁体   English

在Libgdx中加载磁贴

[英]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? 那么,我可以通过以下方式之一执行此操作吗?或者我是否已正确执行此操作?

  1. Generically load all assets in my assets folder without knowing the file name. 通常将所有资产加载到我的资产文件夹中,而不知道文件名。 I suppose that's hard because I have to have a way to easily map them as I load (say, in a Map). 我认为这很困难,因为我必须有一种在加载时轻松映射它们的方法(例如,在Map中)。
  2. Load the large tile sheet, index it and store it in a map. 加载大的瓷砖图纸,将其编入索引并将其存储在地图中。 Same problem as number 1. 与数字1相同的问题。
  3. Do it the right way, which someone will hopefully now explain to me. 以正确的方式做,希望有人现在能向我解释。

Bonus brownie points for other improvement ideas! 奖励布朗尼积分可用于其他改进想法!

You could use Atlas with TexturePacker2 . 您可以将AtlasTexturePacker2一起使用。 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.pngtiles/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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM