Shape.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Shape.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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace Spacewar
  15. {
  16. /// <summary>
  17. /// Shape is the base class of any object that is renderable
  18. /// </summary>
  19. public abstract class Shape : IDisposable
  20. {
  21. /// <summary>
  22. /// The vertex buffer used by this shape
  23. /// </summary>
  24. protected VertexBuffer buffer;
  25. protected VertexDeclaration vertexDecl;
  26. /// <summary>
  27. /// The current world matrix used to render this shape
  28. /// </summary>
  29. protected Matrix world;
  30. protected Vector3 position;
  31. private Game game = null;
  32. #region Properties
  33. public Matrix World
  34. {
  35. get
  36. {
  37. return world;
  38. }
  39. set
  40. {
  41. world = value;
  42. }
  43. }
  44. public Vector3 Position
  45. {
  46. get
  47. {
  48. return position;
  49. }
  50. set
  51. {
  52. position = value;
  53. }
  54. }
  55. protected Game GameInstance
  56. {
  57. get
  58. {
  59. return game;
  60. }
  61. }
  62. #endregion
  63. /// <summary>
  64. /// Creates a new shape. Calls the virtual Create method to generate any vertex buffers etc
  65. /// </summary>
  66. public Shape(Game game)
  67. {
  68. this.game = game;
  69. Create();
  70. }
  71. /// <summary>
  72. /// Creates the vertex buffers etc. This routine is called on object creation and on device reset etc
  73. /// </summary>
  74. abstract public void Create();
  75. /// <summary>
  76. /// Renders the shape. Base class does nothing
  77. /// </summary>
  78. public virtual void Render()
  79. {
  80. }
  81. /// <summary>
  82. /// Updates the shape. Base class does nothing
  83. /// </summary>
  84. /// <param name="time">Game Time</param>
  85. /// <param name="elapsedTime">Elapsed game time since last call</param>
  86. public virtual void Update(TimeSpan time, TimeSpan elapsedTime)
  87. {
  88. //Nothing for now
  89. }
  90. /// <summary>
  91. /// Creates a rectangle with a certain number of rows and columns. The buffer is PositionOnly and always 0.0-1.0
  92. /// </summary>
  93. /// <param name="Columns">Number of columns</param>
  94. /// <param name="Rows">Number of Rows</param>
  95. /// <returns>Populated Vertex buffer</returns>
  96. public VertexBuffer Plane(int Columns, int Rows)
  97. {
  98. //TODO: Would be better to have a PositionOnly vertex type here but that's not in this build
  99. //Its used for the backdrop of evolved which is a full screen quad with texture coordinates derived from
  100. //other shader variables
  101. //TODO: Fix up shader for evolved backdrop to ignore the color in the vertex format
  102. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
  103. VertexBuffer Buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), Columns * Rows * 6, BufferUsage.WriteOnly);
  104. //VertexPositionColor data = Buffer.Lock<PositionOnly>(0, 0, LockFlags.None);
  105. VertexPositionColor[] data = new VertexPositionColor[Columns * Rows * 6];
  106. //Buffer coordinates are 0.0-1.0 so we can use them as the base texture coordinates too
  107. int pointCount = 0;
  108. for (int x = 0; x < Columns; x++)
  109. {
  110. for (int y = 0; y < Rows; y++)
  111. {
  112. data[pointCount + 0] = new VertexPositionColor(new Vector3((float)x / (float)Columns, (float)y / (float)Rows, 0), Color.White);
  113. data[pointCount + 1] = new VertexPositionColor(new Vector3((float)(x + 1) / (float)Columns, (float)y / (float)Rows, 0), Color.White);
  114. data[pointCount + 2] = new VertexPositionColor(new Vector3((float)(x + 1) / (float)Columns, (float)(y + 1) / (float)Rows, 0), Color.White);
  115. data[pointCount + 3] = new VertexPositionColor(new Vector3((float)x / (float)Columns, (float)y / (float)Rows, 0), Color.White); //same as 0
  116. data[pointCount + 4] = new VertexPositionColor(new Vector3((float)(x + 1) / (float)Columns, (float)(y + 1) / (float)Rows, 0), Color.White); //same as 2
  117. data[pointCount + 5] = new VertexPositionColor(new Vector3((float)x / (float)Columns, (float)(y + 1) / (float)Rows, 0), Color.White);
  118. pointCount += 6;
  119. }
  120. }
  121. Buffer.SetData<VertexPositionColor>(data);
  122. return Buffer;
  123. }
  124. /// <summary>
  125. /// Called when a device is created
  126. /// </summary>
  127. public virtual void OnCreateDevice()
  128. {
  129. Create();
  130. }
  131. protected virtual void Dispose(bool all)
  132. {
  133. if (buffer != null)
  134. {
  135. buffer.Dispose();
  136. buffer = null;
  137. }
  138. if (vertexDecl != null)
  139. {
  140. vertexDecl.Dispose();
  141. vertexDecl = null;
  142. }
  143. }
  144. public virtual void Dispose()
  145. {
  146. Dispose(true);
  147. GC.SuppressFinalize(this);
  148. }
  149. }
  150. }