Game1.cs 30 KB

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