Skip to content

Commit

Permalink
Add shadow rendering for resource sprites.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and reaperrr committed Jan 23, 2021
1 parent 30e5c80 commit 889153c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions OpenRA.Mods.Common/Traits/World/ResourceRenderer.cs
Expand Up @@ -36,6 +36,7 @@ public class ResourceRenderer : IWorldLoaded, IRenderOverlay, ITickRender, INoti

readonly HashSet<CPos> dirty = new HashSet<CPos>();
readonly Queue<CPos> cleanDirty = new Queue<CPos>();
TerrainSpriteLayer shadowLayer;
TerrainSpriteLayer spriteLayer;

public ResourceRenderer(Actor self, ResourceRendererInfo info)
Expand Down Expand Up @@ -68,6 +69,17 @@ void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr)
spriteLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
}

if (shadowLayer == null)
{
var firstWithShadow = r.Value.Variants.Values.FirstOrDefault(v => v.ShadowStart > 0);
if (firstWithShadow != null)
{
var first = firstWithShadow.GetShadow(0, WAngle.Zero);
var emptySprite = new Sprite(first.Sheet, Rectangle.Empty, TextureChannel.Alpha);
shadowLayer = new TerrainSpriteLayer(w, wr, emptySprite, first.BlendMode, wr.World.Type != WorldType.Editor);
}
}

// All resources must share a sheet and blend mode
var sprites = r.Value.Variants.Values.SelectMany(v => Exts.MakeArray(v.Length, x => v.GetSprite(x)));
if (sprites.Any(s => s.BlendMode != spriteLayer.BlendMode))
Expand All @@ -94,13 +106,20 @@ protected void UpdateSpriteLayers(CPos cell, ISpriteSequence sequence, int frame
{
// resource.Type is meaningless (and may be null) if resource.Sequence is null
if (sequence != null)
{
shadowLayer?.Update(cell, sequence.GetShadow(frame, WAngle.Zero), palette, sequence.IgnoreWorldTint);
spriteLayer.Update(cell, sequence, palette, frame);
}
else
{
shadowLayer?.Clear(cell);
spriteLayer.Clear(cell);
}
}

void IRenderOverlay.Render(WorldRenderer wr)
{
shadowLayer?.Draw(wr.Viewport);
spriteLayer.Draw(wr.Viewport);
}

Expand Down Expand Up @@ -166,6 +185,7 @@ void INotifyActorDisposing.Disposing(Actor self)
if (disposed)
return;

shadowLayer?.Dispose();
spriteLayer.Dispose();

ResourceLayer.CellChanged -= AddDirtyCell;
Expand Down

0 comments on commit 889153c

Please sign in to comment.