@Chuck_Esterbrook Thanks for the new version, I tried it this morning, but it seems to run a lot slower - taking a couple of seconds to complete 186 country colour changes. In the mean time I think I’ve now come up with a solution to the whole problems.
Texture2D historyMap = TextureManager.GetSpriteTextureData(SpriteTexture.Asset.HistoryMap).Texture2D;
_historyMapData = historyMap.GetPixels();
-
When I load in the blank map (it’s transparent for sea, and white for the countries/region), I get get the pixel colours and store them in a class wide Color[] variable
-
In my Update() method (I update all the regions when a key is pressed - at the moment, just random colours - Key press is to simulate a turn by turn change. I’ve put the flood fill into a “util” class and pass the Color[] data to it after getting the target colour of the point(x,y);
if (_updateMap)
{
var randomColor = new Color(BaseGame.RND.Next(32, 255), BaseGame.RND.Next(32, 255), BaseGame.RND.Next(32, 255));
Texture2D historyMap = TextureManager.GetSpriteTextureData(SpriteTexture.Asset.HistoryMap).Texture2D;
foreach (Region region in _historyMapRegions)
{
region.Color = GetRandomColor();
Color targetColor = _historyMapData[region.X + region.Y * historyMap.Width];
Util.FloodFill(region.X, region.Y, historyMap.Width, historyMap.Height, targetColor, region.Color, _historyMapData);
}
_updateMap = false;
} -
I then draw the end result to the screen, by setting the Colour[] data to the texture and then simply draw it as normal.
Texture2D historyMap = TextureManager.GetSpriteTextureData(SpriteTexture.Asset.HistoryMap).Texture2D;
historyMap.SetData(_historyMapData);
int leftBorder = (renderTarget2D.Width - historyMap.Width) / 2;
int topBorder = (renderTarget2D.Height - historyMap.Height) / 2;
spriteBatch.Draw(historyMap, new Vector2(leftBorder, topBorder), Color.White); -
Here is my final version of the Flood Fill - Yes it’s recursive, but I’ve figured out why it was getting a stack overflow error - the first IF statement seems to stops this issue. By hold down the key and running the update() method constantly the drawing reminds me of the old commodore 64 loading screens
I’m very happy with the final result.
public static void FloodFill(int x, int y, int w, int h, Color targetColor, Color newColor, Color[] currentData)
{
if (currentData[y * w + x] != targetColor)
return;
currentData[y * w + x] = newColor;
if (y > 0 && currentData[(y - 1) * w + x] != newColor)
FloodFill(x, y - 1, w, h, targetColor, newColor, currentData);
if (y < h - 1 && currentData[(y + 1) * w + x] != newColor)
FloodFill(x, y + 1, w, h, targetColor, newColor, currentData);
if (x > 0 && currentData[y * w + (x - 1)] != newColor)
FloodFill(x - 1, y, w, h, targetColor, newColor, currentData);
if (x < w - 1 && currentData[y * w + (x + 1)] != newColor)
FloodFill(x + 1, y, w, h, targetColor, newColor, currentData);
}
Again, many thanks for all your help on this.
(apologies for the formatting, can’t work out how it works with multiple sections)
EDIT: Recursive - Still get the stack error but only when I tried to pain the background - don’t seem to get it for the countries I have on the map, so it’s very much a size issue. I’ve implemented your “maxDepth” you did in an earlier post to stop it from crashing.