Skip to content

Commit

Permalink
Merge pull request OpenRA#7288 from RoosterDragon/faster-depth-sorting
Browse files Browse the repository at this point in the history
Speed up depth sorting of renderables.
  • Loading branch information
chrisforbes committed Jan 8, 2015
2 parents 0a493df + 69c409c commit 507fe66
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
16 changes: 0 additions & 16 deletions OpenRA.Game/Graphics/Renderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@

namespace OpenRA.Graphics
{
public class RenderableComparer : IComparer<IRenderable>
{
WorldRenderer wr;
public RenderableComparer(WorldRenderer wr)
{
this.wr = wr;
}

public int Compare(IRenderable x, IRenderable y)
{
var xOrder = wr.ScreenZPosition(x.Pos, x.ZOffset);
var yOrder = wr.ScreenZPosition(y.Pos, y.ZOffset);
return xOrder.CompareTo(yOrder);
}
}

public interface IRenderable
{
WPos Pos { get; }
Expand Down
13 changes: 10 additions & 3 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public PaletteReference(string name, int index, IPalette palette)

public sealed class WorldRenderer : IDisposable
{
public static readonly Func<IRenderable, int> RenderableScreenZPositionComparisonKey =
r => ZPosition(r.Pos, r.ZOffset);

public readonly World World;
public readonly Theater Theater;
public Viewport Viewport { get; private set; }
Expand Down Expand Up @@ -69,7 +72,6 @@ PaletteReference CreatePaletteReference(string name)

List<IRenderable> GenerateRenderables()
{
var comparer = new RenderableComparer(this);
var actors = World.ScreenMap.ActorsInBox(Viewport.TopLeft, Viewport.BottomRight)
.Append(World.WorldActor)
.ToList();
Expand All @@ -82,7 +84,7 @@ List<IRenderable> GenerateRenderables()
if (World.OrderGenerator != null)
worldRenderables = worldRenderables.Concat(World.OrderGenerator.Render(this, World));

worldRenderables = worldRenderables.OrderBy(r => r, comparer);
worldRenderables = worldRenderables.OrderBy(RenderableScreenZPositionComparisonKey);

// Effects are drawn on top of all actors
// HACK: Effects aren't interleaved with actors.
Expand Down Expand Up @@ -241,7 +243,12 @@ public int2 ScreenPxOffset(WVec vec)
public float ScreenZPosition(WPos pos, int offset)
{
var ts = Game.ModData.Manifest.TileSize;
return (pos.Y + pos.Z + offset) * ts.Height / 1024f;
return ZPosition(pos, offset) * ts.Height / 1024f;
}

static int ZPosition(WPos pos, int offset)
{
return pos.Y + pos.Z + offset;
}

public WPos Position(int2 screenPx)
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.RA/Orders/PlaceBuildingOrderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
initialized = true;
}

var comparer = new RenderableComparer(wr);
var offset = world.Map.CenterOfCell(topLeft) + FootprintUtils.CenterOffset(world, buildingInfo);
var previewRenderables = preview
.SelectMany(p => p.Render(wr, offset))
.OrderBy(r => r, comparer);
.OrderBy(WorldRenderer.RenderableScreenZPositionComparisonKey);

foreach (var r in previewRenderables)
yield return r;
Expand Down

0 comments on commit 507fe66

Please sign in to comment.