PageRenderTime 25ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/TTengine/core/Efflet.cs

http://github.com/trancetrance/TTengine
C# | 65 lines | 45 code | 9 blank | 11 comment | 6 complexity | 78727005619884f9ec98ffe13d0625e1 MD5 | raw file
 1// (c) 2010-2012 TranceTrance.com. Distributed under the FreeBSD license in LICENSE.txt
 2
 3using Microsoft.Xna.Framework.Graphics;
 4using Microsoft.Xna.Framework;
 5
 6namespace TTengine.Core
 7{
 8    /**
 9     * base class for (shader) effects that are applied to an entire screen, i.e. Screenlet.
10     * Screenlet takes care of rendering the Efflets in order by calling OnDrawEfflet()
11     * at the end of a Draw() cycle.
12     * 
13     * Subclasses of Efflet can use inside the shader these predefined variables:
14     *   DrawColor - value of Gamelet.DrawColor as RGB plus the alpha value
15     */
16    public class Efflet: Gamelet
17    {
18        protected string fxFileName = "";
19        protected Effect effect = null;
20        protected SpriteBatch spriteBatch = null;
21        protected EffectParameter drawColorParameter=null;
22
23        public Efflet(string fxFileName)
24        {
25            this.fxFileName = fxFileName;
26            DrawInfo = new DrawInfo();
27            Add(DrawInfo);
28            LoadEffect();
29        }
30
31        protected virtual void LoadEffect()
32        {
33            spriteBatch = new SpriteBatch(Screen.graphicsDevice);
34            if (fxFileName != null)
35            {
36                effect = TTengineMaster.ActiveGame.Content.Load<Effect>(fxFileName);
37                drawColorParameter = effect.Parameters["DrawColor"];
38            }
39            VertexShaderInit(effect);
40        }
41
42        protected override void OnUpdate(ref UpdateParams p)
43        {
44            if (drawColorParameter != null) drawColorParameter.SetValue(DrawInfo.DrawColor.ToVector4() );
45        }
46
47        internal override void Draw(ref DrawParams p)
48        {
49            if (!Active) return;
50            base.Draw(ref p);
51
52            // add myself to list of efflets for post-process, in case I'm visible.
53            if (Visible) Screen.effletsList.Add(this);
54        }
55
56        /// to override, called when eff should apply itself to a sourceBuffer, drawing to screen.RenderTarget as usual.
57        public virtual void OnDrawEfflet(ref DrawParams p, RenderTarget2D sourceBuffer)
58        {
59            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null, effect);
60            spriteBatch.Draw(sourceBuffer, Screen.ScreenRectangle, Color.White);
61            spriteBatch.End();
62        }
63
64    }
65}