Use a single method with optional parameters for drawing tiled textures.

This commit is contained in:
itchyOwl
2018-04-06 11:54:41 +03:00
parent e89fa0dc5c
commit b86b8cd377
10 changed files with 45 additions and 153 deletions

View File

@@ -295,8 +295,7 @@ namespace Barotrauma
float alpha = character.IsUnconscious ? 1.0f : Math.Min((80.0f - character.Oxygen)/50.0f, 0.8f);
noiseOverlay.DrawTiled(spriteBatch, Vector2.Zero - offset, new Vector2(GameMain.GraphicsWidth, GameMain.GraphicsHeight) + offset,
Vector2.Zero,
Color.White * alpha);
color: Color.White * alpha);
}
else

View File

@@ -402,7 +402,7 @@ namespace Barotrauma
if (uiSprite.Sprite.size.X == 0.0f) size.X = rect.Width;
if (uiSprite.Sprite.size.Y == 0.0f) size.Y = rect.Height;
uiSprite.Sprite.DrawTiled(spriteBatch, startPos, size, currColor * (currColor.A / 255.0f));
uiSprite.Sprite.DrawTiled(spriteBatch, startPos, size, color: currColor * (currColor.A / 255.0f));
}
else
{

View File

@@ -105,7 +105,7 @@ namespace Barotrauma
{
if (uiSprite.Tile)
{
uiSprite.Sprite.DrawTiled(spriteBatch, slider.Rect.Location.ToVector2(), slider.Rect.Size.ToVector2(), currColor);
uiSprite.Sprite.DrawTiled(spriteBatch, slider.Rect.Location.ToVector2(), slider.Rect.Size.ToVector2(), color: currColor);
}
else
{

View File

@@ -81,8 +81,9 @@ namespace Barotrauma
{
if (prefab.ResizeHorizontal || prefab.ResizeVertical || SpriteEffects.HasFlag(SpriteEffects.FlipHorizontally) || SpriteEffects.HasFlag(SpriteEffects.FlipVertically))
{
selectedSprite.DrawTiled(spriteBatch, new Vector2(DrawPosition.X - rect.Width / 2, -(DrawPosition.Y + rect.Height / 2)), new Vector2(rect.Width, rect.Height), color);
fadeInBrokenSprite?.Sprite.DrawTiled(spriteBatch, new Vector2(DrawPosition.X - rect.Width / 2, -(DrawPosition.Y + rect.Height / 2)), new Vector2(rect.Width, rect.Height), color * fadeInBrokenSpriteAlpha, Point.Zero, selectedSprite.Depth - 0.000001f);
selectedSprite.DrawTiled(spriteBatch, new Vector2(DrawPosition.X - rect.Width / 2, -(DrawPosition.Y + rect.Height / 2)), new Vector2(rect.Width, rect.Height), color: color);
fadeInBrokenSprite?.Sprite.DrawTiled(spriteBatch, new Vector2(DrawPosition.X - rect.Width / 2, -(DrawPosition.Y + rect.Height / 2)), new Vector2(rect.Width, rect.Height), color: color * fadeInBrokenSpriteAlpha,
depth: selectedSprite.Depth - 0.000001f);
}
else

View File

@@ -70,7 +70,7 @@ namespace Barotrauma
position = placePosition;
}
if (sprite != null) sprite.DrawTiled(spriteBatch, new Vector2(position.X, -position.Y), placeSize, SpriteColor);
if (sprite != null) sprite.DrawTiled(spriteBatch, new Vector2(position.X, -position.Y), placeSize, color: SpriteColor);
}
}
}

View File

@@ -129,7 +129,7 @@ namespace Barotrauma
{
backgroundTop.SourceRect = new Rectangle((int)backgroundPos.X, (int)backgroundPos.Y, 1024, (int)Math.Min(-backgroundPos.Y, 1024));
backgroundTop.DrawTiled(spriteBatch, Vector2.Zero, new Vector2(GameMain.GraphicsWidth, Math.Min(-backgroundPos.Y, GameMain.GraphicsHeight)),
Vector2.Zero, level.BackgroundColor);
color: level.BackgroundColor);
}
if (backgroundPos.Y > -1024)
{
@@ -137,7 +137,7 @@ namespace Barotrauma
background.DrawTiled(spriteBatch,
(backgroundPos.Y < 0) ? new Vector2(0.0f, (int)-backgroundPos.Y) : Vector2.Zero,
new Vector2(GameMain.GraphicsWidth, (int)Math.Ceiling(1024 - backgroundPos.Y)),
Vector2.Zero, level.BackgroundColor);
color: level.BackgroundColor);
}
}
@@ -174,7 +174,8 @@ namespace Barotrauma
while (offsetS.Y <= -srcRect.Height * scale) offsetS.Y += srcRect.Height * scale;
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), Vector2.Zero, srcRect, Color.White * alpha, new Vector2(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));
}
spriteBatch.End();

View File

@@ -74,7 +74,7 @@ namespace Barotrauma
Rectangle prevScissorRect = GameMain.Instance.GraphicsDevice.ScissorRectangle;
GameMain.Instance.GraphicsDevice.ScissorRectangle = rect;
iceTexture.DrawTiled(spriteBatch, new Vector2(rect.X, rect.Y), new Vector2(rect.Width, rect.Height), Vector2.Zero, Color.White * 0.8f);
iceTexture.DrawTiled(spriteBatch, new Vector2(rect.X, rect.Y), new Vector2(rect.Width, rect.Height), color: Color.White * 0.8f);
foreach (LocationConnection connection in connections)
{

View File

@@ -133,7 +133,7 @@ namespace Barotrauma
spriteBatch,
new Vector2(rect.X + drawOffset.X, -(rect.Y + drawOffset.Y)),
new Vector2(rect.Width, rect.Height),
color, Point.Zero);
color: color);
}
}
@@ -175,8 +175,9 @@ namespace Barotrauma
spriteBatch,
new Vector2(sections[i].rect.X + drawOffset.X, -(sections[i].rect.Y + drawOffset.Y)),
new Vector2(sections[i].rect.Width, sections[i].rect.Height),
color,
textureOffset, depth);
color: color,
startOffset: textureOffset,
depth: depth);
}
}

View File

@@ -32,7 +32,7 @@ namespace Barotrauma
newRect = Submarine.AbsRect(placePosition, placeSize);
}
sprite.DrawTiled(spriteBatch, new Vector2(newRect.X, -newRect.Y), new Vector2(newRect.Width, newRect.Height), Color.White);
sprite.DrawTiled(spriteBatch, new Vector2(newRect.X, -newRect.Y), new Vector2(newRect.Width, newRect.Height));
GUI.DrawRectangle(spriteBatch, new Rectangle(newRect.X - GameMain.GraphicsWidth, -newRect.Y, newRect.Width + GameMain.GraphicsWidth * 2, newRect.Height), Color.White);
GUI.DrawRectangle(spriteBatch, new Rectangle(newRect.X, -newRect.Y - GameMain.GraphicsHeight, newRect.Width, newRect.Height + GameMain.GraphicsHeight * 2), Color.White);

View File

@@ -89,181 +89,71 @@ 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;
spriteBatch.Draw(texture, pos + offset, sourceRect, color, rotation + rotate, origin, scale, spriteEffect, depth == null ? this.depth : (float)depth);
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Color color)
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize,
Rectangle? sourceRect = null, Color? color = null, Point? startOffset = null, Vector2? textureScale = null, float? depth = null)
{
DrawTiled(spriteBatch, pos, targetSize, Vector2.Zero, color);
}
// Init optional values, if not provided
Rectangle rect = sourceRect ?? this.sourceRect;
color = color ?? Color.White;
startOffset = startOffset ?? Point.Zero;
Vector2 scale = textureScale ?? Vector2.One;
depth = depth ?? this.depth;
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Color color, Point offset, float? overrideDepth = null)
{
targetSize = targetSize / scale;
//how many times the texture needs to be drawn on the x-axis
int xTiles = (int)Math.Ceiling(targetSize.X / sourceRect.Width);
int xTiles = (int)Math.Ceiling(targetSize.X / rect.Width);
//how many times the texture needs to be drawn on the y-axis
int yTiles = (int)Math.Ceiling(targetSize.Y / sourceRect.Height);
int yTiles = (int)Math.Ceiling(targetSize.Y / rect.Height);
float depth = overrideDepth == null ? this.depth : (float)overrideDepth;
Rectangle texPerspective = sourceRect;
texPerspective.Location += offset;
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);
Rectangle texPerspective = rect;
texPerspective.Location += startOffset.Value;
while (texPerspective.X >= rect.Right)
texPerspective.X = rect.X + (texPerspective.X - rect.Right);
while (texPerspective.Y >= rect.Bottom)
texPerspective.Y = rect.Y + (texPerspective.Y - rect.Bottom);
float top = pos.Y;
texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height);
texPerspective.Height = (int)Math.Min(targetSize.Y, rect.Height);
for (int y = 0; y < yTiles; y++)
{
var movementY = texPerspective.Height;
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, sourceRect.Width);
texPerspective.Width = (int)Math.Min(targetSize.X, rect.Width);
for (int x = 0; x < xTiles; x++)
{
var movementX = texPerspective.Width;
float movementX = texPerspective.Width * scale.X;
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))
{
spriteBatch.Draw(texture,
new Vector2(left, top),
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
color, rotation, Vector2.Zero, 1.0f, effects, depth);
spriteBatch.Draw(texture, new Vector2(left, top), texPerspective, color.Value, rotation, Vector2.Zero, scale, effects, depth.Value);
texPerspective.Width -= diff;
left += diff;
}
else
{
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);
}
}
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, 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.X + movementX >= rect.Right) texPerspective.X = rect.X;
left += movementX;
}
if (texPerspective.Y + movementY >= sourceRect.Bottom) texPerspective.Y = sourceRect.Y;
if (texPerspective.Y + movementY >= rect.Bottom)
{
texPerspective.Y = rect.Y;
}
top += movementY;
}
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Color color)
{
DrawTiled(spriteBatch, pos, targetSize, startOffset, sourceRect, color, Vector2.One);
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Rectangle sourceRect, Color color)
{
DrawTiled(spriteBatch, pos, targetSize, startOffset, sourceRect, color, Vector2.One);
}
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Rectangle sourceRect, Color color, Vector2 scale)
{
//pos.X = (int)pos.X;
//pos.Y = (int)pos.Y;
//how many times the texture needs to be drawn on the x-axis
int xTiles = (int)Math.Ceiling((targetSize.X + startOffset.X) / (sourceRect.Width*scale.X));
//how many times the texture needs to be drawn on the y-axis
int yTiles = (int)Math.Ceiling((targetSize.Y + startOffset.Y) / (sourceRect.Height*scale.Y));
Vector2 position = pos - startOffset;
Rectangle drawRect = sourceRect;
position.X = pos.X;
for (int x = 0; x < xTiles; x++)
{
drawRect.X = sourceRect.X;
drawRect.Height = sourceRect.Height;
if (x == xTiles - 1)
{
drawRect.Width -= (int)((position.X + sourceRect.Width*scale.X) - (pos.X + targetSize.X));
}
else
{
drawRect.Width = (int)(sourceRect.Width*scale.X);
}
if (position.X < pos.X)
{
float diff = pos.X - position.X;
position.X += diff;
drawRect.Width -= (int)diff;
drawRect.X += (int)diff;
}
position.Y = pos.Y;
for (int y = 0; y < yTiles; y++)
{
drawRect.Y = sourceRect.Y;
if (y == yTiles - 1)
{
drawRect.Height -= (int)((position.Y + sourceRect.Height*scale.Y) - (pos.Y + targetSize.Y));
}
else
{
drawRect.Height = (int)(sourceRect.Height*scale.Y);
}
if (position.Y < pos.Y)
{
int diff = (int)(pos.Y - position.Y);
position.Y += diff;
drawRect.Height -= diff;
drawRect.Y += diff;
}
spriteBatch.Draw(texture, position,
drawRect, color, rotation, Vector2.Zero, 1.0f, effects, depth);
position.Y += sourceRect.Height*scale.Y;
}
position.X += sourceRect.Width*scale.X;
}
}
partial void DisposeTexture()
{
//check if another sprite is using the same texture