Page 1 of 1
Best way to chunk tiles?
Posted: Wed Oct 26, 2022 3:03 pm
by Sammm
Hello!
I'm attempting to make a bigger indie game with Love2D, and I'm using Tiled with STI to create large maps. However, due to performance, I don't think it's a wise idea to draw the entire map on the screen, but rather, divide it into chunks and only draw the chunks the player is in to increase performance. However, I'm a bit unsure on the best way to do this. My first thought was to divide the map into 25x50 tiles, then check if the player is within these tiles. If so, then draw the chunk(s). However, looping through tiles would also take a lot of computing power, so perhaps I should add triggers to the map, and if the player passes a certain trigger then I draw the chunk? What do you all think?
Thanks for any help!
Re: Best way to chunk tiles?
Posted: Wed Oct 26, 2022 4:07 pm
by BrotSagtMist
I dont even understand how that is a problem in the first place.
If you render like:
Code: Select all
for x=Posx,Posx+srceensizex do
for y=Posy,Posy+srceensizey do
love.graphics.draw(Tiles[x][y],x*TS,y*TS)
end
end
You can have giant maps without cpu usage, checks or anything.
You just draw what is on the screen and ignore the rest of the map.
Re: Best way to chunk tiles?
Posted: Wed Oct 26, 2022 4:23 pm
by Sammm
I'm using STI with Tiled though, so I'm sure I can loop through tiles like that. I'll experiment and see what I can do though.
Re: Best way to chunk tiles?
Posted: Wed Oct 26, 2022 4:26 pm
by BrotSagtMist
Yea i never got why one would use tiled in the first place, seems to be overly complicated..
Re: Best way to chunk tiles?
Posted: Wed Oct 26, 2022 5:25 pm
by pgimeno
Maybe STI isn't the best choice for that purpose. It's possible to draw only the tiles that are on the screen, adding them to a SpriteBatch every frame, assuming you have the tile numbers in a 1D or 2D array.
What's the size of the map? I'm drawing a 4096x2048 pixel map (sometimes twice every frame) in Thrust II Reloaded and that doesn't seem to be a bottleneck. The tiles are 32x32 pixels and the map is 128x64 tiles, also made with Tiled, and I read the data from the TMX file directly (setting it to CSV).
Re: Best way to chunk tiles?
Posted: Wed Oct 26, 2022 6:02 pm
by Sammm
The maps are going to be pretty big, since it's a metroidvania world. I've heard about spritebatches as well, just don't really know what they are. I'll see if I can figure out how to implament that into my tiled world.
Re: Best way to chunk tiles?
Posted: Thu Oct 27, 2022 7:06 am
by darkfrei
1) Set the chunk as 32x32 tiles.
2) Make the map.
3) Take the time that the chunk needs to be loaded.
4) Make the loading system that spread the loading time between several ticks/updates.
5) Load and unload the chunks that are not visible for the player, according the movement velocity and the time to reach the unloaded chunk.
Re: Best way to chunk tiles?
Posted: Thu Oct 27, 2022 9:07 pm
by pgimeno
Sammm wrote: ↑Wed Oct 26, 2022 6:02 pm
I've heard about spritebatches as well, just don't really know what they are.
GPUs are really good at drawing a lot of triangles in a single draw call, as opposed to using one draw call per polygon. Spritebatches exploit that, because the gain in drawing speed compensates the time to put together the triangles.
A spritebatch is just a collection of quads (pairs of triangles) that can be drawn in a single draw call. You need to define the layout first. You can clear and re-fill the spritebatch from scratch every frame, and the speed will probably be acceptable. There's a catch: one single texture per spritebatch.
With a metroidvania style game, it's more a question of whether the map will fit in memory, and how to deal with changing tilesets. An FFI array will be more compact than a table.