简体   繁体   中英

Libgdx TileMap : How should I do to load my ressources efficiently?

Here the situation: I'm developpping a clone of an old Tiled base game. I'm using LIBGDX api. And i'm wondering about how I should manage the loading of my ressources.

I have few maps (3000*3000 tiles).

I could easily use tmx file to represent my map and load it but : All my floor texture are large seamless texture cut in differents tiles and I need to generate transitions tiles dynamically between them.

I tried to generate all the transitions tiles and to use a tmx file but it take a huge amount of space and the game i'm trying to copy doesn't have those tiles and it launch quickly.

So I tried to create a TileMap that I populate by reading the basic map file I got.

Here the code :

        MapLayers layers = worldMap.getLayers();
        TiledMapTileSets tileSets  = worldMap.getTileSets();
        TiledMapTileLayer floorLayer = new TiledMapTileLayer(WORLD_WIDTH,WORLD_HEIGHT,TILE_WIDTH, TILE_HEIGHT);
        TiledMapTileSet floorTileSet = new TiledMapTileSet();
        layers.add(floorLayer);
        tileSets.addTileSet(floorTileSet);

        floorLayer.setCell(0, 0, new Cell());
        byte[] rawData = null;
        try {
            rawData = java.nio.file.Files.readAllBytes(Paths.get(".."+System.getProperty("file.separator")+"core"+System.getProperty("file.separator")+"assets"+System.getProperty("file.separator")+"map"+System.getProperty("file.separator")+"v2_worldmap.map"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        CompressedMap originalMap = new CompressedMap(rawData);
        UncompressedMap uncompMap = originalMap.getUncompressedMap();
        NormalizedMap normalizedMap = uncompMap.getNormalizedMap();

        int originalMapTileid = 20;
        int newTileId = 0;
        int tileSetId = 0;
        for(int y = 0; y < 3072; y++){
            for(int x = 0; x < 3072 ;x++){
                floorLayer.setCell(x, 3071-y, new Cell());
                originalMapTileid = normalizedMap.getIdAtPos(x, y);

                String textname = ToolKit.findTextureNameByIdAndPos(assetManager, (short) originalMapTileid, x, y);
                if(!textureNameToTileId.containsKey(textname)){
                    textureNameToTileId.put(textname, newTileId);
                    //System.out.println(ToolKit.textPathTable.get(textname).toString());
                    floorTileSet.putTile(newTileId, new StaticTiledMapTile(new TextureRegion(new Texture(ToolKit.textPathTable.get(textname)))));
                    tileSetId = newTileId;
                    newTileId++;
                }else{
                    tileSetId  = textureNameToTileId.get(textname);
                }
                floorLayer.getCell(x, 3071-y).setTile(floorTileSet.getTile(tileSetId));
            }
        }

What I do here is basically the same than in a Tmx file. I list the needed ressource them I use them in may map.

----Algorythm ---

For each tile id, i'm finding the linked texture

If the texture is not contained by the TiledMapTileSet

I store that texture in a TileMapTileSet And I put the tile into the layer

Else

I put the tile into the layer

---end---

To calcul that and to load ressources it take around 2 minutes. Its a lot and in the old game it was quicker than that(20seconds). It is actually quicker also if I use tmx but i won't generated all the tiled for transitions before.

How the libgdx load work with tmx file to make them so fast to load?

How should I do for generate my map efficiently?

What is the best practices in game about ressources loading?

You are loading each tile separately, no matter what you do, that will be slow. Reading a lot of smaller files is slower than reading one large file. It also will be slow to render, due to needless texture switching. Your map being very large does't help either.

The easiest thing, would be to cut the size of the map. Make it 300x300 and now your loading time is 100 time faster.

You don't seam to be generating anything in the code you posted, just loading a ton of small Textures. Any reason you cant put them all in the atlas to make it somewhat fast? You can have an atlas for each map part.

Check the source for AssetManager and relevant loaders to see how it all works. A lot of the loading can be done on separate thread, without blocking the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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