RetroProjectiles.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // RetroProjectiles.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. using System.Diagnostics;
  16. #endregion
  17. namespace Spacewar
  18. {
  19. /// <summary>
  20. /// A collection to batch render the bullets for retro mode
  21. /// </summary>
  22. public class RetroProjectiles : Projectiles
  23. {
  24. private Effect effect;
  25. private EffectParameter worldViewProjectionParam;
  26. private VertexBuffer buffer;
  27. private const int maxTriangleCount = 200;
  28. private const int maxProjectileCount = maxTriangleCount * 3;
  29. VertexPositionColor[] data;
  30. public RetroProjectiles(Game game)
  31. : base(game)
  32. {
  33. Create();
  34. }
  35. /// <summary>
  36. /// Creates a group of projectiles
  37. /// </summary>
  38. /// <param name="player">Which player shot the bullet</param>
  39. /// <param name="position">Start position of projectile</param>
  40. /// <param name="velocity">Initial velocity of projectile</param>
  41. /// <param name="angle">Direction projectile is facing</param>
  42. /// <param name="time">Game time that this projectile was shot</param>
  43. /// <param name="particles">The particles to add to for effects</param>
  44. public override void Add(PlayerIndex player, Vector3 position, Vector3 velocity, float angle, TimeSpan time, Particles particles)
  45. {
  46. Add(new Projectile(GameInstance, player, position, velocity, angle, time, null));
  47. }
  48. /// <summary>
  49. /// Renders all bullets in a single batch
  50. /// </summary>
  51. public override void Render()
  52. {
  53. if (Count > 0)
  54. {
  55. base.Render();
  56. //RETRO
  57. //All bullets are rendered in a single draw call.
  58. //Build a new vertex buffer with all the bullets
  59. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  60. GraphicsDevice device = graphicsService.GraphicsDevice;
  61. // Don't exceed the maximum number of vertices in our default Vertex Buffer!
  62. int totalCount = (Count > maxTriangleCount) ? maxTriangleCount : Count;
  63. int position = 0;
  64. foreach (Projectile bullet in this)
  65. {
  66. if (position < maxProjectileCount)
  67. {
  68. Vector3 bulletPosition = bullet.Position;
  69. data[position++].Position = bulletPosition;
  70. data[position++].Position = bulletPosition + new Vector3(-1, 0, 0);
  71. data[position++].Position = bulletPosition + new Vector3(0, 1, 0);
  72. bulletPosition.X += .5f;
  73. data[position++].Position = bulletPosition;
  74. data[position++].Position = bulletPosition + new Vector3(-1, 0, 0);
  75. data[position++].Position = bulletPosition + new Vector3(0, 1, 0);
  76. }
  77. }
  78. device.RasterizerState = RasterizerState.CullNone;
  79. worldViewProjectionParam.SetValue(SpacewarGame.Camera.View * SpacewarGame.Camera.Projection);
  80. effect.Techniques[0].Passes[0].Apply();
  81. device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, data, 0, totalCount * 2);
  82. //We DO NOT call the base class here since we have handled all the children
  83. }
  84. }
  85. public void Create()
  86. {
  87. //Load the correct shader and set up the parameters
  88. if (effect == null || effect.IsDisposed)
  89. {
  90. OnCreateDevice();
  91. }
  92. }
  93. public override void OnCreateDevice()
  94. {
  95. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  96. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\simple");
  97. worldViewProjectionParam = effect.Parameters["worldViewProjection"];
  98. buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), maxProjectileCount * 4, BufferUsage.WriteOnly);
  99. data = new VertexPositionColor[maxProjectileCount * 4];
  100. // initialize the Projectile Pool
  101. for (int position = 0; position < maxProjectileCount * 4; position++)
  102. {
  103. data[position] = new VertexPositionColor(new Vector3(0.0f, 0.0f, 0.0f), Color.White);
  104. }
  105. }
  106. }
  107. }