PrimitivesSampleGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PrimitivesSampleGame.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;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Input;
  17. #endregion
  18. namespace PrimitivesSample
  19. {
  20. // This sample illustrates the use of PrimitiveBatch to draw lines and points
  21. // on the screen. Lines and points are used to recreate the Spacewars starter kit's
  22. // retro mode.
  23. public class PrimitivesSampleGame : Microsoft.Xna.Framework.Game
  24. {
  25. #region Constants
  26. // this constant controls the number of stars that will be created when the game
  27. // starts up.
  28. const int NumStars = 500;
  29. // what percentage of those stars will be "big" stars? the default is 20%.
  30. const float PercentBigStars = .2f;
  31. // how bright will stars be? somewhere between these two values.
  32. const byte MinimumStarBrightness = 56;
  33. const byte MaximumStarBrightness = 255;
  34. // how big is the ship?
  35. const float ShipSizeX = 10f;
  36. const float ShipSizeY = 15f;
  37. const float ShipCutoutSize = 5f;
  38. // the radius of the sun.
  39. const float SunSize = 30f;
  40. #endregion
  41. #region Fields
  42. GraphicsDeviceManager graphics;
  43. // PrimitiveBatch is the new class introduced in this sample. We'll use it to
  44. // draw everything in this sample, including the stars, ships, and sun.
  45. PrimitiveBatch primitiveBatch;
  46. // these two lists, stars, and starColors, keep track of the positions and
  47. // colors of all the stars that we will randomly generate during the initialize
  48. // phase.
  49. List<Vector2> stars = new List<Vector2>();
  50. List<Color> starColors = new List<Color>();
  51. #endregion
  52. #region Initialization
  53. public PrimitivesSampleGame()
  54. {
  55. graphics = new GraphicsDeviceManager(this);
  56. Content.RootDirectory = "Content";
  57. #if WINDOWS_PHONE || IPHONE || ANDROID
  58. TargetElapsedTime = TimeSpan.FromTicks(333333);
  59. graphics.PreferredBackBufferWidth = 480;
  60. graphics.PreferredBackBufferHeight = 800;
  61. graphics.IsFullScreen = true;
  62. #else
  63. // set the backbuffer size to something that will work well on both xbox
  64. // and windows.
  65. graphics.PreferredBackBufferWidth = 853;
  66. graphics.PreferredBackBufferHeight = 480;
  67. #endif
  68. }
  69. protected override void Initialize()
  70. {
  71. base.Initialize();
  72. // CreateStars needs to know how big the GraphicsDevice's viewport is, so
  73. // once base.Initialize has been called, we can call this.
  74. CreateStars();
  75. }
  76. private void CreateStars()
  77. {
  78. // since every star will be put in a random place and have a random color,
  79. // a random number generator might come in handy.
  80. Random random = new Random();
  81. // where can we put the stars?
  82. int screenWidth = graphics.GraphicsDevice.Viewport.Width;
  83. int screenHeight = graphics.GraphicsDevice.Viewport.Height;
  84. for (int i = 0; i < NumStars; i++)
  85. {
  86. // pick a random spot...
  87. Vector2 where = new Vector2(
  88. random.Next(0, screenWidth),
  89. random.Next(0, screenHeight));
  90. // ...and a random color. it's safe to cast random.Next to a byte,
  91. // because MinimumStarBrightness and MaximumStarBrightness are both
  92. // bytes.
  93. byte greyValue =
  94. (byte)random.Next(MinimumStarBrightness, MaximumStarBrightness);
  95. Color color = new Color(greyValue, greyValue, greyValue);
  96. // if the random number was greater than the percentage chance for a big
  97. // star, this is just a normal star.
  98. if ((float)random.NextDouble() > PercentBigStars)
  99. {
  100. starColors.Add(color);
  101. stars.Add(where);
  102. }
  103. else
  104. {
  105. // if this star is randomly selected to be a "big" star, we actually
  106. // add four points and colors to stars and starColors. big stars are
  107. // a block of four points, instead of just one point.
  108. for (int j = 0; j < 4; j++)
  109. {
  110. starColors.Add(color);
  111. }
  112. stars.Add(where);
  113. stars.Add(where + Vector2.UnitX);
  114. stars.Add(where + Vector2.UnitY);
  115. stars.Add(where + Vector2.One);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Load your graphics content.
  121. /// </summary>
  122. protected override void LoadContent()
  123. {
  124. primitiveBatch = new PrimitiveBatch(graphics.GraphicsDevice);
  125. }
  126. #endregion
  127. #region Update and Draw
  128. /// <summary>
  129. /// Allows the game to run logic such as updating the world,
  130. /// checking for collisions, gathering input and playing audio.
  131. /// </summary>
  132. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  133. protected override void Update(GameTime gameTime)
  134. {
  135. // Allows the game to exit
  136. if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  137. || Keyboard.GetState().IsKeyDown(Keys.Escape))
  138. this.Exit();
  139. base.Update(gameTime);
  140. }
  141. /// <summary>
  142. /// This is called when the game should draw itself.
  143. /// </summary>
  144. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  145. protected override void Draw(GameTime gameTime)
  146. {
  147. graphics.GraphicsDevice.Clear(Color.Black);
  148. // how big is the screen? we'll use that information to center the sun
  149. // and place the ships.
  150. int screenWidth = graphics.GraphicsDevice.Viewport.Width;
  151. int screenHeight = graphics.GraphicsDevice.Viewport.Height;
  152. // draw the sun in the center
  153. DrawSun(new Vector2(screenWidth / 2, screenHeight / 2));
  154. // draw the left hand ship
  155. DrawShip(new Vector2(100, screenHeight / 2));
  156. // and the right hand ship
  157. DrawShip(new Vector2(screenWidth - 100, screenHeight / 2));
  158. DrawStars();
  159. base.Draw(gameTime);
  160. }
  161. // DrawStars is called to do exactly what its name says: draw the stars.
  162. private void DrawStars()
  163. {
  164. // stars are drawn as a list of points, so begin the primitiveBatch.
  165. primitiveBatch.Begin(PrimitiveType.TriangleList);
  166. // loop through all of the stars, and tell primitive batch to draw them.
  167. // each star is a very small triangle.
  168. for (int i = 0; i < stars.Count; i++)
  169. {
  170. primitiveBatch.AddVertex(stars[i], starColors[i]);
  171. primitiveBatch.AddVertex(stars[i] + Vector2.UnitX, starColors[i]);
  172. primitiveBatch.AddVertex(stars[i] + Vector2.UnitY, starColors[i]);
  173. }
  174. // and then tell it that we're done.
  175. primitiveBatch.End();
  176. }
  177. // called to draw the spacewars ship at a point on the screen.
  178. private void DrawShip(Vector2 where)
  179. {
  180. // tell the primitive batch to start drawing lines
  181. primitiveBatch.Begin(PrimitiveType.LineList);
  182. // from the nose, down the left hand side
  183. primitiveBatch.AddVertex(
  184. where + new Vector2(0f, -ShipSizeY), Color.White);
  185. primitiveBatch.AddVertex(
  186. where + new Vector2(-ShipSizeX, ShipSizeY), Color.White);
  187. // to the right and up, into the cutout
  188. primitiveBatch.AddVertex(
  189. where + new Vector2(-ShipSizeX, ShipSizeY), Color.White);
  190. primitiveBatch.AddVertex(
  191. where + new Vector2(0f, ShipSizeY - ShipCutoutSize), Color.White);
  192. // to the right and down, out of the cutout
  193. primitiveBatch.AddVertex(
  194. where + new Vector2(0f, ShipSizeY - ShipCutoutSize), Color.White);
  195. primitiveBatch.AddVertex(
  196. where + new Vector2(ShipSizeX, ShipSizeY), Color.White);
  197. // and back up to the nose, where we started.
  198. primitiveBatch.AddVertex(
  199. where + new Vector2(ShipSizeX, ShipSizeY), Color.White);
  200. primitiveBatch.AddVertex(
  201. where + new Vector2(0f, -ShipSizeY), Color.White);
  202. // and we're done.
  203. primitiveBatch.End();
  204. }
  205. // called to draw the spacewars sun.
  206. private void DrawSun(Vector2 where)
  207. {
  208. // the sun is made from 4 lines in a circle.
  209. primitiveBatch.Begin(PrimitiveType.LineList);
  210. // draw the vertical and horizontal lines
  211. primitiveBatch.AddVertex(where + new Vector2(0, SunSize), Color.White);
  212. primitiveBatch.AddVertex(where + new Vector2(0, -SunSize), Color.White);
  213. primitiveBatch.AddVertex(where + new Vector2(SunSize, 0), Color.White);
  214. primitiveBatch.AddVertex(where + new Vector2(-SunSize, 0), Color.White);
  215. // to know where to draw the diagonal lines, we need to use trig.
  216. // cosine of pi / 4 tells us what the x coordinate of a circle's radius is
  217. // at 45 degrees. the y coordinate normally would come from sin, but sin and
  218. // cos 45 are the same, so we can reuse cos for both x and y.
  219. float sunSizeDiagonal = (float)Math.Cos(MathHelper.PiOver4);
  220. // since that trig tells us the x and y for a unit circle, which has a
  221. // radius of 1, we need scale that result by the sun's radius.
  222. sunSizeDiagonal *= SunSize;
  223. primitiveBatch.AddVertex(
  224. where + new Vector2(-sunSizeDiagonal, sunSizeDiagonal), Color.Gray);
  225. primitiveBatch.AddVertex(
  226. where + new Vector2(sunSizeDiagonal, -sunSizeDiagonal), Color.Gray);
  227. primitiveBatch.AddVertex(
  228. where + new Vector2(sunSizeDiagonal, sunSizeDiagonal), Color.Gray);
  229. primitiveBatch.AddVertex(
  230. where + new Vector2(-sunSizeDiagonal, -sunSizeDiagonal), Color.Gray);
  231. primitiveBatch.End();
  232. }
  233. #endregion
  234. // #region Entry point
  235. //
  236. // /// <summary>
  237. // /// The main entry point for the application.
  238. // /// </summary>
  239. // static void Main()
  240. // {
  241. // using (PrimitivesSampleGame game = new PrimitivesSampleGame())
  242. // {
  243. // game.Run();
  244. // }
  245. // }
  246. //
  247. // #endregion
  248. }
  249. }