RetroStarfield.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // RetroStarfield.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. /// <summary>
  19. /// The stars for the background of retro mode
  20. /// </summary>
  21. public class RetroStarfield : Shape
  22. {
  23. private const int numberOfTriangles = 800;
  24. private const int numberOfPoints = numberOfTriangles * 3; // Each point is actually a triangle
  25. private const int percentBigStars = 20;
  26. private Effect effect;
  27. private EffectParameter worldViewProjectionParam;
  28. public RetroStarfield(Game game)
  29. : base(game)
  30. {
  31. }
  32. /// <summary>
  33. /// Creates vertex buffer full of points
  34. /// </summary>
  35. public override void Create()
  36. {
  37. OnCreateDevice();
  38. }
  39. /// <summary>
  40. /// Renders the starfield
  41. /// </summary>
  42. public override void Render()
  43. {
  44. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  45. GraphicsDevice device = graphicsService.GraphicsDevice;
  46. device.SetVertexBuffer(buffer);
  47. worldViewProjectionParam.SetValue(World * SpacewarGame.Camera.View * SpacewarGame.Camera.Projection);
  48. effect.Techniques[0].Passes[0].Apply();
  49. device.DrawPrimitives(PrimitiveType.TriangleList, 0, numberOfTriangles);
  50. }
  51. public override void OnCreateDevice()
  52. {
  53. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  54. effect = SpacewarGame.ContentManager.Load<Effect>(SpacewarGame.Settings.MediaPath + @"shaders\simple");
  55. worldViewProjectionParam = effect.Parameters["worldViewProjection"];
  56. buffer = new VertexBuffer(graphicsService.GraphicsDevice, typeof(VertexPositionColor), numberOfPoints, BufferUsage.WriteOnly);
  57. VertexPositionColor[] data = new VertexPositionColor[numberOfPoints];
  58. int pointCount = 0;
  59. int triangleCount = 0;
  60. Random random = new Random();
  61. while (triangleCount < numberOfTriangles)
  62. {
  63. byte greyValue = (byte)(random.Next(200) + 56); // 56-255
  64. Color color = new Color(greyValue, greyValue, greyValue);
  65. Vector2 position = new Vector2(random.Next(560) - 280, random.Next(420) - 210);
  66. //Add a big star if the time is right and there is room in the buffer
  67. if (random.Next(100) < percentBigStars && (triangleCount + 2) < numberOfTriangles)
  68. {
  69. //Big stars are just 4 points
  70. data[pointCount++] = new VertexPositionColor(new Vector3(position.X, position.Y, 0), color);
  71. data[pointCount++] = new VertexPositionColor(new Vector3(position.X - 1f, position.Y, 0), color);
  72. data[pointCount++] = new VertexPositionColor(new Vector3(position.X, position.Y + 1f, 0), color);
  73. data[pointCount++] = new VertexPositionColor(new Vector3(position.X - 1f, position.Y, 0), color);
  74. data[pointCount++] = new VertexPositionColor(new Vector3(position.X - 1f, position.Y + 1f, 0), color);
  75. data[pointCount++] = new VertexPositionColor(new Vector3(position.X, position.Y + 1f, 0), color);
  76. triangleCount += 2;
  77. }
  78. else
  79. {
  80. data[pointCount++] = new VertexPositionColor(new Vector3(position.X, position.Y, 0), color);
  81. data[pointCount++] = new VertexPositionColor(new Vector3(position.X - 0.5f, position.Y, 0), color);
  82. data[pointCount++] = new VertexPositionColor(new Vector3(position.X, position.Y + 0.5f, 0), color);
  83. triangleCount++;
  84. }
  85. }
  86. buffer.SetData<VertexPositionColor>(data);
  87. }
  88. }
  89. }