Skip to content

Commit

Permalink
Consolidate viewport clip calculations into one place (except for Ter…
Browse files Browse the repository at this point in the history
…rainRenderer, but changing that calculation crashes my gfx card).
  • Loading branch information
pchote committed Nov 26, 2010
1 parent 7c5c989 commit fb0e399
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 67 deletions.
2 changes: 0 additions & 2 deletions OpenRA.Game/Graphics/Minimap.cs
Expand Up @@ -136,8 +136,6 @@ public static Bitmap ActorsBitmap(World world)
{
int* c = (int*)bitmapData.Scan0;

var player = world.LocalPlayer;

foreach (var t in world.Queries.WithTraitMultiple<IRadarSignature>())
{
if (!world.LocalShroud.IsVisible(t.Actor))
Expand Down
26 changes: 18 additions & 8 deletions OpenRA.Game/Graphics/Viewport.cs
Expand Up @@ -122,20 +122,30 @@ public void Center(IEnumerable<Actor> actors)

scrollPosition = this.NormalizeScrollPosition((avgPos.ToInt2() - screenSize / 2));
}

public Rectangle ShroudBounds( World world )
public Rectangle ViewBounds(World world)
{
if( world.LocalShroud.Disabled || !world.LocalShroud.Bounds.HasValue )
return world.Map.Bounds;
return Rectangle.Intersect( world.LocalShroud.Bounds.Value, world.Map.Bounds );
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 ViewBounds()

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;
}
}
}
22 changes: 2 additions & 20 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Expand Up @@ -51,27 +51,9 @@ public int Compare(Renderable x, Renderable y)
}
}

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<Renderable> 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);
Expand All @@ -91,7 +73,7 @@ IEnumerable<Renderable> SpritesToRender()
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);
Expand Down
24 changes: 9 additions & 15 deletions OpenRA.Game/ShroudRenderer.cs
Expand Up @@ -116,25 +116,19 @@ internal void Draw( WorldRenderer wr )
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;
Expand All @@ -154,11 +148,11 @@ void DrawShroud( WorldRenderer wr, int minx, int miny, int maxx, int maxy, Sprit
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));
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions OpenRA.Game/Traits/World/ResourceLayer.cs
Expand Up @@ -26,20 +26,12 @@ public class ResourceLayer: IRenderOverlay, IWorldLoaded

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<ResourceType>() )
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;
Expand Down
15 changes: 8 additions & 7 deletions OpenRA.Game/Traits/World/Shroud.cs
Expand Up @@ -26,23 +26,26 @@ public class Shroud

public int[,] visibleCells;
public bool[,] exploredCells;
public Rectangle? exploredBounds;
Rectangle? exploredBounds;
bool disabled = false;
public bool Disabled
{
get { return disabled; }
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)
{
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;
}
Expand Down Expand Up @@ -98,8 +101,7 @@ void AddActor(Actor a)
}

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;
Expand Down Expand Up @@ -165,8 +167,7 @@ public void Explore(World world, int2 center, int range)
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();
}
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.RA/Buildings/BibLayer.cs
Expand Up @@ -74,8 +74,7 @@ public void DoBib(Actor b, bool isAdd)

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))
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.RA/World/SmudgeLayer.cs
Expand Up @@ -75,8 +75,7 @@ public void AddSmudge(int2 loc)

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)
{
Expand Down

0 comments on commit fb0e399

Please sign in to comment.