CustomModelSampleGame.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CustomModelSampleGame.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace CustomModelSample
  16. {
  17. /// <summary>
  18. /// Sample showing how to use a custom class to
  19. /// replace the built-in XNA Framework Model type.
  20. /// </summary>
  21. public class CustomModelSampleGame : Microsoft.Xna.Framework.Game
  22. {
  23. #region Fields
  24. GraphicsDeviceManager graphics;
  25. CustomModel model;
  26. Matrix world;
  27. Matrix view;
  28. Matrix projection;
  29. #endregion
  30. #region Initialization
  31. public CustomModelSampleGame()
  32. {
  33. graphics = new GraphicsDeviceManager(this);
  34. Content.RootDirectory = "Content";
  35. }
  36. /// <summary>
  37. /// Load your graphics content.
  38. /// </summary>
  39. protected override void LoadContent()
  40. {
  41. model = Content.Load<CustomModel>("tank");
  42. // Calculate camera view and projection matrices.
  43. view = Matrix.CreateLookAt(new Vector3(1000, 500, 0),
  44. new Vector3(0, 150, 0), Vector3.Up);
  45. projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  46. GraphicsDevice.Viewport.AspectRatio, 10, 10000);
  47. }
  48. #endregion
  49. #region Update and Draw
  50. /// <summary>
  51. /// Allows the game to run logic.
  52. /// </summary>
  53. protected override void Update(GameTime gameTime)
  54. {
  55. HandleInput();
  56. // Update the world transform to make the model rotate.
  57. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  58. world = Matrix.CreateRotationY(time * 0.1f);
  59. base.Update(gameTime);
  60. }
  61. /// <summary>
  62. /// This is called when the game should draw itself.
  63. /// </summary>
  64. protected override void Draw(GameTime gameTime)
  65. {
  66. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  67. model.Draw(world, view, projection);
  68. base.Draw(gameTime);
  69. }
  70. #endregion
  71. #region Handle Input
  72. /// <summary>
  73. /// Handles input for quitting the game.
  74. /// </summary>
  75. private void HandleInput()
  76. {
  77. KeyboardState currentKeyboardState = Keyboard.GetState();
  78. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  79. // Check for exit.
  80. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  81. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  82. {
  83. Exit();
  84. }
  85. }
  86. #endregion
  87. }
  88. #region Entry Point
  89. /// <summary>
  90. /// The main entry point for the application.
  91. /// </summary>
  92. static class Program
  93. {
  94. static void Main()
  95. {
  96. using (CustomModelSampleGame game = new CustomModelSampleGame())
  97. {
  98. game.Run();
  99. }
  100. }
  101. }
  102. #endregion
  103. }