LibGDX: How to make a tilemap?

I would like to draw a map composed of a limited set of slices at different zoom levels, like google maps for example. The reason I am considering using tiles is that the map is very large and at the highest magnification the entire map is very large (15,000 to 20,000 pixels).

Now I thought about creating a quadrant to organize the fragments. Every time a frame is drawn, the quadtree logic evaluates which tiles to render, so rendering times are minimal. However, how would I organize the map tile objects myself? When will I best load tiles? Creating a sprite for each tile seems like overkill since tile sizes and positions remain static. SpriteCache doesn't seem to help in this case, as I only draw a subset of the map for each frame. Also, there are quite a few pros and cons in a few cases: When I load all the tiles when the app starts, it takes a long time to load and cache them in the GPU (it doesn't even count all the tiles to fit into memory). When I load the required tiles at runtime, I assumethat you are experiencing some lag when rendering tiles.

Is there a "best practice" for such a case?

I know there is no right or wrong answer to this, but I would like to hear other suggestions for solving this problem.

Thank!!!!

+3


source to share


1 answer


'how can I arrange map tile objects?':
a) You can group tiles into Districts,
b) Then your viewer / camera only draws the areas that are visible (e.g. culling, but not for each tile, because it is too expensive to ask each stove, if it is in sight); c) Each area drawn draws its tiles in the correct place (relative to Area.position).

Here the question is, "When will I best load tiles?" can be solved with the answer:
- the best approach is to load sprites before drawing call and only for Area.Tiles on the view;
- load sprites from the pool.

I tested this approach on a 1000 x 1000 tiles map,
where my Viewer (active tiles) was 3 x 3 Area (90 x 90 tiles). And with this approach, it doesn't matter for performance how big the card is (while the computer has memory :)), only the size of the viewer matters ...


If the viewer can be larger then you can preprocess the entire Area.Tiles with a texture and draw directly one Area.texture for each visible area on each frame.



Textures should be loaded initially when the application starts.
Textures can be linked to tiles when creating a Viewer and when moving one area up / down / left / right.

for (Area area : Viewer.areas) {
    for (Tile tile : area.tiles) {
        tile.texture = <loaded_texture> OR tile.image = new Libgdx.Image(<loaded_texture>) OR any other appropriate for your tile object way
    }
}

      

+2


source







All Articles