Cache or inline some delegates to avoid repeated allocations.

This commit is contained in:
RoosterDragon
2015-01-14 22:32:00 +00:00
parent e4ce46514c
commit 930d9cbea3
3 changed files with 20 additions and 16 deletions

View File

@@ -23,12 +23,12 @@ namespace OpenRA.Graphics
static SheetBuilder builder; static SheetBuilder builder;
readonly int size; readonly int size;
string name;
readonly Func<string, float> lineWidth;
public SpriteFont(string name, int size) public SpriteFont(string name, int size)
{ {
this.size = size; this.size = size;
this.name = name;
face = new Face(library, name); face = new Face(library, name);
face.SetPixelSizes((uint)size, (uint)size); face.SetPixelSizes((uint)size, (uint)size);
@@ -39,12 +39,14 @@ namespace OpenRA.Graphics
// TODO: SheetBuilder state is leaked between mod switches // TODO: SheetBuilder state is leaked between mod switches
if (builder == null) if (builder == null)
builder = new SheetBuilder(SheetType.BGRA); builder = new SheetBuilder(SheetType.BGRA);
Func<char, float> characterWidth = character => glyphs[Pair.New(character, Color.White)].Advance;
lineWidth = line => line.Sum(characterWidth);
PrecacheColor(Color.White); PrecacheColor(Color.White, name);
PrecacheColor(Color.Red); PrecacheColor(Color.Red, name);
} }
void PrecacheColor(Color c) void PrecacheColor(Color c, string name)
{ {
using (new PerfTimer("PrecacheColor {0} {1}px {2}".F(name, size, c.Name))) using (new PerfTimer("PrecacheColor {0} {1}px {2}".F(name, size, c.Name)))
for (var n = (char)0x20; n < (char)0x7f; n++) for (var n = (char)0x20; n < (char)0x7f; n++)
@@ -91,7 +93,7 @@ namespace OpenRA.Graphics
public int2 Measure(string text) public int2 Measure(string text)
{ {
var lines = text.Split('\n'); var lines = text.Split('\n');
return new int2((int)Math.Ceiling(lines.Max(s => s.Sum(a => glyphs[Pair.New(a, Color.White)].Advance))), lines.Length * size); return new int2((int)Math.Ceiling(lines.Max(lineWidth)), lines.Length * size);
} }
Cache<Pair<char, Color>, GlyphInfo> glyphs; Cache<Pair<char, Color>, GlyphInfo> glyphs;

View File

@@ -46,12 +46,15 @@ namespace OpenRA.Graphics
readonly Dictionary<string, PaletteReference> palettes = new Dictionary<string, PaletteReference>(); readonly Dictionary<string, PaletteReference> palettes = new Dictionary<string, PaletteReference>();
readonly TerrainRenderer terrainRenderer; readonly TerrainRenderer terrainRenderer;
readonly Lazy<DeveloperMode> devTrait; readonly Lazy<DeveloperMode> devTrait;
readonly Func<string, PaletteReference> createPaletteReference;
internal WorldRenderer(World world) internal WorldRenderer(World world)
{ {
World = world; World = world;
Viewport = new Viewport(this, world.Map); Viewport = new Viewport(this, world.Map);
createPaletteReference = CreatePaletteReference;
foreach (var pal in world.TraitDict.ActorsWithTrait<ILoadsPalettes>()) foreach (var pal in world.TraitDict.ActorsWithTrait<ILoadsPalettes>())
pal.Trait.LoadPalettes(this); pal.Trait.LoadPalettes(this);
@@ -69,7 +72,7 @@ namespace OpenRA.Graphics
return new PaletteReference(name, palette.GetPaletteIndex(name), pal, palette); return new PaletteReference(name, palette.GetPaletteIndex(name), pal, palette);
} }
public PaletteReference Palette(string name) { return palettes.GetOrAdd(name, CreatePaletteReference); } public PaletteReference Palette(string name) { return palettes.GetOrAdd(name, createPaletteReference); }
public void AddPalette(string name, ImmutablePalette pal) { palette.AddPalette(name, pal, false); } public void AddPalette(string name, ImmutablePalette pal) { palette.AddPalette(name, pal, false); }
public void AddPalette(string name, ImmutablePalette pal, bool allowModifiers) { palette.AddPalette(name, pal, allowModifiers); } public void AddPalette(string name, ImmutablePalette pal, bool allowModifiers) { palette.AddPalette(name, pal, allowModifiers); }
public void ReplacePalette(string name, IPalette pal) { palette.ReplacePalette(name, pal); palettes[name].Palette = pal; } public void ReplacePalette(string name, IPalette pal) { palette.ReplacePalette(name, pal); palettes[name].Palette = pal; }

View File

@@ -157,20 +157,19 @@ namespace OpenRA.Mods.Common.Traits
return notVisibleEdges; return notVisibleEdges;
var cell = uv.ToCPos(map); var cell = uv.ToCPos(map);
Func<CPos, bool> isCellVisible = c => isVisible(c.ToMPos(map));
// If a side is shrouded then we also count the corners // If a side is shrouded then we also count the corners
var edge = Edges.None; var edge = Edges.None;
if (!isCellVisible(cell + new CVec(0, -1))) edge |= Edges.Top; if (!isVisible((cell + new CVec(0, -1)).ToMPos(map))) edge |= Edges.Top;
if (!isCellVisible(cell + new CVec(1, 0))) edge |= Edges.Right; if (!isVisible((cell + new CVec(1, 0)).ToMPos(map))) edge |= Edges.Right;
if (!isCellVisible(cell + new CVec(0, 1))) edge |= Edges.Bottom; if (!isVisible((cell + new CVec(0, 1)).ToMPos(map))) edge |= Edges.Bottom;
if (!isCellVisible(cell + new CVec(-1, 0))) edge |= Edges.Left; if (!isVisible((cell + new CVec(-1, 0)).ToMPos(map))) edge |= Edges.Left;
var ucorner = edge & Edges.AllCorners; var ucorner = edge & Edges.AllCorners;
if (!isCellVisible(cell + new CVec(-1, -1))) edge |= Edges.TopLeft; if (!isVisible((cell + new CVec(-1, -1)).ToMPos(map))) edge |= Edges.TopLeft;
if (!isCellVisible(cell + new CVec(1, -1))) edge |= Edges.TopRight; if (!isVisible((cell + new CVec(1, -1)).ToMPos(map))) edge |= Edges.TopRight;
if (!isCellVisible(cell + new CVec(1, 1))) edge |= Edges.BottomRight; if (!isVisible((cell + new CVec(1, 1)).ToMPos(map))) edge |= Edges.BottomRight;
if (!isCellVisible(cell + new CVec(-1, 1))) edge |= Edges.BottomLeft; if (!isVisible((cell + new CVec(-1, 1)).ToMPos(map))) edge |= Edges.BottomLeft;
// RA provides a set of frames for tiles with shrouded // RA provides a set of frames for tiles with shrouded
// corners but unshrouded edges. We want to detect this // corners but unshrouded edges. We want to detect this