// 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 { /// /// 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! /// [Serializable] public class GameSettings { #region Default /// /// Filename for our game settings file. /// const string SettingsFilename = "XnaGraphicEngineSettings.xml"; /// /// Default instance for our game settings. /// #if !XBOX360 [NonSerialized] #endif private static GameSettings defaultInstance = null; /// /// Need saving the game settings file? Only set to true if /// we really changed some game setting here. /// #if !XBOX360 [NonSerialized] #endif private static bool needSave = false; /// /// Default /// /// Game settings public static GameSettings Default { get { return defaultInstance; } // get } // Default #endregion #region Constructor /// /// Create game settings, don't allow public constructor! /// private GameSettings() { } // GameSettings() /// /// Create game settings. This constructor helps us to only load the /// GameSettings once, not again if GameSettings is recreated by /// the Deserialization process. /// /// Load settings static GameSettings() { defaultInstance = new GameSettings(); Load(); } // GameSettings(loadSettings) #endregion #region Finalizer /// /// ~Game settings /// ~GameSettings() { Save(); } // ~GameSettings() #endregion #region Load /// /// Load /// 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 /// /// Save /// 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 /// /// Highscores /// string highscores = ""; /// /// Highscores /// /// String public string Highscores { get { return highscores; } // get set { if (highscores != value) needSave = true; highscores = value; } // set } // Highscores /// /// Player name /// string playerName = ""; /// /// Player name /// /// String public string PlayerName { get { return playerName; } // get set { if (playerName != value) needSave = true; playerName = value; } // set } // PlayerName /// /// Language /// string language = ""; /// /// Language /// /// String public string Language { get { return language; } // get set { if (language != value) needSave = true; language = value; } // set } // Language /// /// BaseGame width /// int resolutionWidth = 0; /// /// BaseGame width /// /// Int public int ResolutionWidth { get { return resolutionWidth; } // get set { if (resolutionWidth != value) needSave = true; resolutionWidth = value; } // set } // ResolutionWidth /// /// BaseGame height /// int resolutionHeight = 0; /// /// BaseGame height /// /// Int public int ResolutionHeight { get { return resolutionHeight; } // get set { if (resolutionHeight != value) needSave = true; resolutionHeight = value; } // set } // ResolutionHeight /// /// Fullscreen /// bool fullscreen = true; /// /// Fullscreen /// /// Bool public bool Fullscreen { get { return fullscreen; } // get set { if (fullscreen != value) needSave = true; fullscreen = value; } // set } // Fullscreen /// /// Performance settings /// int performanceSettings = -1; /// /// Performance settings /// /// Int public int PerformanceSettings { get { return performanceSettings; } // get set { if (performanceSettings != value) needSave = true; performanceSettings = value; } // set } // PerformanceSettings bool postScreenEffects = true; /// /// Post screen effects /// /// Bool public bool PostScreenEffects { get { return postScreenEffects; } // get set { if (postScreenEffects != value) needSave = true; postScreenEffects = value; } // set } // PostScreenEffects bool shadowMapping = true; /// /// ShadowMapping /// /// Bool public bool ShadowMapping { get { return shadowMapping; } // get set { if (shadowMapping != value) needSave = true; shadowMapping = value; } // set } // ShadowMapping bool highDetail = true; /// /// HighDetail /// /// Bool public bool HighDetail { get { return highDetail; } // get set { if (highDetail != value) needSave = true; highDetail = value; } // set } // HighDetail /// /// Sound volume /// float soundVolume = 0.8f; /// /// Sound volume /// /// Float public float SoundVolume { get { return soundVolume; } // get set { if (soundVolume != value) needSave = true; soundVolume = value; } // set } // SoundVolume /// /// Music volume /// float musicVolume = 0.6f; /// /// Music volume /// /// Float public float MusicVolume { get { return musicVolume; } // get set { if (musicVolume != value) needSave = true; musicVolume = value; } // set } // MusicVolume /// /// Music on /// bool musicOn = true; /// /// Music on /// /// bool public bool MusicOn { get { return musicOn; } // get set { musicOn = value; } // set } // MusicOn /// /// waitForVSync /// bool waitForVSync = false; /// /// waitForVSync /// /// bool public bool WaitForVSync { get { return waitForVSync; } // get set { waitForVSync = value; } // set } // WaitForVSync /// /// Controller sensibility /// float controllerSensibility = 1; /// /// Controller sensibility /// /// Float 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; /// /// Move left key /// /// Char public Keys MoveLeftKey { get { return moveLeftKey; } // get set { if (moveLeftKey != value) needSave = true; moveLeftKey = value; } // set } // MoveLeftKey /// /// Move right key /// /// Char public Keys MoveRightKey { get { return moveRightKey; } // get set { if (moveRightKey != value) needSave = true; moveRightKey = value; } // set } // MoveRightKey /// /// Move up key /// /// Char public Keys MoveForwardKey { get { return moveUpKey; } // get set { if (moveUpKey != value) needSave = true; moveUpKey = value; } // set } // MoveForwardKey /// /// Move down key /// /// Char public Keys MoveBackwardKey { get { return moveDownKey; } // get set { if (moveDownKey != value) needSave = true; moveDownKey = value; } // set } // MoveBackwardKey /*obs /// /// RollLeftKey /// /// Char public Keys RollLeftKey { get { return rollLeftKey; } // get set { if (rollLeftKey != value) needSave = true; rollLeftKey = value; } // set } // RollLeftKey /// /// RollRightKey /// /// Char public Keys RollRightKey { get { return rollRightKey; } // get set { if (rollRightKey != value) needSave = true; rollRightKey = value; } // set } // RollRightKey */ #endregion #region Unit Testing #if DEBUG /// /// Test save game settings /// 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