Advertisement

SFML - Layers of Tiles

Started by February 12, 2014 07:21 AM
1 comment, last by arka80 10 years, 11 months ago

Hello. While working with Tiled editor and experimenting with filestream in C++ to load maps i noticed that Tiled editor can separate tiles into layers. An idea came to my mind, wouldn't be awesome to display tiles on screen based on layers they are living on. For example i can have a layer on screen with an array of tiles on it. If i destroy this layer all of the tiles inside of it would be destroyed. This could give you more control over objects in your map that are related to each other. Say for example you are fighting enemies and you kill their boss, all of he's army should be destroyed, which means that if you destroy parent layer you destroy all child objects living on it.

So i want to know if there is a possible way to display tiles in SFML based on layers. This can also be effective if you need to draw tiles over tiles, which will give depth and z index to objects on screen.

An example sketch of what i mean

sdf.png

I know that you can draw sprites on top of sprites by changing the order of the draw logic but what i want is how we can basically group tiles so that you can destroy them by destroying the layer they are living on and not iterate through each one of them.

Obviously yes! two quick ideas:

  • You could use for each layer for example an extra Surface/Sprite and blit on it
    • If you don't want it more you can stop drawing
    • or delete it
  • You could give them an group ID
    • stop drawing the group if you don't want them anymore.
    • release the group (delete it)

It only about organizing, i think.

Advertisement

You can simply draw first every tile in layer 0, then the ones in layer 1 and so on.

It is quite common to subdivide layers with a kind of logic:

0 -> very background (distant) objects (more subdivision on this layer can get parallax effect)

1 -> terrain layer

2 -> pickable objects (coins, monsters, player) (in Tiled, this is more an Object layer than a Tile layer)

3 -> front objects

if you place your character on layer 2 it will be rendered above the 2 back layers, but covered by everything in the layer 3, for example (and it could collide with objects on layer 2 and 1, but ignore the ones on layer 0 and 3).

Using Tiled, I also duplicate collidable layer into 2: one for tiles and one for objects (rects to use for bounding box collisions).

This topic is closed to new replies.

Advertisement