| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- //-----------------------------------------------------------------------------
- // GameSettings.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml.Serialization;
- using RacingGame.Helpers;
- using System.Threading;
- #if NETFX_CORE
- using Serializable = System.Runtime.Serialization.DataContractAttribute;
- #endif
- namespace RacingGame.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
- {
- /// <summary>
- /// Filename for our game settings file.
- /// </summary>
- const string SettingsFilename = "RacingGameSettings.xml";
- /// <summary>
- /// Default instance for our game settings.
- /// </summary>
- private static GameSettings defaultInstance = new GameSettings();
- /// <summary>
- /// Need saving the game settings file? Only set to true if
- /// we really changed some game setting here.
- /// </summary>
- private static bool needSave = false;
- /// <summary>
- /// Default
- /// </summary>
- /// <returns>Game settings</returns>
- public static GameSettings Default
- {
- get
- {
- return defaultInstance;
- }
- }
- /// <summary>
- /// Create game settings, don't allow public constructor!
- /// </summary>
- private 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>
- public static void Initialize()
- {
- Load();
- }
- /// <summary>
- /// Load
- /// </summary>
- public static void Load()
- {
- bool saveImmediately = false;
- needSave = false;
- FileHelper.StorageContainerMRE.WaitOne();
- FileHelper.StorageContainerMRE.Reset();
- try
- {
- //TODO: Use Nick Gravlyn's easy storage?
- /*
- StorageDevice storageDevice = FileHelper.XnaUserDevice;
- if ((storageDevice != null) && storageDevice.IsConnected)
- {
- IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);
- async.AsyncWaitHandle.WaitOne();
- using (StorageContainer container =
- storageDevice.EndOpenContainer(async))
- {
- async.AsyncWaitHandle.Close();
- if (container.FileExists(SettingsFilename))
- {
- using (Stream file = container.OpenFile(SettingsFilename,
- FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- if (file.Length > 0)
- {
- GameSettings loadedGameSettings =
- (GameSettings)new XmlSerializer(
- typeof(GameSettings)).Deserialize(file);
- if (loadedGameSettings != null)
- defaultInstance = loadedGameSettings;
- }
- else
- {
- // If the file is empty, just create a new file with the
- // default settings.
- needSave = true;
- saveImmediately = true;
- }
- }
- }
- else
- {
- // Create new file after exiting
- needSave = true;
- }
- }
- }*/
- }
- catch (Exception exc)
- {
- System.Diagnostics.Debug.WriteLine("Settings Load Failure: " + exc.ToString());
- }
- FileHelper.StorageContainerMRE.Set();
- if (saveImmediately)
- {
- Save();
- }
- }
- /// <summary>
- /// Save
- /// </summary>
- public static void Save()
- {
- // No need to save if everything is up to date.
- if (needSave == false)
- return;
- needSave = false;
- FileHelper.StorageContainerMRE.WaitOne();
- FileHelper.StorageContainerMRE.Reset();
- // Open a storage container
- try
- {
- /*
- StorageDevice storageDevice = FileHelper.XnaUserDevice;
- if ((storageDevice != null) && storageDevice.IsConnected)
- {
- IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);
- async.AsyncWaitHandle.WaitOne();
- using (StorageContainer container = storageDevice.EndOpenContainer(async))
- {
- async.AsyncWaitHandle.Close();
- using (Stream file = container.CreateFile(SettingsFilename))
- {
- // Save everything in this class with help of the XmlSerializer.
- new XmlSerializer(typeof(GameSettings)).
- Serialize(file, defaultInstance);
- }
- }
- }*/
- }
- catch (Exception exc)
- {
- System.Diagnostics.Debug.WriteLine("Settings Load Failure: " + exc.ToString());
- }
- FileHelper.StorageContainerMRE.Set();
- }
- /// <summary>
- /// Sets all of the graphical settings to their minimum possible
- /// values and saves the changes.
- /// </summary>
- public static void SetMinimumGraphics()
- {
- GameSettings.Default.ResolutionWidth = GameSettings.MinimumResolutionWidth;
- GameSettings.Default.ResolutionHeight = GameSettings.MinimumResolutionHeight;
- GameSettings.Default.ShadowMapping = false;
- GameSettings.Default.HighDetail = false;
- GameSettings.Default.PostScreenEffects = false;
- GameSettings.Save();
- }
- /// <summary>
- /// Highscores
- /// </summary>
- string highscores = "";
- /// <summary>
- /// Highscores
- /// </summary>
- /// <returns>String</returns>
- public string Highscores
- {
- get
- {
- return highscores;
- }
- set
- {
- if (highscores != value)
- needSave = true;
- highscores = value;
- }
- }
- /// <summary>
- /// Player name
- /// </summary>
- string playerName = "Player";
- /// <summary>
- /// Player name
- /// </summary>
- /// <returns>String</returns>
- public string PlayerName
- {
- get
- {
- return playerName;
- }
- set
- {
- if (playerName != value)
- needSave = true;
- playerName = value;
- }
- }
- public const int MinimumResolutionWidth = 640;
- /// <summary>
- /// Resolution width
- /// </summary>
- int resolutionWidth = 0;
- /// <summary>
- /// Resolution width
- /// </summary>
- /// <returns>Int</returns>
- public int ResolutionWidth
- {
- get
- {
- return resolutionWidth;
- }
- set
- {
- if (resolutionWidth != value)
- needSave = true;
- resolutionWidth = value;
- }
- }
- public const int MinimumResolutionHeight = 480;
- /// <summary>
- /// Resolution height
- /// </summary>
- int resolutionHeight = 0;
- /// <summary>
- /// Resolution height
- /// </summary>
- /// <returns>Int</returns>
- public int ResolutionHeight
- {
- get
- {
- return resolutionHeight;
- }
- set
- {
- if (resolutionHeight != value)
- needSave = true;
- resolutionHeight = value;
- }
- }
- /// <summary>
- /// Fullscreen
- /// </summary>
- bool fullscreen = true;
- /// <summary>
- /// Fullscreen
- /// </summary>
- /// <returns>Bool</returns>
- public bool Fullscreen
- {
- get
- {
- return fullscreen;
- }
- set
- {
- if (fullscreen != value)
- needSave = true;
- fullscreen = value;
- }
- }
- bool postScreenEffects = true;
- /// <summary>
- /// Post screen effects
- /// </summary>
- /// <returns>Bool</returns>
- public bool PostScreenEffects
- {
- get
- {
- return postScreenEffects;
- }
- set
- {
- if (postScreenEffects != value)
- needSave = true;
- postScreenEffects = value;
- }
- }
- bool shadowMapping = true;
- /// <summary>
- /// ShadowMapping
- /// </summary>
- /// <returns>Bool</returns>
- public bool ShadowMapping
- {
- get
- {
- return shadowMapping;
- }
- set
- {
- if (shadowMapping != value)
- needSave = true;
- shadowMapping = value;
- }
- }
- bool highDetail = true;
- /// <summary>
- /// HighDetail
- /// </summary>
- /// <returns>Bool</returns>
- public bool HighDetail
- {
- get
- {
- return highDetail;
- }
- set
- {
- if (highDetail != value)
- needSave = true;
- highDetail = value;
- }
- }
- /// <summary>
- /// Sound volume
- /// </summary>
- float soundVolume = 0.8f;
- /// <summary>
- /// Sound volume
- /// </summary>
- /// <returns>Float</returns>
- public float SoundVolume
- {
- get
- {
- return soundVolume;
- }
- set
- {
- if (soundVolume != value)
- needSave = true;
- soundVolume = value;
- }
- }
- /// <summary>
- /// Music volume
- /// </summary>
- float musicVolume = 0.6f;
- /// <summary>
- /// Music volume
- /// </summary>
- /// <returns>Float</returns>
- public float MusicVolume
- {
- get
- {
- return musicVolume;
- }
- set
- {
- if (musicVolume != value)
- needSave = true;
- musicVolume = value;
- }
- }
- /// <summary>
- /// Controller sensitivity
- /// </summary>
- float controllerSensitivity = 0.5f;
- /// <summary>
- /// Controller sensitivity
- /// </summary>
- /// <returns>Float</returns>
- public float ControllerSensitivity
- {
- get
- {
- return controllerSensitivity;
- }
- set
- {
- if (controllerSensitivity != value)
- needSave = true;
- controllerSensitivity = value;
- }
- }
- }
- }
|