StateObjectGame.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //-----------------------------------------------------------------------------
  2. // Game1.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Audio;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. using Microsoft.Xna.Framework.Input.Touch;
  16. using Microsoft.Xna.Framework.Media;
  17. namespace StateObject
  18. {
  19. /// <summary>
  20. /// This is the main type for your game
  21. /// </summary>
  22. public class StateObjectGame : Game
  23. {
  24. GraphicsDeviceManager graphicsDeviceManager;
  25. BasicEffect basicEffect;
  26. VertexDeclaration vertexDeclaration;
  27. VertexBuffer vertexBuffer;
  28. const int number_of_vertices = 6;
  29. RasterizerState rsCullNone;
  30. RasterizerState rsCullCounterClockwise;
  31. RasterizerState rsCullClockwise;
  32. SpriteBatch spriteBatch;
  33. // SpriteFont and mode tracking
  34. SpriteFont instructionFont;
  35. KeyboardState currentKeyboardState = Keyboard.GetState();
  36. KeyboardState lastKeyboardState = Keyboard.GetState();
  37. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  38. GamePadState lastGamePadState = GamePad.GetState(PlayerIndex.One);
  39. bool changeState = false;
  40. public StateObjectGame()
  41. {
  42. graphicsDeviceManager = new GraphicsDeviceManager(this);
  43. Content.RootDirectory = "Content";
  44. }
  45. /// <summary>
  46. /// Allows the game to perform any initialization it needs to before starting to run.
  47. /// This is where it can query for any required services and load any non-graphic
  48. /// related content. Calling base.Initialize will enumerate through any components
  49. /// and initialize them as well.
  50. /// </summary>
  51. protected override void Initialize()
  52. {
  53. // TODO: Add your initialization logic here
  54. CreateEffect();
  55. CreateVertexBuffer();
  56. rsCullNone = new RasterizerState()
  57. {
  58. CullMode = CullMode.None,
  59. FillMode = FillMode.WireFrame,
  60. MultiSampleAntiAlias = false
  61. };
  62. rsCullCounterClockwise = new RasterizerState()
  63. {
  64. CullMode = CullMode.CullCounterClockwiseFace,
  65. FillMode = FillMode.WireFrame,
  66. MultiSampleAntiAlias = false
  67. };
  68. rsCullClockwise = new RasterizerState()
  69. {
  70. CullMode = CullMode.CullClockwiseFace,
  71. FillMode = FillMode.WireFrame,
  72. MultiSampleAntiAlias = false
  73. };
  74. GraphicsDevice.RasterizerState = rsCullNone;
  75. base.Initialize();
  76. }
  77. /// <summary>
  78. /// LoadContent will be called once per game and is the place to load
  79. /// all of your content.
  80. /// </summary>
  81. protected override void LoadContent()
  82. {
  83. // Load the SpriteFont
  84. instructionFont = Content.Load<SpriteFont>("font");
  85. spriteBatch = new SpriteBatch(GraphicsDevice);
  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. /// <summary>
  96. /// Allows the game to run logic such as updating the world,
  97. /// checking for collisions, gathering input, and playing audio.
  98. /// </summary>
  99. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  100. protected override void Update(GameTime gameTime)
  101. {
  102. currentKeyboardState = Keyboard.GetState();
  103. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  104. // Exit the game from a GamePad
  105. if (currentGamePadState.Buttons.Back == ButtonState.Pressed)
  106. this.Exit();
  107. // Exit the game from a Keyboard
  108. if (currentKeyboardState.IsKeyDown(Keys.Escape))
  109. this.Exit();
  110. // Only set changeState when input is detected
  111. if ((currentGamePadState.Buttons.A == ButtonState.Pressed
  112. && currentGamePadState.Buttons.A != lastGamePadState.Buttons.A)
  113. || (currentKeyboardState.IsKeyDown(Keys.A)
  114. && lastKeyboardState.IsKeyUp(Keys.A)))
  115. {
  116. changeState = true;
  117. }
  118. if (changeState)
  119. {
  120. if (GraphicsDevice.RasterizerState == rsCullNone)
  121. {
  122. GraphicsDevice.RasterizerState = rsCullCounterClockwise;
  123. }
  124. else if (GraphicsDevice.RasterizerState == rsCullCounterClockwise)
  125. {
  126. GraphicsDevice.RasterizerState = rsCullClockwise;
  127. }
  128. else
  129. {
  130. GraphicsDevice.RasterizerState = rsCullNone;
  131. }
  132. changeState = false;
  133. }
  134. lastKeyboardState = currentKeyboardState;
  135. lastGamePadState = currentGamePadState;
  136. base.Update(gameTime);
  137. }
  138. /// <summary>
  139. /// This is called when the game should draw itself.
  140. /// </summary>
  141. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  142. protected override void Draw(GameTime gameTime)
  143. {
  144. GraphicsDevice.Clear(Color.MonoGameOrange);
  145. GraphicsDevice.SetVertexBuffer(vertexBuffer);
  146. EffectPass pass = basicEffect.CurrentTechnique.Passes[0];
  147. if (pass != null)
  148. {
  149. pass.Apply();
  150. GraphicsDevice.DrawPrimitives(
  151. PrimitiveType.TriangleList, // primitive type to draw
  152. 0, // start vertex
  153. 2 // number of primitives to draw
  154. );
  155. }
  156. // Draw instructions and current mode
  157. spriteBatch.Begin();
  158. spriteBatch.DrawString(
  159. instructionFont,
  160. "Change Cull Modes:\n A on the Keyboard or A on the GamePad",
  161. new Vector2(20, 250),
  162. Color.White
  163. );
  164. spriteBatch.DrawString(
  165. instructionFont,
  166. $"Cull Mode:\n {GraphicsDevice.RasterizerState.CullMode}",
  167. new Vector2(20, 325),
  168. Color.Yellow
  169. );
  170. spriteBatch.End();
  171. base.Draw(gameTime);
  172. }
  173. private void CreateEffect()
  174. {
  175. basicEffect = new BasicEffect(GraphicsDevice);
  176. }
  177. private void CreateVertexBuffer()
  178. {
  179. vertexDeclaration = new VertexDeclaration(new VertexElement[1]
  180. {
  181. new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0)
  182. }
  183. );
  184. vertexBuffer = new VertexBuffer(
  185. GraphicsDevice,
  186. vertexDeclaration,
  187. number_of_vertices,
  188. BufferUsage.None
  189. );
  190. Vector3[] vertices = new Vector3[number_of_vertices];
  191. vertices[0] = new Vector3(-1, 0, 0); // cw
  192. vertices[1] = new Vector3(0, 1, 0);
  193. vertices[2] = new Vector3(0, 0, 0);
  194. vertices[3] = new Vector3(0, 0, 0); // ccw
  195. vertices[4] = new Vector3(1, 0, 0);
  196. vertices[5] = new Vector3(0, 1, 0);
  197. vertexBuffer.SetData(vertices);
  198. }
  199. }
  200. }