VectorShape.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // VectorShape.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using System.Collections.Generic;
  13. #endregion
  14. namespace Spacewar
  15. {
  16. /// <summary>
  17. /// A base class for drawing simple vector shapes like the retro mode graphics
  18. /// </summary>
  19. public abstract class VectorShape : Shape
  20. {
  21. private Effect effect;
  22. private EffectParameter worldViewProjectionParam;
  23. public VectorShape(Game game)
  24. : base(game)
  25. {
  26. }
  27. /// <summary>
  28. /// Creates the vertex buffers and calls Fill buffer to get the inhertied classes to complete the task
  29. /// </summary>
  30. public override void Create()
  31. {
  32. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  33. buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), 2 * NumberOfVectors, BufferUsage.WriteOnly);
  34. VertexPositionColor[] data = new VertexPositionColor[2 * NumberOfVectors];
  35. FillBuffer(data);
  36. buffer.SetData<VertexPositionColor>(data);
  37. //Load the correct shader and set up the parameters
  38. if (effect == null || effect.IsDisposed)
  39. {
  40. OnCreateDevice();
  41. }
  42. }
  43. /// <summary>
  44. /// Override this method to fill in the vertex buffer with your shape data
  45. /// </summary>
  46. /// <param name="data">A blob of vertex PositionColored data</param>
  47. abstract protected void FillBuffer(VertexPositionColor[] data);
  48. /// <summary>
  49. /// Override this to indicate how many vectors in your vector shape
  50. /// </summary>
  51. abstract protected int NumberOfVectors
  52. {
  53. get;
  54. }
  55. /// <summary>
  56. /// Draws the vector shape
  57. /// </summary>
  58. public override void Render()
  59. {
  60. base.Render();
  61. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  62. GraphicsDevice device = graphicsService.GraphicsDevice;
  63. device.SetVertexBuffer(buffer);
  64. worldViewProjectionParam.SetValue(World * SpacewarGame.Camera.View * SpacewarGame.Camera.Projection);
  65. effect.Techniques[0].Passes[0].Apply();
  66. device.DrawPrimitives(PrimitiveType.LineList, 0, NumberOfVectors);
  67. }
  68. public override void OnCreateDevice()
  69. {
  70. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  71. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\simple");
  72. worldViewProjectionParam = effect.Parameters["worldViewProjection"];
  73. buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), 2 * NumberOfVectors, BufferUsage.WriteOnly);
  74. VertexPositionColor[] data = new VertexPositionColor[2 * NumberOfVectors];
  75. FillBuffer(data);
  76. buffer.SetData<VertexPositionColor>(data);
  77. }
  78. }
  79. }