topical media & game development

talk show tell print

game-xna-intro-XnaShooterUIProject-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;
  using XnaGraphicEngine.GameScreens;
  using XnaGraphicEngine.Sounds;
  using XnaGraphicEngine.Properties;
  #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 Variables <summary> Game screens stack. We can easily add and remove game screens and they follow the game logic automatically. Very cool. </summary> private Stack<IGameScreen> gameScreens = new Stack<IGameScreen>();

  
<summary> Load the main menu and mouse textures at game start, this way we have access in all game screens and don't ever have to reload any of these textures. </summary> public Texture mainMenuTexture = null, mouseCursorTexture = null;

  
<summary> Also preload the in game textures for all in game controls and stuff. Note: These Hud textures are saved in png format to save disk space. Light effect texture is used to show items from a far distance. </summary> public Texture hudTopTexture = null, hudBottomTexture = null; #endregion

                  #region Constructor
  
<summary> Create your game </summary> public YourGame() { // Disable mouse, we use our own mouse texture in the menu // and don't use any mouse cursor in the game anyway. this.IsMouseVisible = false; } // YourGame() #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();
  
                          // Load menu textures
                          mainMenuTexture = new Texture("MainMenu");
                          mouseCursorTexture = new Texture("MouseCursor");
                          hudTopTexture = new Texture("HudTop");
                          hudBottomTexture = new Texture("HudBottom");
  
                          // Create main menu screen
                          gameScreens.Push(new MainMenu());
                          // Start game
                          //gameScreens.Push(new Mission());
                          //inGame = true;
  
                          // Start music
                          Sound.StartMusic();
                  } // Initialize()
                  #endregion
  
                  #region Toggle music on/off
  
<summary> Toggle music on off </summary> public void ToggleMusicOnOff() { if (GameSettings.Default.MusicOn) Sound.StopMusic(); else Sound.StartMusic(); } // ToggleMusicOnOff() #endregion

                  #region Add game screen
                  static bool inGame = false;
  
<summary> In game </summary> <returns>Bool</returns> public static bool InGame { get { return inGame; } // get } // InGame

  
<summary> Add game screen, which will be used until we quit it or add another game screen on top of it. </summary> <param name="newGameScreen">New game screen</param> public void AddGameScreen(IGameScreen newGameScreen) { gameScreens.Push(newGameScreen);

                          inGame = newGameScreen.GetType() == typeof(Mission);
                  } // AddGameScreen(newGameScreen)
                  #endregion
  
                  #region Remove current game screen
  
<summary> Remove current game screen </summary> public void RemoveCurrentGameScreen() { gameScreens.Pop();

                          inGame = gameScreens.Count > 0 &&
                                  gameScreens.Peek().GetType() == typeof(Mission);
                  } // RemoveCurrentGameScreen()
                  #endregion
  
                  #region Render menu background
  
<summary> Render menu background </summary> public void RenderMenuBackground() { // Make sure alpha blending is enabled. BaseGame.EnableAlphaBlending(); mainMenuTexture.RenderOnScreen( BaseGame.ResolutionRect, new Rectangle(0, 0, 1024, 768)); } // RenderMenuBackground() #endregion

                  #region Render game background
  
<summary> Render game background </summary> public static void RenderGameBackground() { //TODO } // RenderGameBackground() #endregion

                  #region Render button
  
<summary> Render button </summary> <param name="buttonType">Button type</param> <param name="rect">Rectangle</param> public bool RenderMenuButton( MenuButton buttonType, Point pos) { // Calc screen rect for rendering (recalculate relative screen position // from 1024x768 to actual screen resolution, just in case ^^). Rectangle rect = new Rectangle( pos.X * BaseGame.Width / 1024, pos.Y * BaseGame.Height / 768, 200 * BaseGame.Width / 1024, 77 * BaseGame.Height / 768);

                          // Is button highlighted?
                          Rectangle innerRect = new Rectangle(
                                  rect.X, rect.Y + rect.Height / 5,
                                  rect.Width, rect.Height * 3 / 5);
                          bool highlight = Input.MouseInBox(
                                  //rect);
                                  // Just use inner rect
                                  innerRect);
  
                          // Was not highlighted last frame?
                          if (highlight &&
                                  Input.MouseWasNotInRectLastFrame(innerRect))
                                  Sound.Play(Sound.Sounds.Highlight);
  
                          // See MainMenu.dds for pixel locations
                          int buttonNum = (int)buttonType;
  
                          // Correct last 2 button numbers (exit and back)
                          //if (buttonNum >= (int)MenuButton.Exit)
                          //        buttonNum -= 2;
  
                          Rectangle pixelRect = new Rectangle(3 + 204 * buttonNum,
                                  840 + 80 * (highlight ? 1 : 0), 200, 77);
  
                          // Render
                          mainMenuTexture.RenderOnScreen(rect, pixelRect);
  
                          // Play click sound if button was just clicked
                          bool ret =
                                  (Input.MouseLeftButtonJustPressed ||
                                  Input.GamePadAJustPressed) &&
                                  this.IsActive &&
                                  highlight;
  
                          if (buttonType == MenuButton.Back &&
                                  Input.GamePadBackJustPressed)
                                  ret = true;
                          if (buttonType == MenuButton.Missions &&
                                  Input.GamePadStartPressed)
                                  ret = true;
  
                          if (ret == true)
                                  Sound.Play(Sound.Sounds.Click);
  
                          // Return true if button was pressed, false otherwise
                          return ret;
                  } // RenderButton(buttonType, rect)
                  #endregion
  
                  #region Render mouse cursor
  
<summary> Render mouse cursor </summary> public void RenderMouseCursor() { if !XBOX360 // We got 4 animation steps, rotate them by the current time int mouseAnimationStep = (int)(BaseGame.TotalTimeMs / 100) % 4;

                          // And render mouse on screen.
                          mouseCursorTexture.RenderOnScreen(
                                  new Rectangle(Input.MousePos.X, Input.MousePos.Y, 60 * 2 / 3, 64 * 2 / 3),
                                  new Rectangle(64 * mouseAnimationStep, 0, 60, 64));
  
                          // Draw all sprites (just the mouse cursor)
                          SpriteHelper.DrawSprites(width, height);
  endif
                  } // RenderMouseCursor()
                  #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) { // If that game screen should be quitted, remove it from stack! if (gameScreens.Count > 0 && gameScreens.Peek().Quit) RemoveCurrentGameScreen();

                          // If no more game screens are left, it is time to quit!
                          if (gameScreens.Count == 0)
                          {
  if DEBUG
                                  // Don't exit if this is just a unit test
                                  if (this.GetType() != typeof(TestGame))
  endif
                                          Exit();
                          } // if
  
                          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();

                          // Start post screen glow shader, will be shown in BaseGame.Draw
                          BaseGame.GlowShader.Start();
  
                          try
                          {
                                  // Execute the game screen on top.
                                  if (gameScreens.Count > 0)
                                          gameScreens.Peek().Run(this);
                          } // try
                          catch (Exception ex)
                          {
                                  Log.Write("Failed to execute " + gameScreens.Peek().Name +
                                          "\nError: " + ex.ToString());
                          } // catch
  
                          base.Draw(gameTime);
  
                          // Show mouse cursor (in all modes except in the game)
                          if (inGame == false &&
                                  gameScreens.Count > 0)
                                  RenderMouseCursor();
                          else
                          {
                                  // In game always center mouse
                                  Input.CenterMouse();
                          } // else
  
                          // Add scene glow on top of everything (2d and 3d!) 
                          glowShader.Show();
                  } // Draw(gameTime)
                  #endregion
  
                  #region Start Game
  
<summary> Start game </summary> public static void StartGame() { using (YourGame game = new YourGame()) { game.Run(); } // using (game)

                          GameSettings.Save();
                  } // 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");
                                  },
                                  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.