topical media & game development

talk show tell print

game-xna-intro-XnaGraphicEngineChapter5-YourGame.cs / cs



  // Project: XnaGraphicEngineVs2005, File: YourGame.cs
  // Namespace: XnaGraphicEngine, Class: YourGame
  // Path: C:\code\XnaBook\XnaGraphicEngine, Author: Abi
  // Code lines: 323, Size of file: 8,65 KB
  // Creation date: 21.11.2006 03:56
  // Last modified: 27.11.2006 07:09
  // Generated with Commenter by abi.exDream.com
  
  #region Using directives
  using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Audio;
  using Microsoft.Xna.Framework.Content;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Input;
  using Microsoft.Xna.Framework.Storage;
  using System;
  using System.Collections.Generic;
  using XnaGraphicEngine.Game;
  using XnaGraphicEngine.Graphics;
  using Texture = XnaGraphicEngine.Graphics.Texture;
  using Model = XnaGraphicEngine.Graphics.Model;
  using XnaGraphicEngine.Helpers;
  #endregion
  
  namespace XnaGraphicEngine
  {
  
<summary> YourGame test class, very similar to the MyOwnGraphicEngine project from the Rocket Commander tutorials, but this time in XNA. While this class is pretty easy the graphic engine we build in the background is quite powerful and will enable us to create cool games very quickly now as we can see in a few chapters :) </summary> public class YourGame : BaseGame { #region Constructor <summary> Create your game </summary> public YourGame() { // Init simple camera SetCamera(new SimpleCamera(this));

                          // And Fps counter
                          this.Components.Add(new FpsCounter(this));
                  } // YourGame()
  
                  GameComponent currentCamera = null;
  
<summary> Set camera </summary> <param name="camera">New camera</param> public void SetCamera(GameComponent camera) { // Remove existing cameras if (currentCamera != null) this.Components.Remove(currentCamera); // Add new camera! this.Components.Add(camera); currentCamera = camera; } #endregion

                  #region Initialize
  
<summary> Allows the game to perform any initialization it needs. </summary> protected override void Initialize() { base.Initialize();

                          // Make sure mouse is centered
                          Input.Update();
                          Input.MousePos = new Point(
                                  Window.ClientBounds.X + width / 2,
                                  Window.ClientBounds.Y + height / 2);
                          Input.Update();
  
                          // Nothing else in here yet
                  } // Initialize()
                  #endregion
  
                  #region Update
  
<summary> Allows the game to run logic such as updating the world, checking for collisions, gathering input and playing audio. </summary> <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();

                          // TODO: Add your update logic here
  
                          base.Update(gameTime);
                  } // Update(gameTime)
                  #endregion
  
                  #region Draw
  
<summary> This is called when the game should draw itself. </summary> <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { ClearBackground();

                          // Not a unit test?
                          if (this.GetType() == typeof(YourGame))
                                  TextureFont.WriteText(30, 30, "StartGame does nothing, "+
                                          "please start one of the unit tests instead!");
  
                          base.Draw(gameTime);
                  } // Draw(gameTime)
                  #endregion
  
                  #region Start Game
  
<summary> Start game </summary> public static void StartGame() { using (YourGame game = new YourGame()) { game.Run(); } // using (game) } // StartGame() #endregion #region Unit tests if DEBUG #region TestRenderOurNewGraphicEngine <summary> Test render our new graphic engine </summary> public static void TestRenderOurNewGraphicEngine() { Texture backgroundTexture = null; Model rocketModel = null;

                          TestGame.Start("TestRenderOurNewGraphicEngine",
                                  delegate
                                  {
                                          // Load background and rocket
                                          backgroundTexture = new Texture("SpaceBackground");
                                          rocketModel = new Model("Rocket");
                                          TestGame.game.SetCamera(new RotationCamera(TestGame.game));
                                  },
                                  delegate
                                  {
                                          // Show background
                                          backgroundTexture.RenderOnScreen(
                                                  BaseGame.ResolutionRect);
                                          SpriteHelper.DrawSprites(width, height);
  
                                          // Render model in center
                                          BaseGame.Device.RenderState.DepthBufferEnable = true;
                                          rocketModel.Render(Matrix.CreateScale(10));
  
                                          // Draw 3d line
                                          BaseGame.DrawLine(
                                                  new Vector3(-100, 0, 0), new Vector3(+100, 0, 0), Color.Red);
  
                                          // Draw save region box for the Xbox 360 (support for older monitors)
                                          Point upperLeft = new Point(width / 15, height / 15);
                                          Point upperRight = new Point(width * 14 / 15, height / 15);
                                          Point lowerRight = new Point(width * 14 / 15, height * 14 / 15);
                                          Point lowerLeft = new Point(width / 15, height * 14 / 15);
                                          BaseGame.DrawLine(upperLeft, upperRight);
                                          BaseGame.DrawLine(upperRight, lowerRight);
                                          BaseGame.DrawLine(lowerRight, lowerLeft);
                                          BaseGame.DrawLine(lowerLeft, upperLeft);
  
                                          // And finally some text
                                          TextureFont.WriteText(upperLeft.X + 15, upperLeft.Y + 15,
                                                  "TestRenderOurNewGraphicEngine");
                                  });
                  } // TestRenderOurNewGraphicEngine()
                  #endregion
  endif
                  #endregion
          } // class YourGame
  } // namespace XnaGraphicEngine
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.