Simple MapEntity visibility culling (structures outside the camera view or items with nothing to render aren't drawn)

Could be optimized further, but already gives a pretty nice performance boost
This commit is contained in:
Regalis
2016-11-30 21:09:11 +02:00
parent 42ff4a0d9a
commit a2442bfcb8
5 changed files with 61 additions and 41 deletions

View File

@@ -223,79 +223,79 @@ namespace Barotrauma
DrawTiled(spriteBatch, pos, targetSize, Vector2.Zero, color);
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Color color, Point offset, float? overrideDepth = null)
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Color color, Point offset, float? overrideDepth = null)
{
//how many times the texture needs to be drawn on the x-axis
int xTiles = (int)Math.Ceiling((targetSize.X + startOffset.X) / sourceRect.Width);
int xTiles = (int)Math.Ceiling(targetSize.X / sourceRect.Width);
//how many times the texture needs to be drawn on the y-axis
int yTiles = (int)Math.Ceiling((targetSize.Y + startOffset.Y) / sourceRect.Height);
int yTiles = (int)Math.Ceiling(targetSize.Y / sourceRect.Height);
float depth = overrideDepth == null ? this.depth : (float)overrideDepth;
Rectangle texPerspective = sourceRect;
texPerspective.Location += offset;
while (texPerspective.Location.X >= sourceRect.X + sourceRect.Width)
texPerspective.X = sourceRect.X + (texPerspective.Location.X - (sourceRect.X + sourceRect.Width));
while (texPerspective.Location.Y >= sourceRect.Y + sourceRect.Height)
texPerspective.Y = sourceRect.Y + (texPerspective.Location.Y - (sourceRect.Y + sourceRect.Height));
while (texPerspective.X >= sourceRect.Right)
texPerspective.X = sourceRect.X + (texPerspective.X - sourceRect.Right);
while (texPerspective.Y >= sourceRect.Bottom)
texPerspective.Y = sourceRect.Y + (texPerspective.Y - sourceRect.Bottom);
texPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width);
float top = pos.Y;
texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height);
for (int y = 0; y < yTiles; y++)
{
var movementY = texPerspective.Height;
texPerspective.Height = Math.Min((int)(targetSize.Y - texPerspective.Height * y), texPerspective.Height);
float left = pos.X;
texPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width);
texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height);
float top = pos.Y + texPerspective.Height * y;
for (int x = 0; x < xTiles; x++)
{
float left = pos.X + texPerspective.Width * x;
texPerspective.Width = Math.Min((int)(targetSize.X - texPerspective.Width * x), texPerspective.Width);
texPerspective.Height = Math.Min((int)(targetSize.Y - texPerspective.Height * y), texPerspective.Height);
var movementX = texPerspective.Width;
var movementY = texPerspective.Height;
texPerspective.Width = Math.Min((int)(targetSize.X - texPerspective.Width * x), texPerspective.Width);
if (texPerspective.Right > sourceRect.Right)
{
int diff = texPerspective.Right - sourceRect.Right;
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
{
int diff = (texPerspective.X + texPerspective.Width) - (sourceRect.Right);
spriteBatch.Draw(texture,
new Vector2(left, top),
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
spriteBatch.Draw(texture,
new Vector2(left, top),
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
color, rotation, Vector2.Zero, 1.0f, effects, depth);
texPerspective.Width -= diff;
left += diff;
}
else
{
float diff = (texPerspective.X + texPerspective.Width) - (sourceRect.X + sourceRect.Width);
texPerspective.Width -= (int)diff;
spriteBatch.Draw(texture,
new Vector2(left + texPerspective.Width, top),
new Rectangle(sourceRect.X, texPerspective.Y, (int)diff, texPerspective.Height),
color, rotation, Vector2.Zero, 1.0f, effects, depth);
spriteBatch.Draw(texture,
new Vector2(left + texPerspective.Width, top),
new Rectangle(sourceRect.X, texPerspective.Y, (int)diff, texPerspective.Height),
color, rotation, Vector2.Zero, 1.0f, effects, depth);
}
}
else if (texPerspective.Bottom > sourceRect.Bottom)
{
int diff = (texPerspective.Bottom) - (sourceRect.Bottom);
int diff = texPerspective.Bottom - sourceRect.Bottom;
texPerspective.Height -= diff;
spriteBatch.Draw(texture,
new Vector2(left, top + texPerspective.Height),
new Rectangle(texPerspective.X, sourceRect.Y, texPerspective.Width, diff),
spriteBatch.Draw(texture,
new Vector2(left, top + texPerspective.Height),
new Rectangle(texPerspective.X, sourceRect.Y, texPerspective.Width, diff),
color, rotation, Vector2.Zero, 1.0f, effects, depth);
}
spriteBatch.Draw(texture, new Vector2(left, top), texPerspective, color, rotation, Vector2.Zero, 1.0f, effects, depth);
if (texPerspective.X + movementX >= sourceRect.Right)
texPerspective.X = sourceRect.X;
if (texPerspective.Y + movementY >= sourceRect.Y + sourceRect.Height)
texPerspective.Y = sourceRect.Y;
if (texPerspective.X + movementX >= sourceRect.Right) texPerspective.X = sourceRect.X;
left += movementX;
}
if (texPerspective.Y + movementY >= sourceRect.Bottom) texPerspective.Y = sourceRect.Y;
top += movementY;
}
}