diff --git a/Source/MonoGame.Extended/Graphics/SpriteBatchExtensions.cs b/Source/MonoGame.Extended/Graphics/SpriteBatchExtensions.cs index 1558c2cc..dfec3bcc 100644 --- a/Source/MonoGame.Extended/Graphics/SpriteBatchExtensions.cs +++ b/Source/MonoGame.Extended/Graphics/SpriteBatchExtensions.cs @@ -12,9 +12,10 @@ namespace MonoGame.Extended.Graphics spriteBatch.Draw(textureRegion.Texture, position, sourceRectangle, color); } - public static void Draw(this SpriteBatch spriteBatch, Sprite sprite, Color color) + public static void Draw(this SpriteBatch spriteBatch, Sprite sprite) { - spriteBatch.Draw(sprite.Texture, sprite.Position, color); + if (sprite.IsVisible) + spriteBatch.Draw(sprite.TextureRegion.Texture, sprite.Position, sprite.GetBoundingRectangle(), sprite.TextureRegion.Bounds, sprite.Origin, sprite.Rotation, sprite.Scale, sprite.Color); } public static void Draw(this SpriteBatch spriteBatch, TextureRegion2D textureRegion, Rectangle destinationRectangle, Color color) diff --git a/Source/MonoGame.Extended/Sprites/Sprite.cs b/Source/MonoGame.Extended/Sprites/Sprite.cs index 384372d7..3627664c 100644 --- a/Source/MonoGame.Extended/Sprites/Sprite.cs +++ b/Source/MonoGame.Extended/Sprites/Sprite.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.Graphics; using System; using System.Collections.Generic; using System.Linq; @@ -9,24 +10,69 @@ namespace MonoGame.Extended.Sprites { public class Sprite { - public Texture2D Texture; - public Vector2 Position = Vector2.Zero; + public TextureRegion2D TextureRegion; + public Color Color { get; set; } + public bool IsVisible { get; set; } - public Vector2 Scale = Vector2.One; - - public Rectangle Bounds - { - get { return new Rectangle((int)Position.X, (int)Position.Y, Texture.Bounds.Width, Texture.Bounds.Height); } - } + public Vector2 Position { get; set; } + public Vector2 Scale { get; set; } + public float Rotation { get; set; } + public Vector2 Origin { get; set; } public Sprite(Texture2D texture) { - Texture = texture; + TextureRegion = new TextureRegion2D(texture); + Scale = Vector2.One; } public Sprite(Texture2D texture, Vector2 position) : this(texture) { Position = position; } + + public Sprite(Texture2D texture, Vector2 position, Color color) : this(texture, position) + { + Color = color; + } + + public Sprite(Texture2D texture, Vector2 position, Color? color, float? rotation, Vector2? scale) : this(texture, position) + { + // If any parameters are null, provide them with suitable defaults. + Color = color ?? Color.White; + Rotation = rotation ?? 0; + Scale = scale ?? Vector2.One; + } + + public Sprite(TextureRegion2D texture) + { + TextureRegion = texture; + Scale = Vector2.One; + } + + public Sprite(TextureRegion2D texture, Vector2 position) + : this(texture) + { + Position = position; + } + + public Sprite(TextureRegion2D texture, Vector2 position, Color color) + : this(texture, position) + { + Color = color; + } + + public Sprite(TextureRegion2D texture, Vector2 position, Color? color, float? rotation, Vector2? scale) + : this(texture, position) + { + // If any parameters are null, provide them with suitable defaults. + Color = color ?? Color.White; + Rotation = rotation ?? 0; + Scale = scale ?? Vector2.One; + } + + public Rectangle GetBoundingRectangle() + { + return new Rectangle((int)Position.X, (int)Position.Y, TextureRegion.Bounds.Width, TextureRegion.Bounds.Height); + } } }