From f6a7b03def2404cd4e4d10575314b33e964d615d Mon Sep 17 00:00:00 2001 From: Mike Desaro Date: Sun, 9 Oct 2016 17:27:37 -0700 Subject: [PATCH] massive refactoring of RenderableComponent! moved all SpriteEffects code and origin to Sprite --- Nez-PCL/ECS/Components/Physics/Collider.cs | 6 +- .../Components/Renderables/3D/Renderable3D.cs | 4 +- .../Components/Renderables/LineRenderer.cs | 6 +- Nez-PCL/ECS/Components/Renderables/Mesh.cs | 2 +- .../Renderables/RenderableComponent.cs | 210 ++++-------------- .../Renderables/Sprites/PrototypeSprite.cs | 8 +- .../Renderables/Sprites/ScrollingSprite.cs | 6 +- .../Components/Renderables/Sprites/Sprite.cs | 157 ++++++++++++- .../Renderables/Sprites/SpriteAnimation.cs | 16 +- .../Renderables/Sprites/SpriteMime.cs | 11 +- .../Sprites/SpriteOutlineRenderer.cs | 7 +- .../Components/Renderables/Sprites/SpriteT.cs | 25 ++- .../Renderables/Sprites/SpriteTrail.cs | 45 ++-- .../Renderables/Sprites/TiledSprite.cs | 6 +- .../Renderables/TiledMapComponent.cs | 1 - .../Components/Text/FramesPerSecondCounter.cs | 14 +- Nez-PCL/ECS/Components/Text/Text.cs | 24 +- .../ECS/Components/Text/TextRunComponent.cs | 1 - .../DeferredLighting/Lights/DeferredLight.cs | 8 +- .../DeferredLighting/Lights/PointLight.cs | 1 - Nez-PCL/Graphics/PrimitiveBatch.cs | 12 + 21 files changed, 308 insertions(+), 262 deletions(-) diff --git a/Nez-PCL/ECS/Components/Physics/Collider.cs b/Nez-PCL/ECS/Components/Physics/Collider.cs index a9adb47f7..60458b8a3 100644 --- a/Nez-PCL/ECS/Components/Physics/Collider.cs +++ b/Nez-PCL/ECS/Components/Physics/Collider.cs @@ -173,12 +173,14 @@ public virtual void onEntityAddedToScene() circle.radius = Math.Max( width, height ) * 0.5f; // fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider - var localCenter = renderableBounds.center - entity.transform.position; - localOffset = localCenter; + localOffset = renderableBounds.center - entity.transform.position; } else { shape = new Box( width, height ); + + // fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider + localOffset = renderableBounds.center - entity.transform.position; } } } diff --git a/Nez-PCL/ECS/Components/Renderables/3D/Renderable3D.cs b/Nez-PCL/ECS/Components/Renderables/3D/Renderable3D.cs index 1bfa8c1d0..d28924934 100644 --- a/Nez-PCL/ECS/Components/Renderables/3D/Renderable3D.cs +++ b/Nez-PCL/ECS/Components/Renderables/3D/Renderable3D.cs @@ -67,8 +67,8 @@ public Vector3 rotation /// The rotation degrees. public Vector3 rotationDegrees { - get { return new Vector3( _rotationXY, transform.rotation ) * 57.295779513082320876798154814105f; } - set { rotation = value *= 0.017453292519943295769236907684886f; } + get { return new Vector3( _rotationXY, transform.rotation ) * Mathf.rad2Deg; } + set { rotation = value *= Mathf.deg2Rad; } } /// diff --git a/Nez-PCL/ECS/Components/Renderables/LineRenderer.cs b/Nez-PCL/ECS/Components/Renderables/LineRenderer.cs index 02a102167..2ab33bfaa 100644 --- a/Nez-PCL/ECS/Components/Renderables/LineRenderer.cs +++ b/Nez-PCL/ECS/Components/Renderables/LineRenderer.cs @@ -807,7 +807,7 @@ public override void onEntityTransformChanged( Transform.Component comp ) if( useWorldSpace ) return; - _bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, width, height ); + _bounds.calculateBounds( entity.transform.position, _localOffset, Vector2.Zero, entity.transform.scale, entity.transform.rotation, width, height ); } @@ -841,8 +841,10 @@ public override void debugRender( Graphics graphics ) for( var i = 0; i < _vertices.length; i++ ) { var v = _vertices[i]; - Debug.drawPixel( v.Position.X, v.Position.Y, 4, Color.GhostWhite ); + graphics.batcher.drawPixel( v.Position.X, v.Position.Y, Color.GhostWhite, 4 ); } + + graphics.batcher.drawHollowRect( _bounds, DefaultColors.colliderBounds ); } #endregion diff --git a/Nez-PCL/ECS/Components/Renderables/Mesh.cs b/Nez-PCL/ECS/Components/Renderables/Mesh.cs index 5f28a6424..efa438e1d 100644 --- a/Nez-PCL/ECS/Components/Renderables/Mesh.cs +++ b/Nez-PCL/ECS/Components/Renderables/Mesh.cs @@ -23,7 +23,7 @@ public override RectangleF bounds { if( _areBoundsDirty ) { - _bounds.calculateBounds( entity.transform.position + _topLeftVertPosition, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, _width, _height ); + _bounds.calculateBounds( entity.transform.position + _topLeftVertPosition, Vector2.Zero, Vector2.Zero, entity.transform.scale, entity.transform.rotation, _width, _height ); _areBoundsDirty = false; } diff --git a/Nez-PCL/ECS/Components/Renderables/RenderableComponent.cs b/Nez-PCL/ECS/Components/Renderables/RenderableComponent.cs index fcaf4a7e5..258e2ef0a 100644 --- a/Nez-PCL/ECS/Components/Renderables/RenderableComponent.cs +++ b/Nez-PCL/ECS/Components/Renderables/RenderableComponent.cs @@ -1,6 +1,5 @@ using System; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; namespace Nez @@ -13,12 +12,6 @@ public abstract class RenderableComponent : Component, IComparable - /// used by Renderers to specify how this sprite should be rendered. If non-null, it is automatically disposed of when the Component - /// is removed from the Entity. - /// - public virtual Material material { get; set; } - /// /// width of the RenderableComponent. subclasses that do not override the bounds property must implement this! /// @@ -32,19 +25,21 @@ public abstract class RenderableComponent : Component, IComparable - /// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning. + /// the AABB that wraps this object /// - /// The local position. - public Vector2 localOffset + /// The bounds. + public virtual RectangleF bounds { - get { return _localOffset; } - set { setLocalOffset( value ); } - } + get + { + if( _areBoundsDirty ) + { + _bounds.calculateBounds( entity.transform.position, _localOffset, Vector2.Zero, entity.transform.scale, entity.transform.rotation, width, height ); + _areBoundsDirty = false; + } - public Vector2 origin - { - get { return _origin; } - set { setOrigin( value ); } + return _bounds; + } } /// @@ -57,16 +52,6 @@ public float layerDepth set { setLayerDepth( value ); } } - /// - /// color passed along to the Batcher when rendering - /// - public Color color = Color.White; - - /// - /// Batchers passed along to the Batcher when rendering. flipX/flipY are helpers for setting this. - /// - public SpriteEffects spriteEffects = SpriteEffects.None; - /// /// lower renderLayers are in the front and higher are in the back, just like layerDepth but not clamped to 0-1. Note that this means /// higher renderLayers are sent to the Batcher first. An important fact when using the stencil buffer. @@ -79,63 +64,24 @@ public int renderLayer } /// - /// determines if the sprite should be rendered normally or flipped horizontally - /// - /// true if flip x; otherwise, false. - public bool flipX - { - get - { - return ( spriteEffects & SpriteEffects.FlipHorizontally ) == SpriteEffects.FlipHorizontally; - } - set - { - spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipHorizontally ) : ( spriteEffects & ~SpriteEffects.FlipHorizontally ); - } - } - - /// - /// determines if the sprite should be rendered normally or flipped vertically + /// color passed along to the Batcher when rendering /// - /// true if flip y; otherwise, false. - public bool flipY - { - get - { - return ( spriteEffects & SpriteEffects.FlipVertically ) == SpriteEffects.FlipVertically; - } - set - { - spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipVertically ) : ( spriteEffects & ~SpriteEffects.FlipVertically ); - } - } + public Color color = Color.White; /// - /// the AABB that wraps this object + /// used by Renderers to specify how this sprite should be rendered. If non-null, it is automatically disposed of when the Component + /// is removed from the Entity. /// - /// The bounds. - public virtual RectangleF bounds - { - get - { - if( _areBoundsDirty ) - { - _bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, width, height ); - _areBoundsDirty = false; - } - - return _bounds; - } - } + public virtual Material material { get; set; } /// - /// helper property for setting the origin in normalized fashion (0-1 for x and y) + /// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning. /// - /// The origin normalized. - public Vector2 originNormalized + /// The local position. + public Vector2 localOffset { - get { return new Vector2( _origin.X / width, _origin.Y / height ); } - set { setOrigin( new Vector2( value.X * width, value.Y * height ) ); } + get { return _localOffset; } + set { setLocalOffset( value ); } } /// @@ -160,7 +106,6 @@ private set } protected Vector2 _localOffset; - protected Vector2 _origin; protected float _layerDepth; protected int _renderLayer; protected RectangleF _bounds; @@ -206,7 +151,7 @@ public override void debugRender( Graphics graphics ) /// isVisibleFromCamera for its culling check. /// protected virtual void onBecameVisible() - {} + { } /// @@ -214,11 +159,11 @@ protected virtual void onBecameVisible() /// isVisibleFromCamera for its culling check. /// protected virtual void onBecameInvisible() - {} + { } public override void onRemovedFromEntity() - {} + { } /// @@ -245,50 +190,6 @@ public RenderableComponent setMaterial( Material material ) } - /// - /// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning. - /// - /// The local offset. - /// Offset. - public RenderableComponent setLocalOffset( Vector2 offset ) - { - if( _localOffset != offset ) - { - _localOffset = offset; - _areBoundsDirty = true; - } - return this; - } - - - /// - /// sets the origin for the Renderable - /// - /// The origin. - /// Origin. - public RenderableComponent setOrigin( Vector2 origin ) - { - if( _origin != origin ) - { - _origin = origin; - _areBoundsDirty = true; - } - return this; - } - - - /// - /// helper for setting the origin in normalized fashion (0-1 for x and y) - /// - /// The origin normalized. - /// Origin. - public RenderableComponent setOriginNormalized( Vector2 value ) - { - setOrigin( new Vector2( value.X * width, value.Y * height ) ); - return this; - } - - /// /// standard Batcher layerdepth. 0 is in front and 1 is in back. Changing this value will trigger a sort of the renderableComponents /// @@ -336,6 +237,22 @@ public RenderableComponent setColor( Color color ) return this; } + + /// + /// offset from the parent entity. Useful for adding multiple Renderables to an Entity that need specific positioning. + /// + /// The local offset. + /// Offset. + public RenderableComponent setLocalOffset( Vector2 offset ) + { + if( _localOffset != offset ) + { + _localOffset = offset; + _areBoundsDirty = true; + } + return this; + } + #endregion @@ -351,49 +268,6 @@ public T getMaterial() where T : Material return material as T; } - - /// - /// Draws the Renderable with an outline. Note that this should be called on disabled Renderables since they shouldnt take part in default - /// rendering if they need an ouline. - /// - /// Graphics. - /// Camera. - /// Offset. - public void drawOutline( Graphics graphics, Camera camera, int offset = 1 ) - { - drawOutline( graphics, camera, Color.Black, offset ); - } - - - public void drawOutline( Graphics graphics, Camera camera, Color outlineColor, int offset = 1 ) - { - // save the stuff we are going to modify so we can restore it later - var originalPosition = _localOffset; - var originalColor = color; - var originalLayerDepth = _layerDepth; - - // set our new values - color = outlineColor; - _layerDepth += 0.01f; - - for( var i = -1; i < 2; i++ ) - { - for( var j = -1; j < 2; j++ ) - { - if( i != 0 || j != 0 ) - { - _localOffset = originalPosition + new Vector2( i * offset, j * offset ); - render( graphics, camera ); - } - } - } - - // restore changed state - _localOffset = originalPosition; - color = originalColor; - _layerDepth = originalLayerDepth; - } - #endregion @@ -418,7 +292,7 @@ public int CompareTo( RenderableComponent other ) if( other.material == null ) return -1; - + return 1; } } diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/PrototypeSprite.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/PrototypeSprite.cs index 8f6962d4d..3374b32e5 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/PrototypeSprite.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/PrototypeSprite.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Nez.Sprites; namespace Nez @@ -7,7 +8,7 @@ namespace Nez /// /// skewable rectangle sprite for prototyping /// - public class PrototypeSprite : RenderableComponent + public class PrototypeSprite : Sprite { public override float width { get { return _width; } } public override float height { get { return _height; } } @@ -22,7 +23,7 @@ public class PrototypeSprite : RenderableComponent float _skewTopX, _skewBottomX, _skewLeftY, _skewRightY; - public PrototypeSprite( float width, float height ) + public PrototypeSprite( float width, float height ) : base( Graphics.instance.pixelTexture ) { _width = width; _height = height; @@ -77,8 +78,9 @@ public override void render( Graphics graphics, Camera camera ) var pos = ( entity.transform.position - ( origin * entity.transform.localScale ) + localOffset ); var size = new Point( (int)( _width * entity.transform.localScale.X ), (int)( _height * entity.transform.localScale.Y ) ); var destRect = new Rectangle( (int)pos.X, (int)pos.Y, size.X, size.Y ); - graphics.batcher.draw( graphics.pixelTexture, destRect, graphics.pixelTexture.sourceRect, color, entity.transform.rotation, SpriteEffects.None, layerDepth, _skewTopX, _skewBottomX, _skewLeftY, _skewRightY ); + graphics.batcher.draw( subtexture, destRect, subtexture.sourceRect, color, entity.transform.rotation, SpriteEffects.None, layerDepth, _skewTopX, _skewBottomX, _skewLeftY, _skewRightY ); } + } } diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/ScrollingSprite.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/ScrollingSprite.cs index 19cc6e0f5..f2da833d8 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/ScrollingSprite.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/ScrollingSprite.cs @@ -62,8 +62,10 @@ public int scrollY public ScrollingSprite( Subtexture subtexture ) : base( subtexture ) { _sourceRect = subtexture.sourceRect; - material = new Material(); - material.samplerState = Core.defaultWrappedSamplerState; + material = new Material + { + samplerState = Core.defaultWrappedSamplerState + }; } diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/Sprite.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/Sprite.cs index 3e6b67ea6..90995ea5e 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/Sprite.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/Sprite.cs @@ -1,22 +1,87 @@ using Nez.Textures; -using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework; namespace Nez.Sprites { + /// + /// the most basic and common Renderable. Renders a Subtexture/Texture. + /// public class Sprite : RenderableComponent { - public override float width + public override RectangleF bounds + { + get + { + if( _areBoundsDirty ) + { + _bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, subtexture.sourceRect.Width, subtexture.sourceRect.Height ); + _areBoundsDirty = false; + } + + return _bounds; + } + } + + + /// + /// the origin of the Sprite. This is set automatically when setting a Subtexture. + /// + /// The origin. + public Vector2 origin { - get { return subtexture.sourceRect.Width; } + get { return _origin; } + set { setOrigin( value ); } } - public override float height + /// + /// helper property for setting the origin in normalized fashion (0-1 for x and y) + /// + /// The origin normalized. + public Vector2 originNormalized { - get { return subtexture.sourceRect.Height; } + get { return new Vector2( _origin.X / width, _origin.Y / height ); } + set { setOrigin( new Vector2( value.X * width, value.Y * height ) ); } } + /// + /// determines if the sprite should be rendered normally or flipped horizontally + /// + /// true if flip x; otherwise, false. + public bool flipX + { + get + { + return ( spriteEffects & SpriteEffects.FlipHorizontally ) == SpriteEffects.FlipHorizontally; + } + set + { + spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipHorizontally ) : ( spriteEffects & ~SpriteEffects.FlipHorizontally ); + } + } + + /// + /// determines if the sprite should be rendered normally or flipped vertically + /// + /// true if flip y; otherwise, false. + public bool flipY + { + get + { + return ( spriteEffects & SpriteEffects.FlipVertically ) == SpriteEffects.FlipVertically; + } + set + { + spriteEffects = value ? ( spriteEffects | SpriteEffects.FlipVertically ) : ( spriteEffects & ~SpriteEffects.FlipVertically ); + } + } + + /// + /// Batchers passed along to the Batcher when rendering. flipX/flipY are helpers for setting this. + /// + public SpriteEffects spriteEffects = SpriteEffects.None; + /// /// the Subtexture that should be displayed by this Sprite. When set, the origin of the Sprite is also set to match Subtexture.origin. /// @@ -27,9 +92,14 @@ public Subtexture subtexture set { setSubtexture( value ); } } + protected Vector2 _origin; protected Subtexture _subtexture; + public Sprite() + {} + + public Sprite( Subtexture subtexture ) { _subtexture = subtexture; @@ -38,9 +108,11 @@ public Sprite( Subtexture subtexture ) public Sprite( Texture2D texture ) : this( new Subtexture( texture ) ) - {} + { } + #region fluent setters + /// /// sets the Subtexture and updates the origin of the Sprite to match Subtexture.origin. If for whatever reason you need /// an origin different from the Subtexture either clone it or set the origin AFTER setting the Subtexture here. @@ -57,6 +129,79 @@ public Sprite setSubtexture( Subtexture subtexture ) } + /// + /// sets the origin for the Renderable + /// + /// The origin. + /// Origin. + public Sprite setOrigin( Vector2 origin ) + { + if( _origin != origin ) + { + _origin = origin; + _areBoundsDirty = true; + } + return this; + } + + + /// + /// helper for setting the origin in normalized fashion (0-1 for x and y) + /// + /// The origin normalized. + /// Origin. + public Sprite setOriginNormalized( Vector2 value ) + { + setOrigin( new Vector2( value.X * width, value.Y * height ) ); + return this; + } + + #endregion + + + /// + /// Draws the Renderable with an outline. Note that this should be called on disabled Renderables since they shouldnt take part in default + /// rendering if they need an ouline. + /// + /// Graphics. + /// Camera. + /// Offset. + public void drawOutline( Graphics graphics, Camera camera, int offset = 1 ) + { + drawOutline( graphics, camera, Color.Black, offset ); + } + + + public void drawOutline( Graphics graphics, Camera camera, Color outlineColor, int offset = 1 ) + { + // save the stuff we are going to modify so we can restore it later + var originalPosition = _localOffset; + var originalColor = color; + var originalLayerDepth = _layerDepth; + + // set our new values + color = outlineColor; + _layerDepth += 0.01f; + + for( var i = -1; i < 2; i++ ) + { + for( var j = -1; j < 2; j++ ) + { + if( i != 0 || j != 0 ) + { + _localOffset = originalPosition + new Vector2( i * offset, j * offset ); + render( graphics, camera ); + } + } + } + + // restore changed state + _localOffset = originalPosition; + color = originalColor; + _layerDepth = originalLayerDepth; + } + + public override void render( Graphics graphics, Camera camera ) { graphics.batcher.draw( _subtexture, entity.transform.position + localOffset, _subtexture.sourceRect, color, entity.transform.rotation, origin, entity.transform.scale, spriteEffects, _layerDepth ); diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteAnimation.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteAnimation.cs index eb05774e2..3fb5b42ca 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteAnimation.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteAnimation.cs @@ -13,6 +13,9 @@ public enum AnimationCompletionBehavior } + /// + /// houses the information that a SpriteT requires for animation + /// public class SpriteAnimation { /// @@ -146,14 +149,11 @@ public SpriteAnimation addFrame( Subtexture subtexture ) } - [System.Obsolete( "Deprecated. origin is now a property of Subtexture so use the other variant of addFrame." )] - public SpriteAnimation addFrame( Subtexture subtexture, Vector2 origin ) - { - addFrame( subtexture ); - return this; - } - - + /// + /// adds multiple frames to this animation + /// + /// The frames. + /// Subtextures. public SpriteAnimation addFrames( List subtextures ) { for( var i = 0; i < subtextures.Count; i++ ) diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteMime.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteMime.cs index 1cf032a7c..3880f9bcc 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteMime.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteMime.cs @@ -1,13 +1,8 @@ -using System; -using Nez.Sprites; -using Microsoft.Xna.Framework; - - -namespace Nez.Sprites +namespace Nez.Sprites { /// /// this component will draw the same frame of of spriteToMime every frame. The only difference in rendering is that SpriteMime uses its own - /// localPosition and color. This allows you to use it for the purpose of shadows (by offsetting via localPosition) or silhouettes (with a + /// localOffset and color. This allows you to use it for the purpose of shadows (by offsetting via localPosition) or silhouettes (with a /// Material that has a stencil read). /// public class SpriteMime : RenderableComponent @@ -16,6 +11,8 @@ public class SpriteMime : RenderableComponent public override float height { get { return _spriteToMime.height; } } + public override RectangleF bounds { get { return _spriteToMime.bounds; } } + Sprite _spriteToMime; diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteOutlineRenderer.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteOutlineRenderer.cs index e43cf542f..6e24535fe 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteOutlineRenderer.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteOutlineRenderer.cs @@ -1,6 +1,4 @@ -using System; -using Nez; -using Nez.Sprites; +using Nez.Sprites; using Microsoft.Xna.Framework; @@ -22,6 +20,8 @@ public class SpriteOutlineRenderer : RenderableComponent /// public Color outlineColor = Color.Black; + public override RectangleF bounds { get { return _sprite.bounds; } } + public override float width { get { return _sprite.width + outlineWidth * 2; } @@ -43,7 +43,6 @@ public SpriteOutlineRenderer( Sprite sprite ) { _sprite = sprite; _sprite.enabled = false; - originNormalized = new Vector2( 0.5f, 0.5f ); } diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteT.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteT.cs index c758a5901..e7842ce44 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteT.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteT.cs @@ -1,6 +1,5 @@ using System; using Nez.Textures; -using Microsoft.Xna.Framework; using System.Collections.Generic; @@ -8,7 +7,8 @@ namespace Nez.Sprites { /// /// Sprite class handles the display and animation of a sprite. It uses a suggested Enum as a key (you can use an int as well if you - /// prefer). If you do use an Enum it is recommended to pass in a IEqualityComparer when using an enum like CoreEvents does. + /// prefer). If you do use an Enum it is recommended to pass in a IEqualityComparer when using an enum like CoreEvents does. See also + /// the EnumEqualityComparerGenerator.tt T4 template for automatically generating the IEqualityComparer. /// public class Sprite : Sprite, IUpdatable where TEnum : struct, IComparable, IFormattable { @@ -26,7 +26,7 @@ public TEnum currentAnimation set { play( value ); } } - Dictionary _animations; + Dictionary _animations; // playback state SpriteAnimation _currentAnimation; @@ -44,15 +44,15 @@ public TEnum currentAnimation /// when the Scene is running. /// /// Custom comparer. - public Sprite( IEqualityComparer customComparer = null ) : base( new Subtexture( null, 0, 0, 0, 0 ) ) + public Sprite( IEqualityComparer customComparer = null ) : base( Graphics.instance.pixelTexture ) { - _animations = new Dictionary( customComparer ); + _animations = new Dictionary( customComparer ); } public Sprite( IEqualityComparer customComparer, Subtexture subtexture ) : base( subtexture ) { - _animations = new Dictionary( customComparer ); + _animations = new Dictionary( customComparer ); } @@ -61,13 +61,14 @@ public Sprite( IEqualityComparer customComparer, Subtexture subtexture ) /// /// Subtexture. public Sprite( Subtexture subtexture ) : this( null, subtexture ) - {} + { } /// /// Sprite needs a Subtexture at constructor time so the first frame of the passed in animation will be used for this constructor /// - /// Subtexture. + /// Animation key. + /// Animation. public Sprite( TEnum animationKey, SpriteAnimation animation ) : this( null, animation.frames[0] ) { addAnimation( animationKey, animation ); @@ -203,11 +204,11 @@ public Sprite addAnimation( TEnum key, SpriteAnimation animation ) } - public SpriteAnimation getAnimation( TEnum key ) - { + public SpriteAnimation getAnimation( TEnum key ) + { Assert.isTrue( _animations.ContainsKey( key ), "{0} is not present in animations", key ); - return _animations[key]; - } + return _animations[key]; + } #region Playback diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteTrail.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteTrail.cs index eab94cf0a..89e10a378 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteTrail.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/SpriteTrail.cs @@ -2,17 +2,21 @@ using Microsoft.Xna.Framework; using Nez.Textures; using System.Collections.Generic; -using Nez.Sprites; using Microsoft.Xna.Framework.Graphics; +using System.Runtime.CompilerServices; namespace Nez.Sprites { /// - /// renders and fades a copy of the Sprite on the same Entity. minDistanceBetweenInstances determines how often a trail sprite is added. + /// renders and fades a series of copies of the Sprite on the same Entity. minDistanceBetweenInstances determines how often a trail + /// sprite is added. /// public class SpriteTrail : RenderableComponent, IUpdatable { + /// + /// helper class that houses the data required for the individual trail instances + /// class SpriteTrailInstance { Vector2 _position; @@ -59,6 +63,7 @@ public void setSpriteRenderOptions( float rotation, Vector2 origin, Vector2 scal /// /// returns true when the fade out is complete /// + [MethodImpl( MethodImplOptions.AggressiveInlining )] public bool update() { _elapsedTime += Time.deltaTime; @@ -77,6 +82,7 @@ public bool update() } + [MethodImpl( MethodImplOptions.AggressiveInlining )] public void render( Graphics graphics, Camera camera ) { graphics.batcher.draw( _subtexture, _position, _subtexture.sourceRect, _renderColor, _rotation, _origin, _scale, _spriteEffects, _layerDepth ); @@ -84,15 +90,11 @@ public void render( Graphics graphics, Camera camera ) } - public override float width - { - get { return _sprite.width; } - } + public override float width { get { return _sprite.width; } } - public override float height - { - get { return _sprite.height; } - } + public override float height { get { return _sprite.height; } } + + public override RectangleF bounds { get { return _sprite.bounds; } } /// /// how far does the Sprite have to move before a new instance is spawned @@ -139,7 +141,6 @@ public SpriteTrail( Sprite sprite, int maxInstances = 15 ) { // we want to store the sprite and move ourself before the Sprite in render order _sprite = sprite; - origin = _sprite.origin; layerDepth = _sprite.layerDepth + 0.001f; _liveSpriteTrailInstances = new List( 5 ); @@ -179,13 +180,10 @@ void IUpdatable.update() } - public override void render( Graphics graphics, Camera camera ) - { - for( var i = 0; i < _liveSpriteTrailInstances.Count; i++ ) - _liveSpriteTrailInstances[i].render( graphics, camera ); - } - - + /// + /// enables the SpriteTrail + /// + /// The sprite trail. public SpriteTrail enableSpriteTrail() { _awaitingDisable = false; @@ -195,6 +193,10 @@ public SpriteTrail enableSpriteTrail() } + /// + /// disables the SpriteTrail optionally waiting for the current trail to fade out first + /// + /// If set to true complete current trail. public void disableSpriteTrail( bool completeCurrentTrail = true ) { if( completeCurrentTrail ) @@ -228,6 +230,13 @@ void spawnInstance() _liveSpriteTrailInstances.Add( instance ); } + + public override void render( Graphics graphics, Camera camera ) + { + for( var i = 0; i < _liveSpriteTrailInstances.Count; i++ ) + _liveSpriteTrailInstances[i].render( graphics, camera ); + } + } } diff --git a/Nez-PCL/ECS/Components/Renderables/Sprites/TiledSprite.cs b/Nez-PCL/ECS/Components/Renderables/Sprites/TiledSprite.cs index 4b5d10db1..17bae0e43 100644 --- a/Nez-PCL/ECS/Components/Renderables/Sprites/TiledSprite.cs +++ b/Nez-PCL/ECS/Components/Renderables/Sprites/TiledSprite.cs @@ -53,8 +53,10 @@ public int scrollY public TiledSprite( Subtexture subtexture ) : base( subtexture ) { _sourceRect = subtexture.sourceRect; - material = new Material(); - material.samplerState = Core.defaultWrappedSamplerState; + material = new Material + { + samplerState = Core.defaultWrappedSamplerState + }; } diff --git a/Nez-PCL/ECS/Components/Renderables/TiledMapComponent.cs b/Nez-PCL/ECS/Components/Renderables/TiledMapComponent.cs index 2c7bd705b..4b433c66b 100644 --- a/Nez-PCL/ECS/Components/Renderables/TiledMapComponent.cs +++ b/Nez-PCL/ECS/Components/Renderables/TiledMapComponent.cs @@ -1,6 +1,5 @@ using System; using Nez.Tiled; -using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using System.Collections.Generic; diff --git a/Nez-PCL/ECS/Components/Text/FramesPerSecondCounter.cs b/Nez-PCL/ECS/Components/Text/FramesPerSecondCounter.cs index d1d982cbb..ff10d1627 100644 --- a/Nez-PCL/ECS/Components/Text/FramesPerSecondCounter.cs +++ b/Nez-PCL/ECS/Components/Text/FramesPerSecondCounter.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using Nez.BitmapFonts; @@ -91,22 +89,22 @@ void updateTextPosition() _horizontalAlign = HorizontalAlign.Left; _verticalAlign = VerticalAlign.Top; localOffset = dockOffset; - break; + break; case FPSDockPosition.TopRight: _horizontalAlign = HorizontalAlign.Right; _verticalAlign = VerticalAlign.Top; localOffset = new Vector2( Core.graphicsDevice.Viewport.Width - dockOffset.X, dockOffset.Y ); - break; + break; case FPSDockPosition.BottomLeft: _horizontalAlign = HorizontalAlign.Left; _verticalAlign = VerticalAlign.Bottom; localOffset = new Vector2( dockOffset.X, Core.graphicsDevice.Viewport.Height - dockOffset.Y ); - break; + break; case FPSDockPosition.BottomRight: _horizontalAlign = HorizontalAlign.Right; _verticalAlign = VerticalAlign.Bottom; localOffset = new Vector2( Core.graphicsDevice.Viewport.Width - dockOffset.X, Core.graphicsDevice.Viewport.Height - dockOffset.Y ); - break; + break; } } @@ -179,6 +177,6 @@ public FramesPerSecondCounter setDockPosition( FPSDockPosition dockPosition ) } #endregion - + } } diff --git a/Nez-PCL/ECS/Components/Text/Text.cs b/Nez-PCL/ECS/Components/Text/Text.cs index 142021292..1fb75eeeb 100644 --- a/Nez-PCL/ECS/Components/Text/Text.cs +++ b/Nez-PCL/ECS/Components/Text/Text.cs @@ -1,16 +1,24 @@ -using System; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework; -using Nez.BitmapFonts; +using Microsoft.Xna.Framework; +using Nez.Sprites; namespace Nez { - public class Text : RenderableComponent + public class Text : Sprite { - public override float width { get { return _size.X; } } - - public override float height { get { return _size.Y; } } + public override RectangleF bounds + { + get + { + if( _areBoundsDirty ) + { + _bounds.calculateBounds( entity.transform.position, _localOffset, _origin, entity.transform.scale, entity.transform.rotation, _size.X, _size.Y ); + _areBoundsDirty = false; + } + + return _bounds; + } + } /// /// text to draw diff --git a/Nez-PCL/ECS/Components/Text/TextRunComponent.cs b/Nez-PCL/ECS/Components/Text/TextRunComponent.cs index cd52410ea..ab8d71bf0 100644 --- a/Nez-PCL/ECS/Components/Text/TextRunComponent.cs +++ b/Nez-PCL/ECS/Components/Text/TextRunComponent.cs @@ -25,7 +25,6 @@ public void compile() textRun.position = transform.position; textRun.rotation = transform.rotation; textRun.compile(); - origin = textRun.origin; } diff --git a/Nez-PCL/Graphics/DeferredLighting/Lights/DeferredLight.cs b/Nez-PCL/Graphics/DeferredLighting/Lights/DeferredLight.cs index b262c0874..5e66285e5 100644 --- a/Nez-PCL/Graphics/DeferredLighting/Lights/DeferredLight.cs +++ b/Nez-PCL/Graphics/DeferredLighting/Lights/DeferredLight.cs @@ -1,8 +1,4 @@ -using System; -using Microsoft.Xna.Framework; - - -namespace Nez.DeferredLighting +namespace Nez.DeferredLighting { public abstract class DeferredLight : RenderableComponent { @@ -13,7 +9,7 @@ public abstract class DeferredLight : RenderableComponent /// Graphics. /// Camera. public override void render( Graphics graphics, Camera camera ) - {} + { } } } diff --git a/Nez-PCL/Graphics/DeferredLighting/Lights/PointLight.cs b/Nez-PCL/Graphics/DeferredLighting/Lights/PointLight.cs index 00e4e3b20..b06d553a2 100644 --- a/Nez-PCL/Graphics/DeferredLighting/Lights/PointLight.cs +++ b/Nez-PCL/Graphics/DeferredLighting/Lights/PointLight.cs @@ -60,7 +60,6 @@ public PointLight setZPosition( float z ) public PointLight setRadius( float radius ) { _radius = radius; - originNormalized = Vector2Ext.halfVector(); _areBoundsDirty = true; return this; } diff --git a/Nez-PCL/Graphics/PrimitiveBatch.cs b/Nez-PCL/Graphics/PrimitiveBatch.cs index 093d45691..edf319a21 100644 --- a/Nez-PCL/Graphics/PrimitiveBatch.cs +++ b/Nez-PCL/Graphics/PrimitiveBatch.cs @@ -83,6 +83,18 @@ public void begin( ref Matrix projection, ref Matrix view ) } + /// + /// Begin is called to tell the PrimitiveBatch what kind of primitives will be drawn, and to prepare the graphics card to render those primitives. + /// Use camera.projectionMatrix and camera.transformMatrix if the batch should be in camera space. + /// + /// The projection. + /// The view. + public void begin( Matrix projection, Matrix view ) + { + begin( ref projection, ref view ); + } + + /// /// End is called once all the primitives have been drawn using AddVertex. /// it will call Flush to actually submit the draw call to the graphics card, and