EvolvedSun.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EvolvedSun.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. class EvolvedSun : Shape
  19. {
  20. private const int xCount = 1;
  21. private const int yCount = 1;
  22. private Effect effect;
  23. private EffectParameter worldParam;
  24. private EffectParameter worldViewProjectionParam;
  25. private EffectParameter sun0TextureParam;
  26. private EffectParameter sun1TextureParam;
  27. private EffectParameter blendFactor;
  28. private Texture2D[] sun;
  29. private int currentFrame;
  30. private double currentTime;
  31. public EvolvedSun(Game game)
  32. : base(game)
  33. {
  34. }
  35. public override void Create()
  36. {
  37. buffer = Plane(xCount, yCount);
  38. //Load the correct shader and set up the parameters
  39. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  40. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\sun");
  41. worldParam = effect.Parameters["world"];
  42. worldViewProjectionParam = effect.Parameters["worldViewProjection"];
  43. sun0TextureParam = effect.Parameters["Sun_Tex0"];
  44. sun1TextureParam = effect.Parameters["Sun_Tex1"];
  45. blendFactor = effect.Parameters["blendFactor"];
  46. //Preload the textures into the cache
  47. int numFrames = 5;
  48. sun = new Texture2D[numFrames];
  49. sun[0] = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\suntest1");
  50. sun[1] = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\suntest2");
  51. sun[2] = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\suntest3");
  52. sun[3] = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\suntest4");
  53. sun[4] = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + @"textures\suntest5");
  54. }
  55. public override void Update(TimeSpan timeSpan, TimeSpan elapsedTime)
  56. {
  57. base.Update(timeSpan, elapsedTime);
  58. // change frames every second.
  59. currentTime += elapsedTime.TotalMilliseconds;
  60. if (currentTime > 1000.0f)
  61. {
  62. currentTime = 0.0f;
  63. currentFrame++;
  64. }
  65. if (currentFrame > 4)
  66. currentFrame = 0;
  67. }
  68. public override void Render()
  69. {
  70. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  71. GraphicsDevice device = graphicsService.GraphicsDevice;
  72. base.Render();
  73. device.SetVertexBuffer(buffer);
  74. worldParam.SetValue(World);
  75. worldViewProjectionParam.SetValue(World * SpacewarGame.Camera.View * SpacewarGame.Camera.Projection);
  76. float currentFactor = (float)(currentTime / 1000.0f);
  77. blendFactor.SetValue(currentFactor);
  78. sun1TextureParam.SetValue(sun[currentFrame]);
  79. if (currentFrame < 4)
  80. sun0TextureParam.SetValue(sun[currentFrame + 1]);
  81. else
  82. sun0TextureParam.SetValue(sun[0]);
  83. effect.Techniques[0].Passes[0].Apply();
  84. device.DrawPrimitives(PrimitiveType.TriangleList, 0, xCount * yCount * 2);
  85. }
  86. }
  87. }