Implemented population center generator

This commit is contained in:
2015-05-22 11:26:29 +03:00
parent e51c74944d
commit b6b2dce32e
23 changed files with 277 additions and 98 deletions

View File

@ -12,7 +12,8 @@ namespace TransportGame.MapViewer
{
None = 0,
Elevation = 1,
All = Elevation
Population = 2,
All = Elevation | Population
};
/// <summary>
@ -36,7 +37,7 @@ namespace TransportGame.MapViewer
private readonly Color WaterColor = Colors.Navy;
private readonly Color ElevationTerrainColor = Colors.White;
private readonly Color TerrainColor = Colors.Gray;
private readonly Color TerrainColor = Colors.Silver;
public MapRenderer()
{
@ -57,14 +58,24 @@ namespace TransportGame.MapViewer
Bitmap24 bitmap = new Bitmap24(Convert.ToInt32(map.Width * Scale), Convert.ToInt32(map.Height * Scale));
// First layer - cells
DrawCells(bitmap, map, (layers & Layers.Elevation) > 0);
DrawPixels(bitmap, map, (layers & Layers.Elevation) > 0, (layers & Layers.Population) > 0);
// Done
return bitmap;
}
}
private void DrawCells(Bitmap24 bitmap, Map map, bool elevation)
private Color AlphaBlend(Color top, Color dst)
{
float A = top.ScA + dst.ScA * (1 - top.ScA);
float R = (top.ScR * top.ScA + dst.ScR * (1 - top.ScA)) / A;
float G = (top.ScG * top.ScA + dst.ScG * (1 - top.ScA)) / A;
float B = (top.ScB * top.ScA + dst.ScB * (1 - top.ScA)) / A;
return Color.FromScRgb(A, R, G, B);
}
private void DrawPixels(Bitmap24 bitmap, Map map, bool elevation, bool population)
{
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap.Height; y++)
@ -76,17 +87,24 @@ namespace TransportGame.MapViewer
if (map.IsWater(mapX, mapY))
bitmap[x, y] = WaterColor;
// Draw elevation
else if (elevation)
{
float alpha = map.Heightmap[mapX, mapY]; // map.Heights range is [0,1]
bitmap[x, y] = Color.Multiply(ElevationTerrainColor, alpha);
}
// Draw terrain
else
{
bitmap[x, y] = TerrainColor;
// Draw elevation
if (elevation)
{
float alpha = map.Heightmap[mapX, mapY]; // map.Heights range is [0,1]
bitmap[x, y] = Color.Multiply(ElevationTerrainColor, alpha);
}
// No elevation, just put a terrain color
else bitmap[x, y] = TerrainColor;
// Population
if (population)
{
Color popColor = Color.FromScRgb(map.GetPopulation(mapX, mapY) * 0.7f, 1, 0, 1);
bitmap[x, y] = AlphaBlend(popColor, bitmap[x, y]);
}
}
}
}

View File

@ -61,9 +61,7 @@
<StackPanel Orientation="Vertical">
<CheckBox IsChecked="{Binding LayersAll}">(All)</CheckBox>
<CheckBox IsChecked="{Binding LayerElevation}">Elevation</CheckBox>
<CheckBox>Roads</CheckBox>
<CheckBox>Buildings</CheckBox>
<CheckBox>Vegetation</CheckBox>
<CheckBox IsChecked="{Binding LayerPopulation}">Population</CheckBox>
<Button Name="buttonRender" Grid.Column="1" Content="Render" Click="buttonRender_Click" />

View File

@ -109,10 +109,10 @@ namespace TransportGame.MapViewer
{
get
{
if (LayerElevation)
if (LayerElevation && LayerPopulation)
return true;
if (!LayerElevation)
if (!LayerElevation && !LayerPopulation)
return false;
return null;
@ -122,12 +122,13 @@ namespace TransportGame.MapViewer
if (value.HasValue)
{
LayerElevation = value.Value;
LayerPopulation = value.Value;
}
}
}
/// <summary>
/// Gets or sets the layer elevation layer flag
/// Gets or sets the elevation layer flag
/// </summary>
public bool LayerElevation
{
@ -148,6 +149,28 @@ namespace TransportGame.MapViewer
}
}
/// <summary>
/// Gets or sets the population layer flag
/// </summary>
public bool LayerPopulation
{
get
{
return _layers.HasFlag(MapRenderer.Layers.Population);
}
set
{
if (value) _layers |= MapRenderer.Layers.Population;
else _layers &= ~MapRenderer.Layers.Population;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("LayerPopulation"));
PropertyChanged(this, new PropertyChangedEventArgs("LayersAll"));
}
}
}
#endregion
/// <summary>

View File

@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TransportGame.Model;
using TransportGame.Utils;
namespace TransportGame.MapViewer.Storage
{
@ -13,16 +14,7 @@ namespace TransportGame.MapViewer.Storage
{
public static Map Read(string file)
{
XmlSerializer serializer = new XmlSerializer(typeof(Map));
Map map;
using (StreamReader reader = new StreamReader(file))
{
map = (Map)serializer.Deserialize(reader);
reader.Close();
}
return map;
return SerializationHelper.DeserializeXml<Map>(file);
}
}
}