EvolvedBackdrop.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EvolvedBackdrop.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework;
  15. #endregion
  16. namespace Spacewar
  17. {
  18. /// <summary>
  19. /// Handles the drawing of the moving nebula backdrops
  20. /// </summary>
  21. public class EvolvedBackdrop : Shape, IDisposable
  22. {
  23. private const int xCount = 1;
  24. private const int yCount = 1;
  25. private Effect effect;
  26. private float layerFactor;
  27. private float timeFactor1;
  28. private float timeFactor2;
  29. private Vector4 layer1Offset;
  30. private Vector4 layer2Offset;
  31. private EffectParameter layer1TextureParam;
  32. private EffectParameter layer2TextureParam;
  33. private EffectParameter layer3TextureParam;
  34. private EffectParameter layerFactorParam;
  35. private EffectParameter layer1OffsetParam;
  36. private EffectParameter layer2OffsetParam;
  37. private Texture2D layer1;
  38. private Texture2D layer2;
  39. private Texture2D layer3;
  40. public EvolvedBackdrop(Game game)
  41. : base(game)
  42. {
  43. layer1Offset = Vector4.Zero;
  44. layer2Offset = Vector4.Zero;
  45. }
  46. /// <summary>
  47. /// Creates the quad needed to render the textures to
  48. /// </summary>
  49. public override void Create()
  50. {
  51. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  52. buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), 6, BufferUsage.WriteOnly);
  53. VertexPositionColor[] data = new VertexPositionColor[6];
  54. data[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
  55. data[1] = new VertexPositionColor(new Vector3(1280, 0, 0), Color.White);
  56. data[2] = new VertexPositionColor(new Vector3(1280, 720, 0), Color.White);
  57. data[3] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White); //same as 0
  58. data[4] = new VertexPositionColor(new Vector3(1280, 720, 0), Color.White); //same as 2
  59. data[5] = new VertexPositionColor(new Vector3(0, 720, 0), Color.White);
  60. buffer.SetData<VertexPositionColor>(data);
  61. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\backdrop");
  62. layer1TextureParam = effect.Parameters["layer1"];
  63. layer2TextureParam = effect.Parameters["layer2"];
  64. layer3TextureParam = effect.Parameters["layer3"];
  65. layerFactorParam = effect.Parameters["layerFactor"];
  66. layer1OffsetParam = effect.Parameters["layer1Offset"];
  67. layer2OffsetParam = effect.Parameters["layer2Offset"];
  68. //Preload the textures into the cache
  69. // TODO: This doesn't support multiple "boards"
  70. layer1 = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\B1_nebula01");
  71. layer2 = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\B1_nebula02");
  72. layer3 = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\B1_stars");
  73. }
  74. /// <summary>
  75. /// Moves the layers and sets the fade values
  76. /// </summary>
  77. /// <param name="time">Current Game time</param>
  78. /// <param name="elapsedTime">Elapsed Game time since last update</param>
  79. public override void Update(TimeSpan time, TimeSpan elapsedTime)
  80. {
  81. base.Update(time, elapsedTime);
  82. timeFactor1 += (float)elapsedTime.TotalSeconds;
  83. timeFactor2 += (float)elapsedTime.TotalSeconds * 2;
  84. //Animate the fading and moving backdrops
  85. //Cross fade between 0.0 and 1.0
  86. layerFactor = (float)(Math.Sin(timeFactor2 * SpacewarGame.Settings.CrossFadeSpeed) * .5 + .5);
  87. //Move the nebula layers up to 200 pixels
  88. layer1Offset.X = (float)((100.0 / 1480.0) * .3 * (Math.Sin(timeFactor1 * SpacewarGame.Settings.OffsetSpeed / 2.0) + 1.0));
  89. layer1Offset.Y = (float)((100.0 / 920.0) * (Math.Cos(timeFactor1 * SpacewarGame.Settings.OffsetSpeed / 1.4) + 1.0));
  90. layer2Offset.X = (float)((100.0 / 1480.0) * (Math.Sin(timeFactor1 * SpacewarGame.Settings.OffsetSpeed) + 1.0));
  91. layer2Offset.Y = (float)((100.0 / 920.0) * .7 * (Math.Cos(timeFactor1 * SpacewarGame.Settings.OffsetSpeed / 1.3) + 1.0));
  92. }
  93. /// <summary>
  94. /// Renders the backdrop
  95. /// </summary>
  96. public override void Render()
  97. {
  98. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  99. GraphicsDevice device = graphicsService.GraphicsDevice;
  100. base.Render();
  101. device.SetVertexBuffer(buffer);
  102. layer1TextureParam.SetValue(layer1);
  103. layer2TextureParam.SetValue(layer2);
  104. layer3TextureParam.SetValue(layer3);
  105. layerFactorParam.SetValue(layerFactor);
  106. layer1OffsetParam.SetValue(layer1Offset);
  107. layer2OffsetParam.SetValue(layer2Offset);
  108. effect.Techniques[0].Passes[0].Apply();
  109. device.DrawPrimitives(PrimitiveType.TriangleList, 0, xCount * yCount * 2);
  110. }
  111. protected override void Dispose(bool all)
  112. {
  113. if (effect != null)
  114. {
  115. effect.Dispose();
  116. effect = null;
  117. }
  118. base.Dispose(all);
  119. }
  120. public override void Dispose()
  121. {
  122. Dispose(true);
  123. GC.SuppressFinalize(this);
  124. }
  125. }
  126. }