Reputation: 3092
I'm playing with 2D games programming. My toy project is a sailing game with explorable islands and more, but at the moment I'm trying to find the best way to apply "beaches" (i.e. soft ends) to the otherwise fairly rough islands. Tile size is 64x48.
My present solution is to iterate through each tile and look for surrounding tiles, and based on their surroundings, replace the water-tile with the correct texture.
I realise that this is a very flawed approach as it is:
Do you guys have an idea how I might get around and solve this in a better way?
Thank you!
edit
This algorithm is performed when the map is being loaded.
Upvotes: 4
Views: 460
Reputation: 28541
An algorithm to detect borders (but not corners at first) in a single pass per line / row would be:
for each horizontal line
previousTag = getTag( first line pixel ) // land or water in our example
for each pixel of the line
currentTag = getTag( this pixel )
if ( previousTag == currentTag )
continue // not a border
else
// We got a vertical border, do what is needed
previousTag = currentTag
endforeach
endforeach
Same goes for vertical lines (instead of incrementing x, you increment y. We can here also know if we got a corner instead of a vertical border:
for each vertical line
previousTag = getTag( first line pixel ) // land or water in our example
for each pixel of the line
currentTag = getTag( this pixel )
if ( previousTag == currentTag )
continue // not a border
else
if ( pixel on the right or on the left is a border )
// we got a corner
else
// we got a horizontal border
previousTag = currentTag
endforeach
endforeach
This should be a pre-process, unless your terrain is dynamic. Don't do that each frame anyway!
Upvotes: 2
Reputation: 21902
At the very least, I would flag the tile so you one have to do this once the first time you render the tile and then reuse the flag value instead of trying to figure it out again.
If you do it at render time, assuming you only show a portion of the map at a time, then it's a lot more efficient than doing it for the entire map all at once.
Other than that, I can't think of another way to make the algorithm more efficient. By definition, a "beach" title is defined as such by the presence of land on the edge of a water tile. So you have no other option than to look at the surrounding tiles...
Upvotes: 2