Microsoft.Xna.Framework.Graphics.SpriteBatch.Draw C# (CSharp) Method

Draw() public method

Submit a sprite for drawing in the current batch.
This overload uses optional parameters. This overload requires only one of position and destinationRectangle been used.
Throwns if both and been used.
public Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector2 position = null, Rectangle destinationRectangle = null, Rectangle sourceRectangle = null, System.Vector2 origin = null, float rotation = 0f, System.Vector2 scale = null, System.Color color = null, SpriteEffects effects = SpriteEffects.None, float layerDepth = 0f ) : void
texture Microsoft.Xna.Framework.Graphics.Texture2D A texture.
position System.Vector2 The drawing location on screen or null if is used.
destinationRectangle Rectangle The drawing bounds on screen or null if is used.
sourceRectangle Rectangle An optional region on the texture which will be rendered. If null - draws full texture.
origin System.Vector2 An optional center of rotation. Uses if null.
rotation float An optional rotation of this sprite. 0 by default.
scale System.Vector2 An optional scale vector. Uses if null.
color System.Color An optional color mask. Uses if null.
effects SpriteEffects The optional drawing modificators. by default.
layerDepth float An optional depth of the layer of this sprite. 0 by default.
return void
        public void Draw (Texture2D texture,
                Vector2? position = null,
                Rectangle? destinationRectangle = null,
                Rectangle? sourceRectangle = null,
                Vector2? origin = null,
                float rotation = 0f,
                Vector2? scale = null,
                Color? color = null,
                SpriteEffects effects = SpriteEffects.None,
                float layerDepth = 0f)
        {

            // Assign default values to null parameters here, as they are not compile-time constants
            if(!color.HasValue)
                color = Color.White;
            if(!origin.HasValue)
                origin = Vector2.Zero;
            if(!scale.HasValue)
                scale = Vector2.One;

            // If both drawRectangle and position are null, or if both have been assigned a value, raise an error
            if((destinationRectangle.HasValue) == (position.HasValue))
            {
                throw new InvalidOperationException("Expected drawRectangle or position, but received neither or both.");
            }
            else if(position != null)
            {
                // Call Draw() using position
                Draw(texture, (Vector2)position, sourceRectangle, (Color)color, rotation, (Vector2)origin, (Vector2)scale, effects, layerDepth);
            }
            else
            {
                // Call Draw() using drawRectangle
                Draw(texture, (Rectangle)destinationRectangle, sourceRectangle, (Color)color, rotation, (Vector2)origin, effects, layerDepth);
            }
        }

Same methods

SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle destinationRectangle, System.Color color ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle destinationRectangle, Rectangle sourceRectangle, System.Color color ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle destinationRectangle, Rectangle sourceRectangle, System.Color color, float rotation, System.Vector2 origin, SpriteEffects effects, float layerDepth ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector2 position, System.Color color ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector2 position, Rectangle sourceRectangle, System.Color color ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector2 position, Rectangle sourceRectangle, System.Color color, float rotation, System.Vector2 origin, System.Vector2 scale, SpriteEffects effects, float layerDepth ) : void
SpriteBatch::Draw ( Microsoft.Xna.Framework.Graphics.Texture2D texture, System.Vector2 position, Rectangle sourceRectangle, System.Color color, float rotation, System.Vector2 origin, float scale, SpriteEffects effects, float layerDepth ) : void

Usage Example

Example #1
0
        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {
            sb.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, _rasterizerScissorsState);

            sb.Draw(frameText, new Rectangle(X, Y, width, height), Color.White);

            //if (new Rectangle(X, Y, width, height).Contains((int)GUI.InputManager.MousePosition.X, (int)GUI.InputManager.MousePosition.Y))
            //    if(GUI.InputManager.MouseManager.State.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            //        sb.Draw(backText, ClientArea, Color.Gray);
            //    else
            //        sb.Draw(backText, ClientArea, Color.LightGray);
            //else

            sb.Draw(backText, ClientArea, topColor);
            topColor = Color.White;

            Rectangle oldsc = sb.GraphicsDevice.ScissorRectangle;

            sb.GraphicsDevice.ScissorRectangle = ClientArea;

            sb.DrawString(Parent.Font, drawText, new Vector2(ClientArea.X + 3, ClientArea.Y + 3 - text_y_scroll), Color.Black); //, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            //sb.DrawString(font, drawString, new Vector2(destination.X, destination.Y - yoffset), color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0);

            sb.GraphicsDevice.ScissorRectangle = oldsc;

            sb.End();
        }
All Usage Examples Of Microsoft.Xna.Framework.Graphics.SpriteBatch::Draw