Game1.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game1.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. using Microsoft.Xna.Framework.Media;
  20. namespace DrawUserPrimitivesWindows
  21. {
  22. /// <summary>
  23. /// This is the main type for your game
  24. /// </summary>
  25. public class Game1 : Microsoft.Xna.Framework.Game
  26. {
  27. Matrix worldMatrix;
  28. Matrix viewMatrix;
  29. Matrix projectionMatrix;
  30. BasicEffect basicEffect;
  31. VertexDeclaration vertexDeclaration;
  32. VertexPositionColor[] pointList;
  33. VertexBuffer vertexBuffer;
  34. int points = 8;
  35. short[] lineListIndices;
  36. short[] lineStripIndices;
  37. short[] triangleListIndices;
  38. short[] triangleStripIndices;
  39. enum PrimType
  40. {
  41. LineList,
  42. LineStrip,
  43. TriangleList,
  44. TriangleStrip
  45. };
  46. PrimType typeToDraw = PrimType.LineList;
  47. RasterizerState rasterizerState;
  48. GamePadState currentGamePadState;
  49. GamePadState lastGamePadState;
  50. KeyboardState currentKeyboardState;
  51. KeyboardState lastKeyboardState;
  52. GraphicsDeviceManager graphics;
  53. public Game1()
  54. {
  55. graphics = new GraphicsDeviceManager(this);
  56. Content.RootDirectory = "Content";
  57. }
  58. /// <summary>
  59. /// Allows the game to perform any initialization it needs to before starting to run.
  60. /// This is where it can query for any required services and load any non-graphic
  61. /// related content. Calling base.Initialize will enumerate through any components
  62. /// and initialize them as well.
  63. /// </summary>
  64. protected override void Initialize()
  65. {
  66. InitializeTransform();
  67. InitializeEffect();
  68. InitializePoints();
  69. InitializeLineList();
  70. InitializeLineStrip();
  71. InitializeTriangleList();
  72. InitializeTriangleStrip();
  73. rasterizerState = new RasterizerState();
  74. rasterizerState.FillMode = FillMode.WireFrame;
  75. rasterizerState.CullMode = CullMode.None;
  76. base.Initialize();
  77. }
  78. /// <summary>
  79. /// LoadContent will be called once per game and is the place to load
  80. /// all of your content.
  81. /// </summary>
  82. protected override void LoadContent()
  83. {
  84. // TODO: use this.Content to load your game content here
  85. }
  86. /// <summary>
  87. /// UnloadContent will be called once per game and is the place to unload
  88. /// all content.
  89. /// </summary>
  90. protected override void UnloadContent()
  91. {
  92. // TODO: Unload any non ContentManager content here
  93. }
  94. /// <summary>
  95. /// Initializes the transforms used by the game.
  96. /// </summary>
  97. private void InitializeTransform()
  98. {
  99. viewMatrix = Matrix.CreateLookAt(
  100. new Vector3(0.0f, 0.0f, 1.0f),
  101. Vector3.Zero,
  102. Vector3.Up
  103. );
  104. projectionMatrix = Matrix.CreateOrthographicOffCenter(
  105. 0,
  106. (float)GraphicsDevice.Viewport.Width,
  107. (float)GraphicsDevice.Viewport.Height,
  108. 0,
  109. 1.0f, 1000.0f);
  110. }
  111. /// <summary>
  112. /// Initializes the effect (loading, parameter setting, and technique selection)
  113. /// used by the game.
  114. /// </summary>
  115. private void InitializeEffect()
  116. {
  117. vertexDeclaration = new VertexDeclaration(new VertexElement[]
  118. {
  119. new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
  120. new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0)
  121. }
  122. );
  123. basicEffect = new BasicEffect(GraphicsDevice);
  124. basicEffect.VertexColorEnabled = true;
  125. worldMatrix = Matrix.CreateTranslation(GraphicsDevice.Viewport.Width / 2f - 150,
  126. GraphicsDevice.Viewport.Height / 2f - 50, 0);
  127. basicEffect.World = worldMatrix;
  128. basicEffect.View = viewMatrix;
  129. basicEffect.Projection = projectionMatrix;
  130. }
  131. /// <summary>
  132. /// Initializes the point list.
  133. /// </summary>
  134. private void InitializePoints()
  135. {
  136. pointList = new VertexPositionColor[points];
  137. for (int x = 0; x < points / 2; x++)
  138. {
  139. for (int y = 0; y < 2; y++)
  140. {
  141. pointList[(x * 2) + y] = new VertexPositionColor(
  142. new Vector3(x * 100, y * 100, 0), Color.White);
  143. }
  144. }
  145. // Initialize the vertex buffer, allocating memory for each vertex.
  146. vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, vertexDeclaration,
  147. points, BufferUsage.None);
  148. // Set the vertex buffer data to the array of vertices.
  149. vertexBuffer.SetData<VertexPositionColor>(pointList);
  150. }
  151. /// <summary>
  152. /// Initializes the line list.
  153. /// </summary>
  154. private void InitializeLineList()
  155. {
  156. // Initialize an array of indices of type short.
  157. lineListIndices = new short[(points * 2) - 2];
  158. // Populate the array with references to indices in the vertex buffer
  159. for (int i = 0; i < points - 1; i++)
  160. {
  161. lineListIndices[i * 2] = (short)(i);
  162. lineListIndices[(i * 2) + 1] = (short)(i + 1);
  163. }
  164. }
  165. /// <summary>
  166. /// Initializes the line strip.
  167. /// </summary>
  168. private void InitializeLineStrip()
  169. {
  170. // Initialize an array of indices of type short.
  171. lineStripIndices = new short[points];
  172. // Populate the array with references to indices in the vertex buffer.
  173. for (int i = 0; i < points; i++)
  174. {
  175. lineStripIndices[i] = (short)(i);
  176. }
  177. }
  178. /// <summary>
  179. /// Initializes the triangle list.
  180. /// </summary>
  181. private void InitializeTriangleList()
  182. {
  183. int width = 4;
  184. int height = 2;
  185. triangleListIndices = new short[(width - 1) * (height - 1) * 6];
  186. for (int x = 0; x < width - 1; x++)
  187. {
  188. for (int y = 0; y < height - 1; y++)
  189. {
  190. triangleListIndices[(x + y * (width - 1)) * 6] = (short)(2 * x);
  191. triangleListIndices[(x + y * (width - 1)) * 6 + 1] = (short)(2 * x + 1);
  192. triangleListIndices[(x + y * (width - 1)) * 6 + 2] = (short)(2 * x + 2);
  193. triangleListIndices[(x + y * (width - 1)) * 6 + 3] = (short)(2 * x + 2);
  194. triangleListIndices[(x + y * (width - 1)) * 6 + 4] = (short)(2 * x + 1);
  195. triangleListIndices[(x + y * (width - 1)) * 6 + 5] = (short)(2 * x + 3);
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// Initializes the triangle strip.
  201. /// </summary>
  202. private void InitializeTriangleStrip()
  203. {
  204. // Initialize an array of indices of type short.
  205. triangleStripIndices = new short[points];
  206. // Populate the array with references to indices in the vertex buffer.
  207. for (int i = 0; i < points; i++)
  208. {
  209. triangleStripIndices[i] = (short)i;
  210. }
  211. }
  212. /// <summary>
  213. /// Allows the game to run logic such as updating the world,
  214. /// checking for collisions, gathering input, and playing audio.
  215. /// </summary>
  216. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  217. protected override void Update(GameTime gameTime)
  218. {
  219. // Allows the game to exit
  220. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  221. this.Exit();
  222. #if MAC || WINDOWS
  223. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  224. this.Exit();
  225. #endif
  226. CheckGamePadInput();
  227. CheckKeyboardInput();
  228. base.Update(gameTime);
  229. }
  230. /// <summary>
  231. /// Determines which primitive to draw based on input from the keyboard
  232. /// or game pad.
  233. /// </summary>
  234. private void CheckGamePadInput()
  235. {
  236. lastGamePadState = currentGamePadState;
  237. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  238. if (((currentGamePadState.Buttons.A == ButtonState.Pressed)) &&
  239. (lastGamePadState.Buttons.A == ButtonState.Released))
  240. {
  241. typeToDraw++;
  242. if (typeToDraw > PrimType.TriangleStrip)
  243. typeToDraw = 0;
  244. }
  245. }
  246. /// <summary>
  247. /// Determines which primitive to draw based on input from the keyboard
  248. /// or game pad.
  249. /// </summary>
  250. private void CheckKeyboardInput()
  251. {
  252. lastKeyboardState = currentKeyboardState;
  253. currentKeyboardState = Keyboard.GetState();
  254. if (currentKeyboardState.IsKeyDown(Keys.A) &&
  255. lastKeyboardState.IsKeyUp(Keys.A))
  256. {
  257. typeToDraw++;
  258. if (typeToDraw > PrimType.TriangleStrip)
  259. typeToDraw = 0;
  260. }
  261. }
  262. /// <summary>
  263. /// This is called when the game should draw itself.
  264. /// </summary>
  265. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  266. protected override void Draw(GameTime gameTime)
  267. {
  268. GraphicsDevice.Clear(Color.SteelBlue);
  269. // The effect is a compiled effect created and compiled elsewhere
  270. // in the application.
  271. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
  272. {
  273. pass.Apply();
  274. switch (typeToDraw)
  275. {
  276. case PrimType.LineList:
  277. DrawLineList();
  278. break;
  279. // case PrimType.LineStrip:
  280. // DrawLineStrip();
  281. // break;
  282. // case PrimType.TriangleList:
  283. // GraphicsDevice.RasterizerState = rasterizerState;
  284. // DrawTriangleList();
  285. // break;
  286. // case PrimType.TriangleStrip:
  287. // GraphicsDevice.RasterizerState = rasterizerState;
  288. // DrawTriangleStrip();
  289. // break;
  290. }
  291. }
  292. base.Draw(gameTime);
  293. }
  294. /// <summary>
  295. /// Draws the line list.
  296. /// </summary>
  297. private void DrawLineList()
  298. {
  299. GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
  300. PrimitiveType.LineList,
  301. pointList,
  302. 0, // vertex buffer offset to add to each element of the index buffer
  303. 8, // number of vertices in pointList
  304. lineListIndices, // the index buffer
  305. 0, // first index element to read
  306. 7 // number of primitives to draw
  307. );
  308. }
  309. /// <summary>
  310. /// Draws the line strip.
  311. /// </summary>
  312. // private void DrawLineStrip()
  313. // {
  314. // for (int i = 0; i < pointList.Length; i++)
  315. // pointList[i].Color = Color.Red;
  316. //
  317. // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
  318. // PrimitiveType.LineStrip,
  319. // pointList,
  320. // 0, // vertex buffer offset to add to each element of the index buffer
  321. // 8, // number of vertices to draw
  322. // lineStripIndices,
  323. // 0, // first index element to read
  324. // 7 // number of primitives to draw
  325. // );
  326. // for (int i = 0; i < pointList.Length; i++)
  327. // pointList[i].Color = Color.White;
  328. //
  329. // }
  330. //
  331. // /// <summary>
  332. // /// Draws the triangle list.
  333. // /// </summary>
  334. // private void DrawTriangleList()
  335. // {
  336. // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
  337. // PrimitiveType.TriangleList,
  338. // pointList,
  339. // 0, // vertex buffer offset to add to each element of the index buffer
  340. // 8, // number of vertices to draw
  341. // triangleListIndices,
  342. // 0, // first index element to read
  343. // 6 // number of primitives to draw
  344. // );
  345. // }
  346. //
  347. // /// <summary>
  348. // /// Draws the triangle strip.
  349. // /// </summary>
  350. // private void DrawTriangleStrip()
  351. // {
  352. // for (int i = 0; i < pointList.Length; i++)
  353. // pointList[i].Color = Color.Red;
  354. //
  355. // GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
  356. // PrimitiveType.TriangleStrip,
  357. // pointList,
  358. // 0, // vertex buffer offset to add to each element of the index buffer
  359. // 8, // number of vertices to draw
  360. // triangleStripIndices,
  361. // 0, // first index element to read
  362. // 6 // number of primitives to draw
  363. // );
  364. // for (int i = 0; i < pointList.Length; i++)
  365. // pointList[i].Color = Color.White;
  366. //
  367. // }
  368. }
  369. }