Merge pull request #7424 from RoosterDragon/minor-alloc

Avoid some memory allocations
This commit is contained in:
penev92
2015-02-16 20:20:40 +02:00
4 changed files with 61 additions and 41 deletions

View File

@@ -181,7 +181,10 @@ namespace OpenRA
public override string ToString() public override string ToString()
{ {
return "{0} {1}{2}".F(Info.Name, ActorID, IsInWorld ? "" : " (not in world)"); var name = Info.Name + " " + ActorID;
if (!IsInWorld)
name += " (not in world)";
return name;
} }
public T Trait<T>() public T Trait<T>()

View File

@@ -88,58 +88,74 @@ namespace OpenRA.Widgets
public static void DrawPanelPartial(string collection, Rectangle bounds, PanelSides ps) public static void DrawPanelPartial(string collection, Rectangle bounds, PanelSides ps)
{ {
var images = new[] { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" }; DrawPanelPartial(bounds, ps,
var ss = images.Select(i => ChromeProvider.GetImage(collection, i)).ToArray(); ChromeProvider.GetImage(collection, "border-t"),
DrawPanelPartial(ss, bounds, ps); ChromeProvider.GetImage(collection, "border-b"),
ChromeProvider.GetImage(collection, "border-l"),
ChromeProvider.GetImage(collection, "border-r"),
ChromeProvider.GetImage(collection, "corner-tl"),
ChromeProvider.GetImage(collection, "corner-tr"),
ChromeProvider.GetImage(collection, "corner-bl"),
ChromeProvider.GetImage(collection, "corner-br"),
ChromeProvider.GetImage(collection, "background"));
} }
public static void DrawPanelPartial(Sprite[] ss, Rectangle bounds, PanelSides ps) public static void DrawPanelPartial(Rectangle bounds, PanelSides ps,
Sprite borderTop,
Sprite borderBottom,
Sprite borderLeft,
Sprite borderRight,
Sprite cornerTopLeft,
Sprite cornerTopRight,
Sprite cornerBottomLeft,
Sprite cornerBottomRight,
Sprite background)
{ {
var marginLeft = ss[2] == null ? 0 : (int)ss[2].Size.X; var marginLeft = borderLeft == null ? 0 : (int)borderLeft.Size.X;
var marginTop = ss[0] == null ? 0 : (int)ss[0].Size.Y; var marginTop = borderTop == null ? 0 : (int)borderTop.Size.Y;
var marginRight = ss[3] == null ? 0 : (int)ss[3].Size.X; var marginRight = borderRight == null ? 0 : (int)borderRight.Size.X;
var marginBottom = ss[1] == null ? 0 : (int)ss[1].Size.Y; var marginBottom = borderBottom == null ? 0 : (int)borderBottom.Size.Y;
var marginWidth = marginRight + marginLeft; var marginWidth = marginRight + marginLeft;
var marginHeight = marginBottom + marginTop; var marginHeight = marginBottom + marginTop;
// Background // Background
if (ps.HasFlags(PanelSides.Center) && ss[8] != null) if (ps.HasFlags(PanelSides.Center) && background != null)
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top + marginTop, FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top + marginTop,
bounds.Width - marginWidth, bounds.Height - marginHeight), bounds.Width - marginWidth, bounds.Height - marginHeight),
ss[8]); background);
// Left border // Left border
if (ps.HasFlags(PanelSides.Left) && ss[2] != null) if (ps.HasFlags(PanelSides.Left) && borderLeft != null)
FillRectWithSprite(new Rectangle(bounds.Left, bounds.Top + marginTop, FillRectWithSprite(new Rectangle(bounds.Left, bounds.Top + marginTop,
marginLeft, bounds.Height - marginHeight), marginLeft, bounds.Height - marginHeight),
ss[2]); borderLeft);
// Right border // Right border
if (ps.HasFlags(PanelSides.Right) && ss[3] != null) if (ps.HasFlags(PanelSides.Right) && borderRight != null)
FillRectWithSprite(new Rectangle(bounds.Right - marginRight, bounds.Top + marginTop, FillRectWithSprite(new Rectangle(bounds.Right - marginRight, bounds.Top + marginTop,
marginLeft, bounds.Height - marginHeight), marginLeft, bounds.Height - marginHeight),
ss[3]); borderRight);
// Top border // Top border
if (ps.HasFlags(PanelSides.Top) && ss[0] != null) if (ps.HasFlags(PanelSides.Top) && borderTop != null)
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top, FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top,
bounds.Width - marginWidth, marginTop), bounds.Width - marginWidth, marginTop),
ss[0]); borderTop);
// Bottom border // Bottom border
if (ps.HasFlags(PanelSides.Bottom) && ss[1] != null) if (ps.HasFlags(PanelSides.Bottom) && borderBottom != null)
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Bottom - marginBottom, FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Bottom - marginBottom,
bounds.Width - marginWidth, marginTop), bounds.Width - marginWidth, marginTop),
ss[1]); borderBottom);
if (ps.HasFlags(PanelSides.Left | PanelSides.Top) && ss[4] != null) if (ps.HasFlags(PanelSides.Left | PanelSides.Top) && cornerTopLeft != null)
DrawRGBA(ss[4], new float2(bounds.Left, bounds.Top)); DrawRGBA(cornerTopLeft, new float2(bounds.Left, bounds.Top));
if (ps.HasFlags(PanelSides.Right | PanelSides.Top) && ss[5] != null) if (ps.HasFlags(PanelSides.Right | PanelSides.Top) && cornerTopRight != null)
DrawRGBA(ss[5], new float2(bounds.Right - ss[5].Size.X, bounds.Top)); DrawRGBA(cornerTopRight, new float2(bounds.Right - cornerTopRight.Size.X, bounds.Top));
if (ps.HasFlags(PanelSides.Left | PanelSides.Bottom) && ss[6] != null) if (ps.HasFlags(PanelSides.Left | PanelSides.Bottom) && cornerBottomLeft != null)
DrawRGBA(ss[6], new float2(bounds.Left, bounds.Bottom - ss[6].Size.Y)); DrawRGBA(cornerBottomLeft, new float2(bounds.Left, bounds.Bottom - cornerBottomLeft.Size.Y));
if (ps.HasFlags(PanelSides.Right | PanelSides.Bottom) && ss[7] != null) if (ps.HasFlags(PanelSides.Right | PanelSides.Bottom) && cornerBottomRight != null)
DrawRGBA(ss[7], new float2(bounds.Right - ss[7].Size.X, bounds.Bottom - ss[7].Size.Y)); DrawRGBA(cornerBottomRight, new float2(bounds.Right - cornerBottomRight.Size.X, bounds.Bottom - cornerBottomRight.Size.Y));
} }
public static string FormatTime(int ticks) public static string FormatTime(int ticks)

View File

@@ -24,7 +24,8 @@ namespace OpenRA.Mods.Cnc
Dictionary<string, string> loadInfo; Dictionary<string, string> loadInfo;
Stopwatch loadTimer = Stopwatch.StartNew(); Stopwatch loadTimer = Stopwatch.StartNew();
Sheet sheet; Sheet sheet;
Sprite[] ss; Sprite borderTop, borderBottom, borderLeft, borderRight,
cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight;
int loadTick; int loadTick;
float2 nodPos, gdiPos, evaPos; float2 nodPos, gdiPos, evaPos;
Sprite nodLogo, gdiLogo, evaLogo, brightBlock, dimBlock; Sprite nodLogo, gdiLogo, evaLogo, brightBlock, dimBlock;
@@ -44,17 +45,14 @@ namespace OpenRA.Mods.Cnc
var res = r.Resolution; var res = r.Resolution;
bounds = new Rectangle(0, 0, res.Width, res.Height); bounds = new Rectangle(0, 0, res.Width, res.Height);
ss = new[] borderTop = new Sprite(sheet, new Rectangle(161, 128, 62, 33), TextureChannel.Alpha);
{ borderBottom = new Sprite(sheet, new Rectangle(161, 223, 62, 33), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(161, 128, 62, 33), TextureChannel.Alpha), borderLeft = new Sprite(sheet, new Rectangle(128, 161, 33, 62), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(161, 223, 62, 33), TextureChannel.Alpha), borderRight = new Sprite(sheet, new Rectangle(223, 161, 33, 62), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(128, 161, 33, 62), TextureChannel.Alpha), cornerTopLeft = new Sprite(sheet, new Rectangle(128, 128, 33, 33), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(223, 161, 33, 62), TextureChannel.Alpha), cornerTopRight = new Sprite(sheet, new Rectangle(223, 128, 33, 33), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(128, 128, 33, 33), TextureChannel.Alpha), cornerBottomLeft = new Sprite(sheet, new Rectangle(128, 223, 33, 33), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(223, 128, 33, 33), TextureChannel.Alpha), cornerBottomRight = new Sprite(sheet, new Rectangle(223, 223, 33, 33), TextureChannel.Alpha);
new Sprite(sheet, new Rectangle(128, 223, 33, 33), TextureChannel.Alpha),
new Sprite(sheet, new Rectangle(223, 223, 33, 33), TextureChannel.Alpha)
};
nodLogo = new Sprite(sheet, new Rectangle(0, 256, 256, 256), TextureChannel.Alpha); nodLogo = new Sprite(sheet, new Rectangle(0, 256, 256, 256), TextureChannel.Alpha);
gdiLogo = new Sprite(sheet, new Rectangle(256, 256, 256, 256), TextureChannel.Alpha); gdiLogo = new Sprite(sheet, new Rectangle(256, 256, 256, 256), TextureChannel.Alpha);
@@ -87,7 +85,10 @@ namespace OpenRA.Mods.Cnc
r.RgbaSpriteRenderer.DrawSprite(nodLogo, nodPos); r.RgbaSpriteRenderer.DrawSprite(nodLogo, nodPos);
r.RgbaSpriteRenderer.DrawSprite(evaLogo, evaPos); r.RgbaSpriteRenderer.DrawSprite(evaLogo, evaPos);
WidgetUtils.DrawPanelPartial(ss, bounds, PanelSides.Edges); WidgetUtils.DrawPanelPartial(bounds, PanelSides.Edges,
borderTop, borderBottom, borderLeft, borderRight,
cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight,
null);
var barY = bounds.Height - 78; var barY = bounds.Height - 78;
if (!setup && r.Fonts != null) if (!setup && r.Fonts != null)

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
public bool IsVisible(Actor self, Player byPlayer) public bool IsVisible(Actor self, Player byPlayer)
{ {
return byPlayer == null || Shroud.GetVisOrigins(self).Any(o => byPlayer.Shroud.IsVisible(o)); return byPlayer == null || Shroud.GetVisOrigins(self).Any(byPlayer.Shroud.IsVisible);
} }
public IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r) public IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r)