Best way to chunk tiles?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Sammm
Prole
Posts: 37
Joined: Fri Sep 09, 2022 4:39 pm

Best way to chunk tiles?

Post 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! :awesome:
Lua is LÖVE, lua is life
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Best way to chunk tiles?

Post 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.
obey
User avatar
Sammm
Prole
Posts: 37
Joined: Fri Sep 09, 2022 4:39 pm

Re: Best way to chunk tiles?

Post 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.
Lua is LÖVE, lua is life
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Best way to chunk tiles?

Post by BrotSagtMist »

Yea i never got why one would use tiled in the first place, seems to be overly complicated..
obey
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Best way to chunk tiles?

Post 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).
User avatar
Sammm
Prole
Posts: 37
Joined: Fri Sep 09, 2022 4:39 pm

Re: Best way to chunk tiles?

Post 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.
Lua is LÖVE, lua is life
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Best way to chunk tiles?

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Best way to chunk tiles?

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 0 guests