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