Revert to the old tiling logic. Insert texture scale logic in between. Turn the silhouette code block into a separate method.

This commit is contained in:
itchyOwl
2018-04-09 09:54:26 +03:00
parent 22c3af6af6
commit a34a024f58
2 changed files with 72 additions and 22 deletions

View File

@@ -175,7 +175,7 @@ namespace Barotrauma
while (offsetS.Y > 0.0f) offsetS.Y -= srcRect.Height * scale;
dustParticles.DrawTiled(spriteBatch, origin + offsetS, new Vector2(cam.WorldView.Width - offsetS.X, cam.WorldView.Height - offsetS.Y),
sourceRect: srcRect, color: Color.White * alpha, textureScale: new Vector2(scale));
rect: srcRect, color: Color.White * alpha, textureScale: new Vector2(scale));
}
spriteBatch.End();

View File

@@ -89,59 +89,109 @@ namespace Barotrauma
public virtual void Draw(SpriteBatch spriteBatch, Vector2 pos, Color color, Vector2 origin, float rotate, Vector2 scale, SpriteEffects spriteEffect = SpriteEffects.None, float? depth = null)
{
// Creates a silhouette for the sprite (or outline if the sprite is rendered on top of it) -> don't remove
//for (int x = -1; x <= 1; x += 2)
//{
// for (int y = -1; y <= 1; y += 2)
// {
// spriteBatch.Draw(texture, pos + offset + new Vector2(x, y) * 1.0f, sourceRect, Color.Black, rotation + rotate, origin, scale, spriteEffect, (depth == null ? this.depth : (float)depth) + 0.0001f);
// }
//}
if (texture == null) return;
//DrawSilhouette(spriteBatch, pos, origin, rotate, scale, spriteEffect, depth);
spriteBatch.Draw(texture, pos + offset, sourceRect, color, rotation + rotate, origin, scale, spriteEffect, depth ?? this.depth);
}
/// <summary>
/// Creates a silhouette for the sprite (or outline if the sprite is rendered on top of it)
/// </summary>
public void DrawSilhouette(SpriteBatch spriteBatch, Vector2 pos, Vector2 origin, float rotate, Vector2 scale, SpriteEffects spriteEffect = SpriteEffects.None, float? depth = null)
{
if (texture == null) return;
for (int x = -1; x <= 1; x += 2)
{
for (int y = -1; y <= 1; y += 2)
{
spriteBatch.Draw(texture, pos + offset + new Vector2(x, y), sourceRect, Color.Black, rotation + rotate, origin, scale, spriteEffect, (depth ?? this.depth) + 0.01f);
}
}
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize,
Rectangle? sourceRect = null, Color? color = null, Point? startOffset = null, Vector2? textureScale = null, float? depth = null)
Rectangle? rect = null, Color? color = null, Point? startOffset = null, Vector2? textureScale = null, float? depth = null)
{
if (texture == null) return;
// Init optional values, if not provided
Rectangle rect = sourceRect ?? this.sourceRect;
if (rect.HasValue)
{
sourceRect = rect.Value;
}
color = color ?? Color.White;
startOffset = startOffset ?? Point.Zero;
Vector2 scale = textureScale ?? Vector2.One;
Rectangle texPerspective = rect;
Rectangle texPerspective = sourceRect;
texPerspective.Location += startOffset.Value;
targetSize = targetSize / scale;
//how many times the texture needs to be drawn on the x-axis
int xTiles = (int)Math.Ceiling(targetSize.X / rect.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 / rect.Height);
int yTiles = (int)Math.Ceiling(targetSize.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);
float top = pos.Y;
texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height);
for (int y = 0; y < yTiles; y++)
{
float movementY = texPerspective.Height * scale.Y;
texPerspective.Height = Math.Min((int)(targetSize.Y - texPerspective.Height * y), texPerspective.Height);
float left = pos.X;
texPerspective.Width = (int)Math.Min(targetSize.X, rect.Width);
texPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width);
for (int x = 0; x < xTiles; x++)
{
float movementX = texPerspective.Width * scale.X;
texPerspective.Width = Math.Min((int)(targetSize.X - texPerspective.Width * x), texPerspective.Width);
spriteBatch.Draw(texture, new Vector2(left, top), texPerspective, color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
if (texPerspective.X + movementX >= rect.Right)
if (texPerspective.Right > sourceRect.Right)
{
texPerspective.X = rect.X;
int diff = texPerspective.Right - sourceRect.Right;
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
{
spriteBatch.Draw(texture,
new Vector2(left, top),
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
texPerspective.Width -= diff;
left += diff;
}
else
{
texPerspective.Width -= diff;
spriteBatch.Draw(texture,
new Vector2(left + texPerspective.Width, top),
new Rectangle(sourceRect.X, texPerspective.Y, (int)diff, texPerspective.Height),
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
}
}
else if (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),
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
}
spriteBatch.Draw(texture, new Vector2(left, top), texPerspective, color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
if (texPerspective.X + movementX >= sourceRect.Right) texPerspective.X = sourceRect.X;
left += movementX;
}
if (texPerspective.Y + movementY >= rect.Bottom)
{
texPerspective.Y = rect.Y;
}
if (texPerspective.Y + movementY >= sourceRect.Bottom) texPerspective.Y = sourceRect.Y;
top += movementY;
}
}