Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tie breaks to determine sort order in WorldRenderer.GenerateRenderables #18774

Merged
merged 1 commit into from Nov 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion OpenRA.Game/Graphics/WorldRenderer.cs
Expand Up @@ -133,7 +133,26 @@ void GenerateRenderables()
foreach (var e in World.ScreenMap.RenderableEffectsInBox(Viewport.TopLeft, Viewport.BottomRight))
renderablesBuffer.AddRange(e.Render(this));

renderablesBuffer.Sort((x, y) => RenderableZPositionComparisonKey(x).CompareTo(RenderableZPositionComparisonKey(y)));
renderablesBuffer.Sort((a, b) =>
{
// Sort is an unstable sort, so elements with the same Z position may get sorted differently each frame.
// This leads to flickering when rendering several frames as the elements get swapped at random.
// To combat this, use a series of arbitrary tie-breaks to determine the "top-most" element.
// This ensures a consistent decision for which one is on top, preventing flicker.
var compared = RenderableZPositionComparisonKey(a).CompareTo(RenderableZPositionComparisonKey(b));
if (compared != 0)
return compared;

compared = a.Pos.X.CompareTo(b.Pos.X);
if (compared != 0)
return compared;

compared = a.Pos.Y.CompareTo(b.Pos.Y);
if (compared != 0)
return compared;

return a.Pos.Z.CompareTo(b.Pos.Z);
});

foreach (var renderable in renderablesBuffer)
preparedRenderables.Add(renderable.PrepareRender(this));
Expand Down