Using Sprites

The XNA SpriteBatch class is nice and lets you draw pretty pictures to the screen, but do I really need to keep track of all that Texture2D, position, rotation, scale, and collision information separately?

Simple Sprites

Xen takes care of handling memory allocations and garbage for you with its integrated object pooling system. To use sprites with Xen, you can Acquire() an sprite instance of a given texture image and use it to transform and move the image as you wish. Once you are done with the instance, you can call Release() on that instance, or it will automatically release itself after the game has exited.
        StaticSprite _spriteRed = null, _spriteBlue = null;

        protected override void LoadContent()
        {
            // ... code ...
            _spriteRed = StaticSprite.Acquire( Content.Load<Texture2D>("Textures/RedRectangle") );
            _spriteBlue = StaticSprite.Acquire( Content.Load<Texture2D>("Textures/BlueRectangle") );
        }
        protected override void UnloadContent()
        {
            // ... code ...
            _spriteRed.Release();
            _spriteBlue.Release();
        }
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            _spriteBatch.Begin();
            _spriteBatch.DrawSprite( _spriteRed );
            _spriteBatch.DrawSprite( _spriteBlue );
            _spriteBatch.End();

            base.Draw( gameTime );
        }
NOTE: For manipulating the sprite, scroll below to the Extents section.

Animated Sprites

Animated sprites allow you to take an animation strip texture (animated sprite sheets) and manage it as a single image as far as you are concerned, by handling the source image rectangle, framerate, and transformation for you. The sprite sheet animation must be in the order of Left -> Right, and Top to Bottom.
        AnimatedSprite _sprite = null;

        protected override void LoadContent()
        {
            // ... code ...
            _sprite = AnimatedSprite.Acquire();
            _sprite.ResetFromCenter( Content.Load<Texture2D>("Textures/AnimatedExplosion"), 5, 5, new Vector2( 100, 100 ) );
        }
        protected override void UnloadContent()
        {
            // ... code ...
            _sprite.Release();
        }
        protected override void Update( GameTime gameTime )
        {
            _sprite.Update( gameTime );
            base.Update( gameTime );
        }
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            _spriteBatch.Begin();
            _sprite.Draw( _spriteBatch );
            _spriteBatch.End();

            base.Draw( gameTime );
        }
This example shows you the second method of drawing a sprite, by passing in the SpriteBatch object to the sprite. Both methods will work for both StaticSprite and AnimatedSprite.
NOTE: For manipulating the sprite, scroll below to the Extents section.

Extents

The sprite object contains several properties that are often useful for game development:
            _sprite.ModulationColor; // Color tint
            _sprite.Opacity; // Opacity
            _sprite.Visible; // Visibility
            _sprite.TextureInfo; // Texture and Source Rectangle
            _sprite.RenderingExtent; // Shape and Transformation
The animated sprite object contains additional properties for controlling the animation as well.
In order to manipulate the sprite, you must work with an Extent. The Extent provides several properties and methods, of which you probably just need a few for now.
            _sprite.RenderingExtent.Anchor; // The current position of the sprite
            _sprite.RenderingExtent.Angle; // The current rotation of the sprite
            _sprite.RenderingExtent.Scale; // The current scale-factor of the sprite
            _sprite.RenderingExtent.ReAnchor(); // Re-positions the origin of the sprite relative to the Anchor positioning
            _sprite.RenderingExtent.ContainsPoint(); // Checks if a point is inside the extent
            _sprite.RenderingExtent.FindClosestPoint(); // Finds the closest point on the extent edge to an arbitrary point
            _sprite.RenderingExtent.Intersects(); // Tests for a collision between this _sprite and another sprite

Composite Extents

Need a way to group multiple sprites together? Maybe a car with a chassis sprite and separate wheel sprites? Then Composite Extents can help you move the car as one entity while at the same time, make those wheels spin individually.
        private CompositeExtent _compositeExtent;

        protected override void LoadContent()
        {
            // ... code ...
            _compositeExtent = CompositeExtent.Acquire();
            _compositeExtent.Add( _spriteRed.RenderingExtent );
            _compositeExtent.Add( _spriteBlueRenderingExtent );
        }
        protected override void UnloadContent()
        {
            // ... code ...
            _compositeExtent.Release();
        }
        protected override void Update( GameTime gameTime )
        {
            KeyboardState ks = Keyboard.GetState();
            if( ks.IsKeyDown( Keys.A ) )
            {
                _compositeExtent.Anchor -= new Vector2( 5, 0 );
            }
            if( ks.IsKeyDown( Keys.D ) )
            {
                _compositeExtent.Anchor += new Vector2( 5, 0 );
            }
            if( ks.IsKeyDown( Keys.W ) )
            {
                _compositeExtent.Anchor += new Vector2( 0, -5 );
            }
            if( ks.IsKeyDown( Keys.S ) )
            {
                _compositeExtent.Anchor += new Vector2( 0, 5 );
            }
            if( ks.IsKeyDown( Keys.Q ) )
            {
                _compositeExtent.Angle -= 0.04f;
            }
            if( ks.IsKeyDown( Keys.E ) )
            {
                _compositeExtent.Angle += 0.04f;
            }
            if( ks.IsKeyDown( Keys.Z ) )
            {
                _compositeExtent.Scale *= 1.04f;
            }
            if( ks.IsKeyDown( Keys.C ) )
            {
                _compositeExtent.Scale /= 1.04f;
            }
            base.Update( gameTime );
        }
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            _spriteBatch.Begin();
            _spriteBatch.DrawSprite( _spriteRed );
            _spriteBatch.DrawSprite( _spriteBlue );
            _spriteBatch.DrawPolygonExtent( _compositeExtent );
            _spriteBatch.End();

            base.Draw( gameTime );
        }

Last edited Feb 7, 2011 at 12:03 AM by twohalf, version 6

Comments

No comments yet.