Game1.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 StateObjectWindows
  21. {
  22. /// <summary>
  23. /// This is the main type for your game
  24. /// </summary>
  25. public class Game1 : Microsoft.Xna.Framework.Game
  26. {
  27. GraphicsDeviceManager graphicsDeviceManager;
  28. BasicEffect basicEffect;
  29. VertexDeclaration vertexDeclaration;
  30. VertexBuffer vertexBuffer;
  31. const int number_of_vertices = 6;
  32. RasterizerState rsCullNone;
  33. bool changeState = false;
  34. public Game1()
  35. {
  36. graphicsDeviceManager = new GraphicsDeviceManager(this);
  37. Content.RootDirectory = "Content";
  38. }
  39. private void CreateEffect()
  40. {
  41. basicEffect = new BasicEffect(GraphicsDevice);
  42. }
  43. private void CreateVertexBuffer()
  44. {
  45. vertexDeclaration = new VertexDeclaration(new VertexElement[1]
  46. {
  47. new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0)
  48. }
  49. );
  50. vertexBuffer = new VertexBuffer(
  51. GraphicsDevice,
  52. vertexDeclaration,
  53. number_of_vertices,
  54. BufferUsage.None
  55. );
  56. Vector3[] vertices = new Vector3[number_of_vertices];
  57. vertices[0] = new Vector3(-1, 0, 0); // cw
  58. vertices[1] = new Vector3(0, 1, 0);
  59. vertices[2] = new Vector3(0, 0, 0);
  60. vertices[3] = new Vector3(0, 0, 0); // ccw
  61. vertices[4] = new Vector3(1, 0, 0);
  62. vertices[5] = new Vector3(0, 1, 0);
  63. vertexBuffer.SetData(vertices);
  64. }
  65. /// <summary>
  66. /// This is called when the game should draw itself.
  67. /// </summary>
  68. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  69. protected override void Draw(GameTime gameTime)
  70. {
  71. GraphicsDevice.Clear(Color.CornflowerBlue);
  72. GraphicsDevice.SetVertexBuffer(vertexBuffer);
  73. EffectPass pass = basicEffect.CurrentTechnique.Passes[0];
  74. if (pass != null)
  75. {
  76. pass.Apply();
  77. GraphicsDevice.DrawPrimitives(
  78. PrimitiveType.TriangleList, // primitive type to draw
  79. 0, // start vertex
  80. 2 // number of primitives to draw
  81. );
  82. }
  83. base.Draw(gameTime);
  84. }
  85. /// <summary>
  86. /// Allows the game to perform any initialization it needs to before starting to run.
  87. /// This is where it can query for any required services and load any non-graphic
  88. /// related content. Calling base.Initialize will enumerate through any components
  89. /// and initialize them as well.
  90. /// </summary>
  91. protected override void Initialize()
  92. {
  93. // TODO: Add your initialization logic here
  94. CreateEffect();
  95. CreateVertexBuffer();
  96. rsCullNone = new RasterizerState();
  97. rsCullNone.CullMode = CullMode.None;
  98. rsCullNone.FillMode = FillMode.WireFrame;
  99. rsCullNone.MultiSampleAntiAlias = false;
  100. base.Initialize();
  101. }
  102. /// <summary>
  103. /// LoadContent will be called once per game and is the place to load
  104. /// all of your content.
  105. /// </summary>
  106. protected override void LoadContent()
  107. {
  108. // TODO: use this.Content to load your game content here
  109. }
  110. /// <summary>
  111. /// UnloadContent will be called once per game and is the place to unload
  112. /// all content.
  113. /// </summary>
  114. protected override void UnloadContent()
  115. {
  116. // TODO: Unload any non ContentManager content here
  117. }
  118. KeyboardState current = Keyboard.GetState();
  119. KeyboardState last = Keyboard.GetState();
  120. bool keyboardChangeState = false;
  121. /// <summary>
  122. /// Allows the game to run logic such as updating the world,
  123. /// checking for collisions, gathering input, and playing audio.
  124. /// </summary>
  125. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  126. protected override void Update(GameTime gameTime)
  127. {
  128. last = current;
  129. current = Keyboard.GetState();
  130. // Exit the game from a GamePad
  131. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  132. this.Exit();
  133. // TODO: Add your update logic here
  134. // Exit the game from a Keyboard
  135. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  136. this.Exit();
  137. if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
  138. changeState = true;
  139. if (current.IsKeyDown(Keys.A) && last.IsKeyUp(Keys.A))
  140. changeState = true;
  141. if ((changeState) && (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Released
  142. || (current.IsKeyDown(Keys.A) && last.IsKeyUp(Keys.A))))
  143. {
  144. if (GraphicsDevice.RasterizerState.CullMode == CullMode.None)
  145. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  146. else if (GraphicsDevice.RasterizerState.CullMode == CullMode.CullCounterClockwiseFace)
  147. GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
  148. else if (GraphicsDevice.RasterizerState.CullMode == CullMode.CullClockwiseFace)
  149. GraphicsDevice.RasterizerState = rsCullNone;
  150. changeState = false;
  151. }
  152. base.Update(gameTime);
  153. }
  154. }
  155. }