We split the caching SpriteLoader into a SpriteCache and FrameCache. SpriteLoader instead becomes a holder for static loading methods. Only a few classes loaded sprite frames, and they all use it with a transient cache. By moving this method into a new class, we can lose the now redundant frame cache, saving on memory significantly since the frame data array can be reclaimed by the GC. This saves ~58 MiB on frames and ~4 MiB on the caching dictionary in simple tests.
69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation. For more information,
|
|
* see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using OpenRA.FileSystem;
|
|
|
|
namespace OpenRA.Graphics
|
|
{
|
|
public class Theater
|
|
{
|
|
SheetBuilder sheetBuilder;
|
|
Dictionary<ushort, Sprite[]> templates;
|
|
Sprite missingTile;
|
|
|
|
public Theater(TileSet tileset)
|
|
{
|
|
var allocated = false;
|
|
Func<Sheet> allocate = () =>
|
|
{
|
|
if (allocated)
|
|
throw new SheetOverflowException("Terrain sheet overflow. Try increasing the tileset SheetSize parameter.");
|
|
allocated = true;
|
|
|
|
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize), true);
|
|
};
|
|
|
|
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
|
|
templates = new Dictionary<ushort, Sprite[]>();
|
|
|
|
var frameCache = new FrameCache(Game.modData.SpriteLoaders, tileset.Extensions);
|
|
foreach (var t in tileset.Templates)
|
|
{
|
|
var allFrames = frameCache[t.Value.Image];
|
|
var frames = t.Value.Frames != null ? t.Value.Frames.Select(f => allFrames[f]).ToArray() : allFrames;
|
|
templates.Add(t.Value.Id, frames.Select(f => sheetBuilder.Add(f)).ToArray());
|
|
}
|
|
|
|
// 1x1px transparent tile
|
|
missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));
|
|
|
|
Sheet.ReleaseBuffer();
|
|
}
|
|
|
|
public Sprite TileSprite(TerrainTile r)
|
|
{
|
|
Sprite[] template;
|
|
if (!templates.TryGetValue(r.Type, out template))
|
|
return missingTile;
|
|
|
|
if (r.Index >= template.Length)
|
|
return missingTile;
|
|
|
|
return template[r.Index];
|
|
}
|
|
|
|
public Sheet Sheet { get { return sheetBuilder.Current; } }
|
|
}
|
|
}
|