From fb0e399ab9529d0acd00975692e4344105a106f7 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 26 Nov 2010 18:23:13 +1300 Subject: [PATCH] Consolidate viewport clip calculations into one place (except for TerrainRenderer, but changing that calculation crashes my gfx card). --- OpenRA.Game/Graphics/Minimap.cs | 2 -- OpenRA.Game/Graphics/Viewport.cs | 28 +++++++++++++++-------- OpenRA.Game/Graphics/WorldRenderer.cs | 22 ++---------------- OpenRA.Game/ShroudRenderer.cs | 24 ++++++++----------- OpenRA.Game/Traits/World/ResourceLayer.cs | 14 +++--------- OpenRA.Game/Traits/World/Shroud.cs | 15 ++++++------ OpenRA.Mods.RA/Buildings/BibLayer.cs | 3 +-- OpenRA.Mods.RA/World/SmudgeLayer.cs | 3 +-- 8 files changed, 43 insertions(+), 68 deletions(-) diff --git a/OpenRA.Game/Graphics/Minimap.cs b/OpenRA.Game/Graphics/Minimap.cs index 7bdaec90a7..7b287668a8 100644 --- a/OpenRA.Game/Graphics/Minimap.cs +++ b/OpenRA.Game/Graphics/Minimap.cs @@ -136,8 +136,6 @@ namespace OpenRA.Graphics { int* c = (int*)bitmapData.Scan0; - var player = world.LocalPlayer; - foreach (var t in world.Queries.WithTraitMultiple()) { if (!world.LocalShroud.IsVisible(t.Actor)) diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 38bf341bc6..b5661c958e 100755 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -122,20 +122,30 @@ namespace OpenRA.Graphics scrollPosition = this.NormalizeScrollPosition((avgPos.ToInt2() - screenSize / 2)); } - - public Rectangle ShroudBounds( World world ) - { - if( world.LocalShroud.Disabled || !world.LocalShroud.Bounds.HasValue ) - return world.Map.Bounds; - return Rectangle.Intersect( world.LocalShroud.Bounds.Value, world.Map.Bounds ); - } - public Rectangle ViewBounds() + public Rectangle ViewBounds(World world) + { + var r = WorldBounds(world); + var left = (int)(Game.CellSize * r.Left - Game.viewport.Location.X); + var top = (int)(Game.CellSize * r.Top - Game.viewport.Location.Y); + var right = left + (int)(Game.CellSize * r.Width); + var bottom = top + (int)(Game.CellSize * r.Height); + + if (left < 0) left = 0; + if (top < 0) top = 0; + if (right > Game.viewport.Width) right = Game.viewport.Width; + if (bottom > Game.viewport.Height) bottom = Game.viewport.Height; + return new Rectangle(left, top, right - left, bottom - top); + } + + public Rectangle WorldBounds(World world) { int2 boundary = new int2(1,1); // Add a curtain of cells around the viewport to account for rounding errors var tl = ViewToWorld(int2.Zero).ToInt2() - boundary; var br = ViewToWorld(new int2(Width, Height)).ToInt2() + boundary; - return Rectangle.FromLTRB(tl.X, tl.Y, br.X, br.Y); + var view = Rectangle.Intersect(Rectangle.FromLTRB(tl.X, tl.Y, br.X, br.Y), world.Map.Bounds); + var b = world.LocalShroud.Bounds; + return (b.HasValue) ? Rectangle.Intersect(view, b.Value) : view; } } } \ No newline at end of file diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index d9e183d6f8..668a1d7d1e 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -51,27 +51,9 @@ namespace OpenRA.Graphics } } - Rectangle GetBoundsRect() - { - var r = (!world.LocalShroud.Disabled && world.LocalShroud.Bounds.HasValue)? - Rectangle.Intersect(world.LocalShroud.Bounds.Value,world.Map.Bounds) : world.Map.Bounds; - - var left = (int)(Game.CellSize * r.Left - Game.viewport.Location.X); - var top = (int)(Game.CellSize * r.Top - Game.viewport.Location.Y); - var right = left + (int)(Game.CellSize * r.Width); - var bottom = top + (int)(Game.CellSize * r.Height); - - if (left < 0) left = 0; - if (top < 0) top = 0; - if (right > Game.viewport.Width) right = Game.viewport.Width; - if (bottom > Game.viewport.Height) bottom = Game.viewport.Height; - - return new Rectangle(left, top, right - left, bottom - top); - } - IEnumerable SpritesToRender() { - var bounds = GetBoundsRect(); + var bounds = Game.viewport.ViewBounds(world); var comparer = new SpriteComparer(); bounds.Offset((int)Game.viewport.Location.X, (int)Game.viewport.Location.Y); @@ -91,7 +73,7 @@ namespace OpenRA.Graphics public void Draw() { RefreshPalette(); - var bounds = GetBoundsRect(); + var bounds = Game.viewport.ViewBounds(world); Game.Renderer.EnableScissor(bounds.Left, bounds.Top, bounds.Width, bounds.Height); terrainRenderer.Draw(this, Game.viewport); diff --git a/OpenRA.Game/ShroudRenderer.cs b/OpenRA.Game/ShroudRenderer.cs index a250c537e8..da48af59ea 100644 --- a/OpenRA.Game/ShroudRenderer.cs +++ b/OpenRA.Game/ShroudRenderer.cs @@ -116,25 +116,19 @@ namespace OpenRA fogSprites[i, j] = ChooseFog(i, j); } - var clipRect = (shroud != null && shroud.Bounds.HasValue) ? Rectangle.Intersect(shroud.Bounds.Value, map.Bounds) : map.Bounds; - clipRect = Rectangle.Intersect(Game.viewport.ViewBounds(), clipRect); - var miny = clipRect.Top; - var maxy = clipRect.Bottom; - var minx = clipRect.Left; - var maxx = clipRect.Right; - - DrawShroud( wr, minx, miny, maxx, maxy, fogSprites, "fog" ); - DrawShroud( wr, minx, miny, maxx, maxy, sprites, "shroud" ); + var clipRect = Game.viewport.WorldBounds(wr.world); + DrawShroud( wr, clipRect, fogSprites, "fog" ); + DrawShroud( wr, clipRect, sprites, "shroud" ); } - void DrawShroud( WorldRenderer wr, int minx, int miny, int maxx, int maxy, Sprite[,] s, string pal ) + void DrawShroud( WorldRenderer wr, Rectangle clip, Sprite[,] s, string pal ) { var shroudPalette = wr.GetPaletteIndex(pal); - for (var j = miny; j < maxy; j++) + for (var j = clip.Top; j < clip.Bottom; j++) { - var starti = minx; - for (var i = minx; i < maxx; i++) + var starti = clip.Left; + for (var i = clip.Left; i < clip.Right; i++) { if (s[i, j] == shadowBits[0x0f]) continue; @@ -154,11 +148,11 @@ namespace OpenRA starti = i + 1; } - if (starti < maxx) + if (starti < clip.Right) s[starti, j].DrawAt( Game.CellSize * new float2(starti, j), shroudPalette, - new float2(Game.CellSize * (maxx - starti), Game.CellSize)); + new float2(Game.CellSize * (clip.Right - starti), Game.CellSize)); } } } diff --git a/OpenRA.Game/Traits/World/ResourceLayer.cs b/OpenRA.Game/Traits/World/ResourceLayer.cs index 95e829abb9..2cf2b315cc 100644 --- a/OpenRA.Game/Traits/World/ResourceLayer.cs +++ b/OpenRA.Game/Traits/World/ResourceLayer.cs @@ -26,20 +26,12 @@ namespace OpenRA.Traits public void Render( WorldRenderer wr ) { - var cliprect = Game.viewport.ShroudBounds( world ); - cliprect = Rectangle.Intersect(Game.viewport.ViewBounds(), cliprect); - - var minx = cliprect.Left; - var maxx = cliprect.Right; - - var miny = cliprect.Top; - var maxy = cliprect.Bottom; - foreach( var rt in world.WorldActor.TraitsImplementing() ) rt.info.PaletteIndex = wr.GetPaletteIndex(rt.info.Palette); - for (int x = minx; x < maxx; x++) - for (int y = miny; y < maxy; y++) + var clip = Game.viewport.WorldBounds(world); + for (int x = clip.Left; x < clip.Right; x++) + for (int y = clip.Top; y < clip.Bottom; y++) { if (!world.LocalShroud.IsExplored(new int2(x, y))) continue; diff --git a/OpenRA.Game/Traits/World/Shroud.cs b/OpenRA.Game/Traits/World/Shroud.cs index 689d0710f5..fa7aed3825 100644 --- a/OpenRA.Game/Traits/World/Shroud.cs +++ b/OpenRA.Game/Traits/World/Shroud.cs @@ -26,7 +26,7 @@ namespace OpenRA.Traits public int[,] visibleCells; public bool[,] exploredCells; - public Rectangle? exploredBounds; + Rectangle? exploredBounds; bool disabled = false; public bool Disabled { @@ -34,7 +34,11 @@ namespace OpenRA.Traits set { disabled = value; Dirty(); } } - public Rectangle? Bounds { get { return exploredBounds; } } + public Rectangle? Bounds + { + get { return !disabled ? exploredBounds : null; } + } + public event Action Dirty = () => { }; public Shroud(World world) @@ -42,7 +46,6 @@ namespace OpenRA.Traits map = world.Map; visibleCells = new int[map.MapSize.X, map.MapSize.Y]; exploredCells = new bool[map.MapSize.X, map.MapSize.Y]; - world.ActorAdded += AddActor; world.ActorRemoved += RemoveActor; } @@ -98,8 +101,7 @@ namespace OpenRA.Traits } var box = new Rectangle(p.X - v.range, p.Y - v.range, 2 * v.range + 1, 2 * v.range + 1); - exploredBounds = exploredBounds.HasValue ? - Rectangle.Union(exploredBounds.Value, box) : box; + exploredBounds = (exploredBounds.HasValue) ? Rectangle.Union(exploredBounds.Value, box) : box; } vis[a] = v; @@ -165,8 +167,7 @@ namespace OpenRA.Traits exploredCells[q.X, q.Y] = true; var box = new Rectangle(center.X - range, center.Y - range, 2 * range + 1, 2 * range + 1); - exploredBounds = exploredBounds.HasValue ? - Rectangle.Union(exploredBounds.Value, box) : box; + exploredBounds = (exploredBounds.HasValue) ? Rectangle.Union(exploredBounds.Value, box) : box; Dirty(); } diff --git a/OpenRA.Mods.RA/Buildings/BibLayer.cs b/OpenRA.Mods.RA/Buildings/BibLayer.cs index 3fce473f61..22a3eda1c1 100755 --- a/OpenRA.Mods.RA/Buildings/BibLayer.cs +++ b/OpenRA.Mods.RA/Buildings/BibLayer.cs @@ -74,8 +74,7 @@ namespace OpenRA.Mods.RA.Buildings public void Render( WorldRenderer wr ) { - var cliprect = Game.viewport.ShroudBounds( world ); - cliprect = Rectangle.Intersect(Game.viewport.ViewBounds(), cliprect); + var cliprect = Game.viewport.WorldBounds(world); foreach (var kv in tiles) { if (!cliprect.Contains(kv.Key.X, kv.Key.Y)) diff --git a/OpenRA.Mods.RA/World/SmudgeLayer.cs b/OpenRA.Mods.RA/World/SmudgeLayer.cs index 64a948a5fd..45c4ad3e70 100755 --- a/OpenRA.Mods.RA/World/SmudgeLayer.cs +++ b/OpenRA.Mods.RA/World/SmudgeLayer.cs @@ -75,8 +75,7 @@ namespace OpenRA.Mods.RA public void Render( WorldRenderer wr ) { - var cliprect = Game.viewport.ShroudBounds( world ); - cliprect = Rectangle.Intersect(Game.viewport.ViewBounds(), cliprect); + var cliprect = Game.viewport.WorldBounds(world); var localPlayer = world.LocalPlayer; foreach (var kv in tiles) {