How to program tile transitions (C++/SFML)

Started by
13 comments, last by Dragonsoulj 9 years, 7 months ago

That looks very good! smile.png

Yeah, what I tried to define was the A method, I think I will go like this. I'll try to find a performance-friendly way to iterate through every objects (even if I don't think there will be thousands of objects, especially just on the view region).


Pre-sort them. =)

Save your tilemap, but also save your object-map ontop of it. Your object-map contains a list of objects pre-sorted by their y position.

Then, you do this:


//Draw objects north of the player.
for every object with object.y < player.y
{
   drawObject();
}

//Draw the player.
drawPlayer();

//Draw objects south of the player.
for every object with object.y > player.y
{
   drawObject();
}

Since most graphics APIs make (0,0) be the upper-left corner of the image you are drawing, you actually need to sort by (object.y + object.height), because objects (like trees) might be taller than the player, and you're actually trying to sort by the bottom of the object (the tree trunk, the player's feet, etc...), not the top.

Advertisement

That looks very good! smile.png

Yeah, what I tried to define was the A method, I think I will go like this. I'll try to find a performance-friendly way to iterate through every objects (even if I don't think there will be thousands of objects, especially just on the view region).


Pre-sort them. =)

Save your tilemap, but also save your object-map ontop of it. Your object-map contains a list of objects pre-sorted by their y position.

Then, you do this:


//Draw objects north of the player.
for every object with object.y < player.y
{
   drawObject();
}

//Draw the player.
drawPlayer();

//Draw objects south of the player.
for every object with object.y > player.y
{
   drawObject();
}

Since most graphics APIs make (0,0) be the upper-left corner of the image you are drawing, you actually need to sort by (object.y + object.height), because objects (like trees) might be taller than the player, and you're actually trying to sort by the bottom of the object (the tree trunk, the player's feet, etc...), not the top.

Yes, that'd work if there was only the player to render, but there will be entities like monsters or animals.

It works for anything as long as you sort them by their (y+height) position.

Though, in the case where you have a huge mix of things moving around, I'd probably make trees count as "entities", and then just sort the entire array of "entities" and draw them in one loop. The rest of the terrain could still be tiles.

Or else, you can move to the other (easier) solution:

Draw the trunks of the tree on a different layer than the tops of the tree.

  • Background layer 1 (water, sand, grass, whatever)
  • Background layer 2 (transition tiles)
  • Background layer 3 (bushes, tree trunks (the bottom part of the trunk, anyway), rocks, etc...)
  • Background layer 4 (anything else needed, like maybe flowers in the bush, or vines wrapped around the tree trunk)
  • <Player and other entities get drawn next>
  • Foreground layer 1 (tree shadows, wall shadows)
  • Foreground layer 2 (tree tops, so they get drawn OVER the player when he walks behind them, and other things like bridges the player walks under)
  • Cloud layer (cloud effects, rain effects, etc...) (This layer is not tiles, but one repeating semi-transparent image that scrolls horizontally and vertically)

It's the less-perfect way, but it works well enough most of the time, as long as most of your entities are consistent sizes.

But it's really not too hard to do it the "perfect" way (sorted the trees with the entities by y pos), if you wanted to put in a little more work to do it right.

Thanks a lot, everything (at least concerning tiles transitions and Y axis sorting) is done.

I also made another topic for the sorting, if anyone is interested, it's here : http://www.gamedev.net/topic/660632-solved-sorting-a-stdmap/

Just to add a potential option: http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/advanced-terrain-texture-splatting-r3287

This topic is closed to new replies.

Advertisement