TrianglesStrip.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #region File description
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <copyright file="TrianglesStrip.cs" company="GAMADU.COM">
  4. // Copyright © 2013 GAMADU.COM. All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without modification, are
  7. // permitted provided that the following conditions are met:
  8. //
  9. // 1. Redistributions of source code must retain the above copyright notice, this list of
  10. // conditions and the following disclaimer.
  11. //
  12. // 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. // of conditions and the following disclaimer in the documentation and/or other materials
  14. // provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY GAMADU.COM 'AS IS' AND ANY EXPRESS OR IMPLIED
  17. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GAMADU.COM OR
  19. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. //
  26. // The views and conclusions contained in the software and documentation are those of the
  27. // authors and should not be interpreted as representing official policies, either expressed
  28. // or implied, of GAMADU.COM.
  29. // </copyright>
  30. // <summary>
  31. // The triangles strip.
  32. // </summary>
  33. // --------------------------------------------------------------------------------------------------------------------
  34. #endregion File description
  35. namespace StarWarrior.Primitives
  36. {
  37. #region Using statements
  38. using System;
  39. using System.Collections.Generic;
  40. using Microsoft.Xna.Framework;
  41. using Microsoft.Xna.Framework.Graphics;
  42. #endregion
  43. /// <summary>The triangles strip.</summary>
  44. public class TrianglesStrip
  45. {
  46. /// <summary>The batch.</summary>
  47. private readonly PrimitiveBatch batch;
  48. /// <summary>The device.</summary>
  49. private readonly GraphicsDevice device;
  50. /// <summary>The point.</summary>
  51. private readonly List<Vector2> point;
  52. /// <summary>The state.</summary>
  53. private readonly RasterizerState state;
  54. /// <summary>Initializes a new instance of the <see cref="TrianglesStrip" /> class.</summary>
  55. /// <param name="device">The device.</param>
  56. /// <param name="primitiveBatch">The primitive batch.</param>
  57. public TrianglesStrip(GraphicsDevice device, PrimitiveBatch primitiveBatch)
  58. {
  59. this.IsFilled = true;
  60. this.Color = Color.White;
  61. this.point = new List<Vector2>();
  62. this.device = device;
  63. this.batch = primitiveBatch;
  64. this.state = new RasterizerState
  65. {
  66. CullMode = CullMode.CullCounterClockwiseFace,
  67. FillMode = FillMode.WireFrame
  68. };
  69. }
  70. /// <summary>Gets or sets the color.</summary>
  71. /// <value>The color.</value>
  72. private Color Color { get; set; }
  73. /// <summary>Gets or sets a value indicating whether this instance is filled.</summary>
  74. /// <value><see langword="true" /> if this instance is filled; otherwise, <see langword="false" />.</value>
  75. private bool IsFilled { get; set; }
  76. /// <summary>The add point.</summary>
  77. /// <param name="x1">The x 1.</param>
  78. /// <param name="y1">The y 1.</param>
  79. public void AddPoint(float x1, float y1)
  80. {
  81. this.point.Add(new Vector2(x1, y1));
  82. }
  83. /// <summary>The draw.</summary>
  84. /// <param name="transform">The transform.</param>
  85. /// <exception cref="System.InvalidOperationException">Need at least 3 points in order to draw the triangles.</exception>
  86. /// <exception cref="InvalidOperationException"></exception>
  87. public void Draw(Vector2 transform)
  88. {
  89. if (this.IsFilled == false)
  90. {
  91. this.device.RasterizerState = this.state;
  92. }
  93. if (this.point.Count < 3)
  94. {
  95. throw new InvalidOperationException("Need at least 3 points in order to draw the triangles.");
  96. }
  97. this.batch.Begin(PrimitiveType.TriangleStrip);
  98. foreach (Vector2 item in this.point)
  99. {
  100. this.batch.AddVertex(item + transform, this.Color);
  101. }
  102. this.batch.End();
  103. if (this.IsFilled == false)
  104. {
  105. this.device.RasterizerState = RasterizerState.CullCounterClockwise;
  106. }
  107. }
  108. }
  109. }