/// <summary> /// Creates a sprite with default settings. /// </summary> /// <param name="textureUri">The relative project path for a texture.</param> public SpriteBatchSprite(string textureUri) { cachedColorAlphaBlend = Color.White; cachedOrigin = Vector2.Zero; cachedScale = Vector2.One; cachedSize = new Tuple <int, int>(0, 0); cachedTexture = null; cachedSrcRect = Rectangle.Empty; cachedDestRect = Rectangle.Empty; origin = SpriteOrigin.TopLeft; performBlendingPremultiplied = true; position = Vector2.Zero; size = SpriteSize.Empty; textureAlphaBlend = 1f; textureColorBlend = Color.White; textureSourceRect = SmoothRect.Empty; this.textureUri = textureUri; Angle = 0f; Depth = 0f; TextureMirroring = SpriteEffects.None; UseDepthInDraw = false; // Computes all cached values. cachedTexture = Texture2DManager.Get(textureUri); bool isCachedTextureLoaded = cachedTexture != null && !cachedTexture.IsDisposed; ComputeColorAlphaBlend(); if (Origin.Kind != SpriteOriginKind.Percentile || isCachedTextureLoaded) { ComputeOrigin(); } if (Size.IsEmpty || Size.Kind == SpriteSizeKind.Scaling || isCachedTextureLoaded) { ComputeScale(); } if (Size.Kind != SpriteSizeKind.Scaling || isCachedTextureLoaded) { ComputeSizeAndDestRectangle(); } if (!TextureSourceRect.IsEmpty || isCachedTextureLoaded) { ComputeSourceRectangle(); } }
/// <summary> /// Returns the cached texture, loading it from disk if a content manager is provided and /// it's not available. Returns null if an error occurs or the content manager is null /// when the texture is not cached. /// </summary> /// <param name="mngr"> /// The content manager to load textures from disk when not available, if provided. If a /// texture is not cached and no content manager is provided, null will be returned. /// </param> /// <returns> /// The <see cref="Texture2D"/> corresponding to the sprite. /// </returns> public Texture2D GetTexture(ContentManager mngr = null) { return(Texture2DManager.Get(textureUri, mngr)); }
/// <summary> /// Draws the sprite using cached values computed when properties change. /// </summary> /// <param name="spriteBatch"> /// A <see cref="SpriteBatch"/> instance to draw with. Expects Begin() to have been called. /// </param> /// <param name="mngr"> /// The content manager to load textures from disk when not available, if provided. If a /// texture is not cached and no content manager is provided, it will fail to draw. /// </param> /// <returns> /// False if the sprite is null or disposed and cannot be drawn, else true. /// </returns> public bool Draw(SpriteBatch spriteBatch, ContentManager mngr = null) { // Fetches the texture if necessary. if (cachedTexture == null) { cachedTexture = Texture2DManager.Get(textureUri, mngr); if (cachedTexture == null || cachedTexture.IsDisposed) { return(false); } RecomputeCache(); } else if (cachedTexture.IsDisposed) { return(false); } // Draws the texture using cached values. if (RequiresFullDrawOverload()) { // Texture, position, source, color, rotation, origin, scale, effects, depth spriteBatch.Draw( cachedTexture, Position, cachedSrcRect, cachedColorAlphaBlend, Angle, cachedOrigin, cachedScale, TextureMirroring, Depth); } else if (!TextureSourceRect.IsEmpty) { if (!Size.IsEmpty) { // Texture, position, size, source, color spriteBatch.Draw( cachedTexture, cachedDestRect, cachedSrcRect, cachedColorAlphaBlend); } else { // Texture, position, source, color spriteBatch.Draw( cachedTexture, Position, cachedSrcRect, cachedColorAlphaBlend); } } else { if (!Size.IsEmpty) { // Texture, position, size, color spriteBatch.Draw( cachedTexture, cachedDestRect, cachedColorAlphaBlend); } else { // Texture, position, color spriteBatch.Draw( cachedTexture, Position, cachedColorAlphaBlend); } } return(true); }