I'm trying to get the basics down for a top-down 2d tiled game in WPF and I have a feeling I'm going about it very wrong. I have tried several methods of displaying a tiled grid of 32x32 .png files on the screen and for small grids (such as 16x16), it works fine. If I move up to a 64x64 grid of tiles with the same image, the memory consumption is monstrous. With my current best approach, it's close to 60MB and redrawing it takes several seconds. Also, redrawing increases the memory usage by 20-30MB. How can I do this more efficiently? Please excuse my code; I can imagine it looks pretty ugly.
class Tile
{
public int Dimension
{
get { return Settings.Default.TileDimensions; }
}
public BitmapImage Image { get; set; }
public Point Location { get; set; }
}
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Columns; c++)
{
var t = new Tile();
t.Image = new BitmapImage(new Uri("/AoW;component/Resources/Images/Grass.png", UriKind.Relative));
t.Location = new Point(c, r);
Tiles.Add(t);
}
}
<Canvas Width="{Binding CanvasWidth}"
Height="{Binding CanvasHeight}">
<ItemsControl ItemsSource="{Binding Current.Container.Map.Tiles,
Source={StaticResource Locator}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding Columns}"
Rows="{Binding Rows}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Width="{Binding Dimension}"
Height="{Binding Dimension}"
RenderOptions.BitmapScalingMode="LowQuality"
Source="{Binding Image}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
Any advice on how to do this properly would be greatly appreciated.