Game1.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. using TexturedQuad;
  21. namespace TexturedQuadWindows
  22. {
  23. /// <summary>
  24. /// This is the main type for your game
  25. /// </summary>
  26. public class Game1 : Microsoft.Xna.Framework.Game
  27. {
  28. GraphicsDeviceManager graphics;
  29. SpriteBatch spriteBatch;
  30. public Game1()
  31. {
  32. graphics = new GraphicsDeviceManager(this);
  33. Content.RootDirectory = "Content";
  34. }
  35. Quad quad;
  36. VertexDeclaration vertexDeclaration;
  37. Matrix View, Projection;
  38. protected override void Initialize()
  39. {
  40. quad = new Quad(Vector3.Zero, Vector3.Backward, Vector3.Up, 1, 1);
  41. View = Matrix.CreateLookAt(new Vector3(0, 0, 2), Vector3.Zero,
  42. Vector3.Up);
  43. Projection = Matrix.CreatePerspectiveFieldOfView(
  44. MathHelper.PiOver4, 4.0f / 3.0f, 1, 500);
  45. base.Initialize();
  46. }
  47. /// <summary>
  48. /// LoadContent will be called once per game and is the place to load
  49. /// all of your content.
  50. /// </summary>
  51. Texture2D texture;
  52. BasicEffect quadEffect;
  53. protected override void LoadContent()
  54. {
  55. // Create a new SpriteBatch, which can be used to draw textures.
  56. spriteBatch = new SpriteBatch(GraphicsDevice);
  57. texture = Content.Load<Texture2D>("Glass");
  58. quadEffect = new BasicEffect(graphics.GraphicsDevice);
  59. // We still do not have lighting implemented in the shaders
  60. //quadEffect.EnableDefaultLighting();
  61. quadEffect.World = Matrix.Identity;
  62. quadEffect.View = View;
  63. quadEffect.Projection = Projection;
  64. quadEffect.TextureEnabled = true;
  65. quadEffect.Texture = texture;
  66. vertexDeclaration = new VertexDeclaration(new VertexElement[]
  67. {
  68. new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
  69. new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
  70. new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
  71. }
  72. );
  73. }
  74. /// <summary>
  75. /// Allows the game to run logic such as updating the world,
  76. /// checking for collisions, gathering input, and playing audio.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Update(GameTime gameTime)
  80. {
  81. // Allows the game to exit
  82. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  83. this.Exit();
  84. #if WINDOWS || MONOMAC
  85. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  86. this.Exit();
  87. #endif
  88. // TODO: Add your update logic here
  89. base.Update(gameTime);
  90. }
  91. /// <summary>
  92. /// This is called when the game should draw itself.
  93. /// </summary>
  94. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  95. protected override void Draw(GameTime gameTime)
  96. {
  97. GraphicsDevice.Clear(Color.CornflowerBlue);
  98. foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
  99. {
  100. pass.Apply();
  101. GraphicsDevice.DrawUserIndexedPrimitives
  102. <VertexPositionNormalTexture>(
  103. PrimitiveType.TriangleList,
  104. quad.Vertices, 0, 4,
  105. quad.Indexes, 0, 2);
  106. }
  107. base.Draw(gameTime);
  108. }
  109. }
  110. }