From 7e28acadd8015751968d6e2017158119bb84f5a1 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 31 Jan 2015 23:04:46 +0000 Subject: [PATCH 1/2] DrawPanelPartial now specifies each sprite as an individual parameter. This allows some array allocations to be avoided, and makes the code more obvious since each sprite is referred to by name rather than an array index. --- OpenRA.Game/Widgets/WidgetUtils.cs | 68 ++++++++++++++++++------------ OpenRA.Mods.Cnc/CncLoadScreen.cs | 27 ++++++------ 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index 106692c69f..241eda29bc 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -88,58 +88,74 @@ namespace OpenRA.Widgets 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" }; - var ss = images.Select(i => ChromeProvider.GetImage(collection, i)).ToArray(); - DrawPanelPartial(ss, bounds, ps); + DrawPanelPartial(bounds, ps, + ChromeProvider.GetImage(collection, "border-t"), + 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 marginTop = ss[0] == null ? 0 : (int)ss[0].Size.Y; - var marginRight = ss[3] == null ? 0 : (int)ss[3].Size.X; - var marginBottom = ss[1] == null ? 0 : (int)ss[1].Size.Y; + var marginLeft = borderLeft == null ? 0 : (int)borderLeft.Size.X; + var marginTop = borderTop == null ? 0 : (int)borderTop.Size.Y; + var marginRight = borderRight == null ? 0 : (int)borderRight.Size.X; + var marginBottom = borderBottom == null ? 0 : (int)borderBottom.Size.Y; var marginWidth = marginRight + marginLeft; var marginHeight = marginBottom + marginTop; // 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, bounds.Width - marginWidth, bounds.Height - marginHeight), - ss[8]); + background); // 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, marginLeft, bounds.Height - marginHeight), - ss[2]); + borderLeft); // 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, marginLeft, bounds.Height - marginHeight), - ss[3]); + borderRight); // 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, bounds.Width - marginWidth, marginTop), - ss[0]); + borderTop); // 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, bounds.Width - marginWidth, marginTop), - ss[1]); + borderBottom); - if (ps.HasFlags(PanelSides.Left | PanelSides.Top) && ss[4] != null) - DrawRGBA(ss[4], new float2(bounds.Left, bounds.Top)); - if (ps.HasFlags(PanelSides.Right | PanelSides.Top) && ss[5] != null) - DrawRGBA(ss[5], new float2(bounds.Right - ss[5].Size.X, bounds.Top)); - if (ps.HasFlags(PanelSides.Left | PanelSides.Bottom) && ss[6] != null) - DrawRGBA(ss[6], new float2(bounds.Left, bounds.Bottom - ss[6].Size.Y)); - if (ps.HasFlags(PanelSides.Right | PanelSides.Bottom) && ss[7] != null) - DrawRGBA(ss[7], new float2(bounds.Right - ss[7].Size.X, bounds.Bottom - ss[7].Size.Y)); + if (ps.HasFlags(PanelSides.Left | PanelSides.Top) && cornerTopLeft != null) + DrawRGBA(cornerTopLeft, new float2(bounds.Left, bounds.Top)); + if (ps.HasFlags(PanelSides.Right | PanelSides.Top) && cornerTopRight != null) + DrawRGBA(cornerTopRight, new float2(bounds.Right - cornerTopRight.Size.X, bounds.Top)); + if (ps.HasFlags(PanelSides.Left | PanelSides.Bottom) && cornerBottomLeft != null) + DrawRGBA(cornerBottomLeft, new float2(bounds.Left, bounds.Bottom - cornerBottomLeft.Size.Y)); + if (ps.HasFlags(PanelSides.Right | PanelSides.Bottom) && cornerBottomRight != null) + DrawRGBA(cornerBottomRight, new float2(bounds.Right - cornerBottomRight.Size.X, bounds.Bottom - cornerBottomRight.Size.Y)); } public static string FormatTime(int ticks) diff --git a/OpenRA.Mods.Cnc/CncLoadScreen.cs b/OpenRA.Mods.Cnc/CncLoadScreen.cs index c4c878697e..796415fa86 100644 --- a/OpenRA.Mods.Cnc/CncLoadScreen.cs +++ b/OpenRA.Mods.Cnc/CncLoadScreen.cs @@ -24,7 +24,8 @@ namespace OpenRA.Mods.Cnc Dictionary loadInfo; Stopwatch loadTimer = Stopwatch.StartNew(); Sheet sheet; - Sprite[] ss; + Sprite borderTop, borderBottom, borderLeft, borderRight, + cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight; int loadTick; float2 nodPos, gdiPos, evaPos; Sprite nodLogo, gdiLogo, evaLogo, brightBlock, dimBlock; @@ -44,17 +45,14 @@ namespace OpenRA.Mods.Cnc var res = r.Resolution; bounds = new Rectangle(0, 0, res.Width, res.Height); - ss = new[] - { - new Sprite(sheet, new Rectangle(161, 128, 62, 33), TextureChannel.Alpha), - new Sprite(sheet, new Rectangle(161, 223, 62, 33), TextureChannel.Alpha), - new Sprite(sheet, new Rectangle(128, 161, 33, 62), TextureChannel.Alpha), - new Sprite(sheet, new Rectangle(223, 161, 33, 62), TextureChannel.Alpha), - new Sprite(sheet, new Rectangle(128, 128, 33, 33), TextureChannel.Alpha), - new Sprite(sheet, new Rectangle(223, 128, 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) - }; + borderTop = new Sprite(sheet, new Rectangle(161, 128, 62, 33), TextureChannel.Alpha); + borderBottom = new Sprite(sheet, new Rectangle(161, 223, 62, 33), TextureChannel.Alpha); + borderLeft = new Sprite(sheet, new Rectangle(128, 161, 33, 62), TextureChannel.Alpha); + borderRight = new Sprite(sheet, new Rectangle(223, 161, 33, 62), TextureChannel.Alpha); + cornerTopLeft = new Sprite(sheet, new Rectangle(128, 128, 33, 33), TextureChannel.Alpha); + cornerTopRight = new Sprite(sheet, new Rectangle(223, 128, 33, 33), TextureChannel.Alpha); + cornerBottomLeft = new Sprite(sheet, new Rectangle(128, 223, 33, 33), TextureChannel.Alpha); + cornerBottomRight = new Sprite(sheet, new Rectangle(223, 223, 33, 33), 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); @@ -87,7 +85,10 @@ namespace OpenRA.Mods.Cnc r.RgbaSpriteRenderer.DrawSprite(nodLogo, nodPos); 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; if (!setup && r.Fonts != null) From b4993efff117295fa55e34ba762a3d438b90316c Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 31 Jan 2015 23:07:14 +0000 Subject: [PATCH 2/2] Avoid some minor allocations. - Don't call string.Format in Actor.ToString since it gets called often, instead prefer a simple concat. - In HiddenUnderFog.IsVisible, avoid a needless level of lambda indirection. --- OpenRA.Game/Actor.cs | 5 ++++- OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index 14f7c65a7b..1dbc155022 100644 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -181,7 +181,10 @@ namespace OpenRA 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() diff --git a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs index 2c7653f1c8..fa2a44dbb6 100644 --- a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs +++ b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs @@ -22,7 +22,7 @@ namespace OpenRA.Mods.Common.Traits { 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 ModifyRender(Actor self, WorldRenderer wr, IEnumerable r)