topical media & game development

talk show tell print

game-xna-intro-XnaShooterUIProject-Properties-GameSettings.cs / cs



  // Project: XnaGraphicEngine, File: GameSettings.cs
  // Namespace: XnaGraphicEngine.Properties, Class: GameSettings
  // Path: C:\code\XnaBook\XnaGraphicEngine\Properties, Author: abi
  // Code lines: 546, Size of file: 11,61 KB
  // Creation date: 07.12.2006 18:22
  // Last modified: 07.12.2006 20:13
  // Generated with Commenter by abi.exDream.com
  
  #region Using directives
  using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Input;
  using Microsoft.Xna.Framework.Storage;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Xml.Serialization;
  using System.Threading;
  using XnaGraphicEngine.Helpers;
  #endregion
  
  namespace XnaGraphicEngine.Properties
  {
  
<summary> Game settings, stored in a custom xml file. The reason for this is we want to be able to store our game data on the Xbox360 too. On the PC we could just use a Settings/config file and have all the code autogenerated for us, but this way it works both on PC and Xbox. Note: The default instance for the game settings is in this class, this way we get the same behaviour as for normal Settings files! </summary> [Serializable] public class GameSettings { #region Default <summary> Filename for our game settings file. </summary> const string SettingsFilename = "XnaGraphicEngineSettings.xml";

  
<summary> Default instance for our game settings. </summary> if !XBOX360 [NonSerialized] endif private static GameSettings defaultInstance = null;

  
<summary> Need saving the game settings file? Only set to true if we really changed some game setting here. </summary> if !XBOX360 [NonSerialized] endif private static bool needSave = false;

  
<summary> Default </summary> <returns>Game settings</returns> public static GameSettings Default { get { return defaultInstance; } // get } // Default #endregion

                  #region Constructor
  
<summary> Create game settings, don't allow public constructor! </summary> private GameSettings() { } // GameSettings()

  
<summary> Create game settings. This constructor helps us to only load the GameSettings once, not again if GameSettings is recreated by the Deserialization process. </summary> <param name="loadSettings">Load settings</param> static GameSettings() { defaultInstance = new GameSettings(); Load(); } // GameSettings(loadSettings) #endregion

                  #region Finalizer
  
<summary> ~Game settings </summary> ~GameSettings() { Save(); } // ~GameSettings() #endregion

                  #region Load
  
<summary> Load </summary> public static void Load() { needSave = false;

                          FileStream file =
                                  FileHelper.LoadGameContentFile(SettingsFilename);
                          if (file == null)
                                  return;
  
                          // If the file is empty, just create a new file with the default
                          // settings.
                          if (file.Length == 0)
                          {
                                  // Close the file
                                  file.Close();
  
                                  // But first check if there is maybe a file in the game directory
                                  // to load the default game settings from.
                                  file = FileHelper.LoadGameContentFile(SettingsFilename);
                                  if (file != null)
                                  {
                                          // Load everything into this class
                                          GameSettings loadedGameSettings =
                                                  (GameSettings)new XmlSerializer(typeof(GameSettings)).
                                                  Deserialize(file);
                                          if (loadedGameSettings != null)
                                                  defaultInstance = loadedGameSettings;
  
                                          // Close the file
                                          file.Close();
                                  } // if (file)
  
                                  // Save user settings file
                                  needSave = true;
                                  Save();
                          } // if (file.Length)
                          else
                          {
                                  // Else load everything into this class with help of the
                                  // XmlSerializer.
                                  GameSettings loadedGameSettings =
                                          (GameSettings)new XmlSerializer(typeof(GameSettings)).
                                          Deserialize(file);
                                  if (loadedGameSettings != null)
                                          defaultInstance = loadedGameSettings;
  
                                  // Close the file
                                  file.Close();
                          } // else
                  } // Load()
                  #endregion
  
                  #region Save
  
<summary> Save </summary> public static void Save() { // No need to save if everything is up to date. if (needSave == false) return;

                          needSave = false;
                          FileStream file = FileHelper.SaveGameContentFile(SettingsFilename);
  
                          // Save everything in this class with help of the XmlSerializer.
                          new XmlSerializer(typeof(GameSettings)).
                                  Serialize(file, defaultInstance);
  
                          // Close the file
                          file.Close();
                  } // Save()
                  #endregion
  
                  #region Setting variables with properties
  
<summary> Highscores </summary> string highscores = ""; <summary> Highscores </summary> <returns>String</returns> public string Highscores { get { return highscores; } // get set { if (highscores != value) needSave = true; highscores = value; } // set } // Highscores

  
<summary> Player name </summary> string playerName = ""; <summary> Player name </summary> <returns>String</returns> public string PlayerName { get { return playerName; } // get set { if (playerName != value) needSave = true; playerName = value; } // set } // PlayerName

  
<summary> Language </summary> string language = ""; <summary> Language </summary> <returns>String</returns> public string Language { get { return language; } // get set { if (language != value) needSave = true; language = value; } // set } // Language

  
<summary> BaseGame width </summary> int resolutionWidth = 0; <summary> BaseGame width </summary> <returns>Int</returns> public int ResolutionWidth { get { return resolutionWidth; } // get set { if (resolutionWidth != value) needSave = true; resolutionWidth = value; } // set } // ResolutionWidth

  
<summary> BaseGame height </summary> int resolutionHeight = 0; <summary> BaseGame height </summary> <returns>Int</returns> public int ResolutionHeight { get { return resolutionHeight; } // get set { if (resolutionHeight != value) needSave = true; resolutionHeight = value; } // set } // ResolutionHeight

  
<summary> Fullscreen </summary> bool fullscreen = true; <summary> Fullscreen </summary> <returns>Bool</returns> public bool Fullscreen { get { return fullscreen; } // get set { if (fullscreen != value) needSave = true; fullscreen = value; } // set } // Fullscreen

  
<summary> Performance settings </summary> int performanceSettings = -1; <summary> Performance settings </summary> <returns>Int</returns> public int PerformanceSettings { get { return performanceSettings; } // get set { if (performanceSettings != value) needSave = true; performanceSettings = value; } // set } // PerformanceSettings

                  bool postScreenEffects = true;
  
<summary> Post screen effects </summary> <returns>Bool</returns> public bool PostScreenEffects { get { return postScreenEffects; } // get set { if (postScreenEffects != value) needSave = true; postScreenEffects = value; } // set } // PostScreenEffects

                  bool shadowMapping = true;
  
<summary> ShadowMapping </summary> <returns>Bool</returns> public bool ShadowMapping { get { return shadowMapping; } // get set { if (shadowMapping != value) needSave = true; shadowMapping = value; } // set } // ShadowMapping

                  bool highDetail = true;
  
<summary> HighDetail </summary> <returns>Bool</returns> public bool HighDetail { get { return highDetail; } // get set { if (highDetail != value) needSave = true; highDetail = value; } // set } // HighDetail

  
<summary> Sound volume </summary> float soundVolume = 0.8f; <summary> Sound volume </summary> <returns>Float</returns> public float SoundVolume { get { return soundVolume; } // get set { if (soundVolume != value) needSave = true; soundVolume = value; } // set } // SoundVolume

  
<summary> Music volume </summary> float musicVolume = 0.6f; <summary> Music volume </summary> <returns>Float</returns> public float MusicVolume { get { return musicVolume; } // get set { if (musicVolume != value) needSave = true; musicVolume = value; } // set } // MusicVolume

  
<summary> Music on </summary> bool musicOn = true; <summary> Music on </summary> <returns>bool</returns> public bool MusicOn { get { return musicOn; } // get set { musicOn = value; } // set } // MusicOn

  
<summary> waitForVSync </summary> bool waitForVSync = false; <summary> waitForVSync </summary> <returns>bool</returns> public bool WaitForVSync { get { return waitForVSync; } // get set { waitForVSync = value; } // set } // WaitForVSync

  
<summary> Controller sensibility </summary> float controllerSensibility = 1; <summary> Controller sensibility </summary> <returns>Float</returns> public float ControllerSensibility { get { return controllerSensibility; } // get set { if (controllerSensibility != value) needSave = true; controllerSensibility = value; } // set } // ControllerSensibility

                  Keys moveLeftKey = Keys.A,
                          moveRightKey = Keys.D,
                          moveUpKey = Keys.W,
                          moveDownKey = Keys.S;//,
                          //rollLeftKey = Keys.Q,
                          //rollRightKey = Keys.E;
  
  
<summary> Move left key </summary> <returns>Char</returns> public Keys MoveLeftKey { get { return moveLeftKey; } // get set { if (moveLeftKey != value) needSave = true; moveLeftKey = value; } // set } // MoveLeftKey

  
<summary> Move right key </summary> <returns>Char</returns> public Keys MoveRightKey { get { return moveRightKey; } // get set { if (moveRightKey != value) needSave = true; moveRightKey = value; } // set } // MoveRightKey

  
<summary> Move up key </summary> <returns>Char</returns> public Keys MoveForwardKey { get { return moveUpKey; } // get set { if (moveUpKey != value) needSave = true; moveUpKey = value; } // set } // MoveForwardKey

  
<summary> Move down key </summary> <returns>Char</returns> public Keys MoveBackwardKey { get { return moveDownKey; } // get set { if (moveDownKey != value) needSave = true; moveDownKey = value; } // set } // MoveBackwardKey

                  /*obs
  
<summary> RollLeftKey </summary> <returns>Char</returns> public Keys RollLeftKey { get { return rollLeftKey; } // get set { if (rollLeftKey != value) needSave = true; rollLeftKey = value; } // set } // RollLeftKey

  
<summary> RollRightKey </summary> <returns>Char</returns> public Keys RollRightKey { get { return rollRightKey; } // get set { if (rollRightKey != value) needSave = true; rollRightKey = value; } // set } // RollRightKey

  
                  #endregion
  
                  #region Unit Testing
  if DEBUG
  
<summary> Test save game settings </summary> public static void TestSaveGameSettings() { // Just save some data for the highscores ^^ GameSettings.Default.Highscores = "TestSaveGameSettings"; // And save, on the Xbox360 we should get a dialog asking us // where we want to save our savegame file. GameSettings.Save(); } // TestSaveGameSettings() endif #endregion } // class GameSettings } // namespace XnaGraphicEngine.Properties


(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.
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-2780434-1"; urchinTracker(); </script>