Skip to content

Commit

Permalink
Remove color caches
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun authored and Mailaender committed May 4, 2024
1 parent 2398910 commit 6b463f9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 46 deletions.
38 changes: 26 additions & 12 deletions OpenRA.Mods.Common/Graphics/ContrailRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public class ContrailRenderable : IRenderable, IFinalizedRenderable

public int Length => trail.Length;

readonly Actor owner;
readonly World world;
readonly Color startcolor;
readonly Color endcolor;
readonly Color startColor;
readonly bool usePlayerStartColor;
readonly Color endColor;
readonly bool usePlayerEndColor;

// Store trail positions in a circular buffer
readonly WPos[] trail;
Expand All @@ -33,33 +36,36 @@ public class ContrailRenderable : IRenderable, IFinalizedRenderable
int length;
readonly int skip;

public ContrailRenderable(World world, Color startcolor, Color endcolor, WDist startWidth, WDist endWidth, int length, int skip, int zOffset)
: this(world, new WPos[length], startWidth, endWidth, 0, 0, skip, startcolor, endcolor, zOffset) { }
public ContrailRenderable(World world, Actor owner, Color startcolor, bool usePlayerStartColor, Color endcolor, bool usePlayerEndColor, WDist startWidth, WDist endWidth, int length, int skip, int zOffset)
: this(world, owner, new WPos[length], startWidth, endWidth, 0, 0, skip, startcolor, usePlayerStartColor, endcolor, usePlayerEndColor, zOffset) { }

ContrailRenderable(World world, WPos[] trail, WDist startWidth, WDist endWidth, int next, int length, int skip, Color startcolor, Color endcolor, int zOffset)
ContrailRenderable(World world, Actor owner, WPos[] trail, WDist startWidth, WDist endWidth, int next, int length, int skip, Color startColor, bool usePlayerStartColor, Color endColor, bool usePlayerEndColor, int zOffset)
{
this.world = world;
this.owner = owner;
this.trail = trail;
this.startWidth = startWidth;
this.endWidth = endWidth;
this.next = next;
this.length = length;
this.skip = skip;
this.startcolor = startcolor;
this.endcolor = endcolor;
this.startColor = startColor;
this.usePlayerStartColor = usePlayerStartColor;
this.usePlayerEndColor = usePlayerEndColor;
this.endColor = endColor;
ZOffset = zOffset;
}

public WPos Pos => trail[Index(next - 1)];
public int ZOffset { get; }
public bool IsDecoration => true;

public IRenderable WithZOffset(int newOffset) { return new ContrailRenderable(world, (WPos[])trail.Clone(), startWidth, endWidth, next, length, skip, startcolor, endcolor, newOffset); }
public IRenderable WithZOffset(int newOffset) { return new ContrailRenderable(world, owner, (WPos[])trail.Clone(), startWidth, endWidth, next, length, skip, startColor, usePlayerStartColor, endColor, usePlayerEndColor, newOffset); }
public IRenderable OffsetBy(in WVec vec)
{
// Lambdas can't use 'in' variables, so capture a copy for later
var offset = vec;
return new ContrailRenderable(world, trail.Select(pos => pos + offset).ToArray(), startWidth, endWidth, next, length, skip, startcolor, endcolor, ZOffset);
return new ContrailRenderable(world, owner, trail.Select(pos => pos + offset).ToArray(), startWidth, endWidth, next, length, skip, startColor, usePlayerStartColor, endColor, usePlayerEndColor, ZOffset);
}

public IRenderable AsDecoration() { return this; }
Expand All @@ -68,22 +74,30 @@ public IRenderable OffsetBy(in WVec vec)
public void Render(WorldRenderer wr)
{
// Note: The length of contrail is now actually the number of the points to draw the contrail
// and we require at least two points to draw a tail
// and we require at least two points to draw a tail.
var renderLength = length - skip;
if (renderLength <= 1)
return;

var screenWidth = wr.ScreenVector(new WVec(1, 0, 0))[0];
var wcr = Game.Renderer.WorldRgbaColorRenderer;

var startColor = this.startColor;
if (usePlayerStartColor)
startColor = Color.FromArgb(this.startColor.A, owner.OwnerColor());

var endColor = this.endColor;
if (usePlayerEndColor)
endColor = Color.FromArgb(this.endColor.A, owner.OwnerColor());

// Start of the first line segment is the tail of the list - don't smooth it.
var curPos = trail[Index(next - skip - 1)];
var curColor = startcolor;
var curColor = startColor;

for (var i = 1; i < renderLength; i++)
{
var j = next - skip - 1 - i;
var nextColor = Exts.ColorLerp(i / (renderLength - 1f), startcolor, endcolor);
var nextColor = Exts.ColorLerp(i / (renderLength - 1f), startColor, endColor);

var nextX = 0L;
var nextY = 0L;
Expand Down
11 changes: 8 additions & 3 deletions OpenRA.Mods.Common/Projectiles/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,14 @@ public Bullet(BulletInfo info, ProjectileArgs args)

if (info.ContrailLength > 0)
{
var startcolor = info.ContrailStartColorUsePlayerColor ? Color.FromArgb(info.ContrailStartColorAlpha, Player.ActorColor(args.SourceActor)) : Color.FromArgb(info.ContrailStartColorAlpha, info.ContrailStartColor);
var endcolor = info.ContrailEndColorUsePlayerColor ? Color.FromArgb(info.ContrailEndColorAlpha, Player.ActorColor(args.SourceActor)) : Color.FromArgb(info.ContrailEndColorAlpha, info.ContrailEndColor ?? startcolor);
contrail = new ContrailRenderable(world, startcolor, endcolor, info.ContrailStartWidth, info.ContrailEndWidth ?? info.ContrailStartWidth, info.ContrailLength, info.ContrailDelay, info.ContrailZOffset);
var startcolor = Color.FromArgb(info.ContrailStartColorAlpha, info.ContrailStartColor);
var endcolor = Color.FromArgb(info.ContrailEndColorAlpha, info.ContrailEndColor ?? startcolor);
contrail = new ContrailRenderable(world, args.SourceActor,
startcolor, info.ContrailStartColorUsePlayerColor,
endcolor, info.ContrailEndColor == null ? info.ContrailStartColorUsePlayerColor : info.ContrailEndColorUsePlayerColor,
info.ContrailStartWidth,
info.ContrailEndWidth ?? info.ContrailStartWidth,
info.ContrailLength, info.ContrailDelay, info.ContrailZOffset);
}

trailPalette = info.TrailPalette;
Expand Down
11 changes: 8 additions & 3 deletions OpenRA.Mods.Common/Projectiles/Missile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,14 @@ public Missile(MissileInfo info, ProjectileArgs args)

if (info.ContrailLength > 0)
{
var startcolor = info.ContrailStartColorUsePlayerColor ? Color.FromArgb(info.ContrailStartColorAlpha, Player.ActorColor(args.SourceActor)) : Color.FromArgb(info.ContrailStartColorAlpha, info.ContrailStartColor);
var endcolor = info.ContrailEndColorUsePlayerColor ? Color.FromArgb(info.ContrailEndColorAlpha, Player.ActorColor(args.SourceActor)) : Color.FromArgb(info.ContrailEndColorAlpha, info.ContrailEndColor ?? startcolor);
contrail = new ContrailRenderable(world, startcolor, endcolor, info.ContrailStartWidth, info.ContrailEndWidth ?? info.ContrailStartWidth, info.ContrailLength, info.ContrailDelay, info.ContrailZOffset);
var startcolor = Color.FromArgb(info.ContrailStartColorAlpha, info.ContrailStartColor);
var endcolor = Color.FromArgb(info.ContrailEndColorAlpha, info.ContrailEndColor ?? startcolor);
contrail = new ContrailRenderable(world, args.SourceActor,
startcolor, info.ContrailStartColorUsePlayerColor,
endcolor, info.ContrailEndColor == null ? info.ContrailStartColorUsePlayerColor : info.ContrailEndColorUsePlayerColor,
info.ContrailStartWidth,
info.ContrailEndWidth ?? info.ContrailStartWidth,
info.ContrailLength, info.ContrailDelay, info.ContrailZOffset);
}

trailPalette = info.TrailPalette;
Expand Down
18 changes: 14 additions & 4 deletions OpenRA.Mods.Common/Traits/Render/Contrail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ public Contrail(Actor self, ContrailInfo info)
{
this.info = info;

startcolor = info.StartColorUsePlayerColor ? Color.FromArgb(info.StartColorAlpha, Player.ActorColor(self)) : Color.FromArgb(info.StartColorAlpha, info.StartColor);
endcolor = info.EndColorUsePlayerColor ? Color.FromArgb(info.EndColorAlpha, Player.ActorColor(self)) : Color.FromArgb(info.EndColorAlpha, info.EndColor ?? startcolor);
trail = new ContrailRenderable(self.World, startcolor, endcolor, info.StartWidth, info.EndWidth ?? info.StartWidth, info.TrailLength, info.TrailDelay, info.ZOffset);
startcolor = Color.FromArgb(info.StartColorAlpha, info.StartColor);
endcolor = Color.FromArgb(info.EndColorAlpha, info.EndColor ?? startcolor);
trail = new ContrailRenderable(self.World, self,
startcolor, info.StartColorUsePlayerColor,
endcolor, info.EndColor == null ? info.StartColorUsePlayerColor : info.EndColorUsePlayerColor,
info.StartWidth,
info.EndWidth ?? info.StartWidth,
info.TrailLength, info.TrailDelay, info.ZOffset);

body = self.Trait<BodyOrientation>();
}
Expand Down Expand Up @@ -106,7 +111,12 @@ IEnumerable<Rectangle> IRender.ScreenBounds(Actor self, WorldRenderer wr)

void INotifyAddedToWorld.AddedToWorld(Actor self)
{
trail = new ContrailRenderable(self.World, startcolor, endcolor, info.StartWidth, info.EndWidth ?? info.StartWidth, info.TrailLength, info.TrailDelay, info.ZOffset);
trail = new ContrailRenderable(self.World, self,
startcolor, info.StartColorUsePlayerColor,
endcolor, info.EndColor == null ? info.StartColorUsePlayerColor : info.EndColorUsePlayerColor,
info.StartWidth,
info.EndWidth ?? info.StartWidth,
info.TrailLength, info.TrailDelay, info.ZOffset);
}
}
}
8 changes: 2 additions & 6 deletions OpenRA.Mods.Common/Traits/Render/RenderDebugState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits.Render
Expand All @@ -26,14 +25,13 @@ sealed class RenderDebugStateInfo : TraitInfo
public override object Create(ActorInitializer init) { return new RenderDebugState(init.Self, this); }
}

sealed class RenderDebugState : INotifyAddedToWorld, INotifyOwnerChanged, INotifyCreated, IRenderAnnotationsWhenSelected
sealed class RenderDebugState : INotifyAddedToWorld, INotifyCreated, IRenderAnnotationsWhenSelected
{
readonly DebugVisualizations debugVis;
readonly SpriteFont font;
readonly WVec offset;
SquadManagerBotModule[] squadManagerModules;

Color color;
string tagString;

public RenderDebugState(Actor self, RenderDebugStateInfo info)
Expand All @@ -42,7 +40,6 @@ public RenderDebugState(Actor self, RenderDebugStateInfo info)
var yOffset = buildingInfo?.Dimensions.Y ?? 1;
offset = new WVec(0, 512 * yOffset, 0);

color = self.OwnerColor();
font = Game.Renderer.Fonts[info.Font];

debugVis = self.World.WorldActor.TraitOrDefault<DebugVisualizations>();
Expand All @@ -58,13 +55,12 @@ void INotifyAddedToWorld.AddedToWorld(Actor self)
tagString = self.ToString();
}

void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) => color = self.OwnerColor();

IEnumerable<IRenderable> IRenderAnnotationsWhenSelected.RenderAnnotations(Actor self, WorldRenderer wr)
{
if (debugVis == null || !debugVis.ActorTags)
yield break;

var color = self.OwnerColor();
yield return new TextAnnotationRenderable(font, self.CenterPosition - offset, 0, color, tagString);

// Get the actor's activity.
Expand Down
9 changes: 3 additions & 6 deletions OpenRA.Mods.Common/Traits/Render/WithNameTagDecoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
public class WithNameTagDecoration : WithDecorationBase<WithNameTagDecorationInfo>, INotifyOwnerChanged
{
readonly SpriteFont font;
readonly WithNameTagDecorationInfo info;
string name;
Color color;

public WithNameTagDecoration(Actor self, WithNameTagDecorationInfo info)
: base(self, info)
{
font = Game.Renderer.Fonts[info.Font];
color = info.UsePlayerColor ? self.OwnerColor() : info.Color;
this.info = info;

name = self.Owner.PlayerName;
if (name.Length > info.MaxLength)
Expand All @@ -67,15 +67,12 @@ protected override IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRe
var size = font.Measure(name);
return new IRenderable[]
{
new UITextRenderable(font, self.CenterPosition, screenPos - size / 2, 0, color, name)
new UITextRenderable(font, self.CenterPosition, screenPos - size / 2, 0, info.UsePlayerColor ? self.OwnerColor() : info.Color, name)
};
}

void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
if (Info.UsePlayerColor)
color = self.OwnerColor();

name = self.Owner.PlayerName;
if (name.Length > Info.MaxLength)
name = name[..Info.MaxLength];
Expand Down
14 changes: 2 additions & 12 deletions OpenRA.Mods.Common/Traits/Render/WithTextControlGroupDecoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,16 @@ void IRulesetLoaded<ActorInfo>.RulesetLoaded(Ruleset rules, ActorInfo info)
public override object Create(ActorInitializer init) { return new WithTextControlGroupDecoration(init.Self, this); }
}

public class WithTextControlGroupDecoration : IDecoration, INotifyOwnerChanged
public class WithTextControlGroupDecoration : IDecoration
{
readonly WithTextControlGroupDecorationInfo info;
readonly SpriteFont font;
readonly CachedTransform<int, string> label;

Color color;

public WithTextControlGroupDecoration(Actor self, WithTextControlGroupDecorationInfo info)
{
this.info = info;
font = Game.Renderer.Fonts[info.Font];
color = info.UsePlayerColor ? self.OwnerColor() : info.Color;

label = new CachedTransform<int, string>(g => self.World.ControlGroups.Groups[g]);
}

Expand All @@ -74,14 +70,8 @@ IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer
var screenPos = container.GetDecorationOrigin(self, wr, info.Position, info.Margin);
return new IRenderable[]
{
new UITextRenderable(font, self.CenterPosition, screenPos, 0, color, text)
new UITextRenderable(font, self.CenterPosition, screenPos, 0, info.UsePlayerColor ? self.OwnerColor() : info.Color, text)
};
}

void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
if (info.UsePlayerColor)
color = self.OwnerColor();
}
}
}

0 comments on commit 6b463f9

Please sign in to comment.