Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is fading a single tile of a tilemap in and out efficient?  (Read 1331 times)

0 Members and 1 Guest are viewing this topic.

Notion

  • Newbie
  • *
  • Posts: 17
    • View Profile
Is fading a single tile of a tilemap in and out efficient?
« on: November 09, 2020, 11:40:49 am »
Hey everyone,

this is kind of a broad question with probably no "100% accurate" way to answer "the right way". Yet I struggle a lot to find a solution for this problem:

I got a tilemap grid thingy. My game world is about 200*200 Tiles of Size 120*120 big. The efficient way to render those is by putting the relevant tiles in proximity to the camera in a vertex array as can be seen in the tutorial.
Yet I need things to move on top of those tiles. And while they are on top, they should keep fading in and out, and the tile should do the same as well, just "anti-synchronic" (sorry, I lack the english words to be more precise here). So while the Actor on Top is totally opaque, the tile is totally transparent, while the thing on top is transparent, the tile is opaque. Both alphas are changed with a sine function with an x-Offset of +1, their sum is always 1.

The actual problem: In order for this to work I need to check all the occupied tiles, change their color and therefore have to re-calculate the whole vertex array, right? This is probably less efficient, than going back to a sprite for each tile?
Sprite for each tile still seems really unpleasant performance wise. Can anyone give me advice on how to solve this issue? I would be really grateful!

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Is fading a single tile of a tilemap in and out efficient?
« Reply #1 on: November 09, 2020, 02:14:30 pm »
There's no need to recreate the entire array. Assuming you have 4 vertices per tile you'll have something like

std::vector<sf::vertex> verts(800);

To update the transparency of a single tile do

verts[8].color = sf::Color::Transparent;
verts[9].color = sf::Color::Transparent;
verts[10].color = sf::Color::Transparent;
verts[11].color = sf::Color::Transparent;
 

You can easily find out your starting index by looking at the grid index of the tile you want to modify and multiplying it by 4. It can even be simplified to

for(auto i = gridIndex; i < gridIndex + 4; ++i)
{
    verts[i].color = newColour;
}
 

 

anything