PageRenderTime 42ms CodeModel.GetById 32ms app.highlight 4ms RepoModel.GetById 4ms app.codeStats 0ms

/TTengine/core/EffectSpritelet.cs

http://github.com/trancetrance/TTengine
C# | 144 lines | 83 code | 18 blank | 43 comment | 9 complexity | 12a041c998bee202c3107aaf2820e80e MD5 | raw file
  1using System;
  2using Microsoft.Xna.Framework.Graphics;
  3
  4namespace TTengine.Core
  5{
  6    /**
  7     * A Spritelet that supports drawing with a given shader effect applied (HLSL)
  8     */
  9    public class EffectSpritelet : Spritelet
 10    {
 11        /// <summary>
 12        /// whether the Effect is used when drawing (true), or not (false).
 13        /// </summary>
 14        public bool EffectEnabled = true;
 15
 16        /// <summary>
 17        /// the Effect applied (if any)
 18        /// </summary>
 19        protected Effect eff = null;
 20
 21        /// <summary>
 22        /// filename from which to load .fx Effect (if any)
 23        /// </summary>
 24        protected String effectFile = null;
 25
 26        /// <summary>
 27        /// Link to certain default parameters in the shader effect
 28        /// </summary>
 29        protected EffectParameter timeParam, positionParam;
 30
 31        /// <summary>
 32        /// construct with BasicEffect and given texture loaded from content file
 33        /// </summary>
 34        /// <param name="textureFile">texture content file</param>
 35        public EffectSpritelet(String textureFile)
 36            : base(textureFile)
 37        {
 38            this.effectFile = null;
 39            InitEffect();
 40        }
 41
 42        /// <summary>
 43        /// construct with BasicEffect and given Texture2D
 44        /// </summary>
 45        /// <param name="texture">texture content file</param>
 46        public EffectSpritelet(Texture2D texture)
 47            : base(texture)
 48        {
 49            this.effectFile = null;
 50            InitEffect();
 51        }
 52
 53        /// <summary>
 54        /// construct with given texture loaded from content file and given shader Effect
 55        /// </summary>
 56        /// <param name="textureFile">texture content file</param>
 57        /// <param name="effectFile">shader effect file</param>
 58        public EffectSpritelet(String textureFile, String effectFile)
 59            : base(textureFile)
 60        {
 61            this.effectFile = effectFile;
 62            InitEffect();
 63        }
 64
 65        /// <summary>
 66        /// construct with given Texture2D and shader effect
 67        /// </summary>
 68        /// <param name="texture">texture to use</param>
 69        /// <param name="effectFile">shader effect file to load</param>
 70        public EffectSpritelet(Texture2D texture, String effectFile)
 71            : base(texture)
 72        {
 73            this.effectFile = effectFile;
 74            InitEffect();
 75        }
 76
 77        /// <summary>
 78        /// Get reference to the Effect applied
 79        /// </summary>
 80        public Effect Eff
 81        {
 82            get { return eff; }
 83        }
 84
 85        /// <summary>
 86        /// all Effect initialization goes here
 87        /// </summary>
 88        protected virtual void InitEffect()
 89        {
 90            if (effectFile != null)
 91                eff = TTengineMaster.ActiveGame.Content.Load<Effect>(effectFile);
 92            else
 93                eff = new BasicEffect(Screen.graphicsDevice);
 94            VertexShaderInit(eff);
 95
 96            // find or create my effect-related spritebatch
 97            MySpriteBatch = Screen.CreateSharedSpriteBatch(eff);
 98
 99            // try to find common parameters in the Effect (gets null if not found)
100            timeParam = eff.Parameters["Time"];
101            positionParam = eff.Parameters["Position"];
102        }
103
104        protected override void OnDraw(ref DrawParams p)
105        {
106            if (Texture != null)
107            {
108                // supply the shader parameters that may have been configured
109                // TODO may not be useful with shared spritebatches: whole batch of drawn objects uses latest set shader params only.
110                if (timeParam != null)
111                    timeParam.SetValue(SimTime);
112                if (positionParam != null)
113                    positionParam.SetValue(Motion.Position);
114
115                MySpriteBatch.Draw(Texture, DrawInfo.DrawPosition, null, DrawInfo.DrawColor,
116                       Motion.RotateAbs, DrawInfo.DrawCenter, DrawInfo.DrawScale, SpriteEffects.None, DrawInfo.LayerDepth);
117            }
118        }
119
120        public override SpriteBatch MySpriteBatch
121        {
122            get
123            {
124                if (EffectEnabled)
125                {
126                    Screen.UseSharedSpriteBatch(eff);
127                    return mySpriteBatch;
128                }
129                else
130                {
131                    return Parent.MySpriteBatch;
132                }
133            }
134
135            set
136            {
137                Screen.CreateSharedSpriteBatch(eff);
138                mySpriteBatch = value;
139            }
140        }
141
142
143    }
144}