/TTengine/core/EffectSpritelet.cs

http://github.com/trancetrance/TTengine · C# · 144 lines · 83 code · 18 blank · 43 comment · 9 complexity · 12a041c998bee202c3107aaf2820e80e MD5 · raw file

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