GameSettings.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //-----------------------------------------------------------------------------
  2. // GameSettings.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Xml.Serialization;
  14. using RacingGame.Helpers;
  15. using System.Threading;
  16. #if NETFX_CORE
  17. using Serializable = System.Runtime.Serialization.DataContractAttribute;
  18. #endif
  19. namespace RacingGame.Properties
  20. {
  21. /// <summary>
  22. /// Game settings, stored in a custom xml file. The reason for this is
  23. /// we want to be able to store our game data on the Xbox360 too.
  24. /// On the PC we could just use a Settings/config file and have all the
  25. /// code autogenerated for us, but this way it works both on PC and Xbox.
  26. /// Note: The default instance for the game settings is in this class,
  27. /// this way we get the same behaviour as for normal Settings files!
  28. /// </summary>
  29. [Serializable]
  30. public class GameSettings
  31. {
  32. /// <summary>
  33. /// Filename for our game settings file.
  34. /// </summary>
  35. const string SettingsFilename = "RacingGameSettings.xml";
  36. /// <summary>
  37. /// Default instance for our game settings.
  38. /// </summary>
  39. private static GameSettings defaultInstance = new GameSettings();
  40. /// <summary>
  41. /// Need saving the game settings file? Only set to true if
  42. /// we really changed some game setting here.
  43. /// </summary>
  44. private static bool needSave = false;
  45. /// <summary>
  46. /// Default
  47. /// </summary>
  48. /// <returns>Game settings</returns>
  49. public static GameSettings Default
  50. {
  51. get
  52. {
  53. return defaultInstance;
  54. }
  55. }
  56. /// <summary>
  57. /// Create game settings, don't allow public constructor!
  58. /// </summary>
  59. private GameSettings()
  60. {
  61. }
  62. /// <summary>
  63. /// Create game settings. This constructor helps us to only load the
  64. /// GameSettings once, not again if GameSettings is recreated by
  65. /// the Deserialization process.
  66. /// </summary>
  67. /// <param name="loadSettings">Load settings</param>
  68. public static void Initialize()
  69. {
  70. Load();
  71. }
  72. /// <summary>
  73. /// Load
  74. /// </summary>
  75. public static void Load()
  76. {
  77. bool saveImmediately = false;
  78. needSave = false;
  79. FileHelper.StorageContainerMRE.WaitOne();
  80. FileHelper.StorageContainerMRE.Reset();
  81. try
  82. {
  83. //TODO: Use Nick Gravlyn's easy storage?
  84. /*
  85. StorageDevice storageDevice = FileHelper.XnaUserDevice;
  86. if ((storageDevice != null) && storageDevice.IsConnected)
  87. {
  88. IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);
  89. async.AsyncWaitHandle.WaitOne();
  90. using (StorageContainer container =
  91. storageDevice.EndOpenContainer(async))
  92. {
  93. async.AsyncWaitHandle.Close();
  94. if (container.FileExists(SettingsFilename))
  95. {
  96. using (Stream file = container.OpenFile(SettingsFilename,
  97. FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  98. {
  99. if (file.Length > 0)
  100. {
  101. GameSettings loadedGameSettings =
  102. (GameSettings)new XmlSerializer(
  103. typeof(GameSettings)).Deserialize(file);
  104. if (loadedGameSettings != null)
  105. defaultInstance = loadedGameSettings;
  106. }
  107. else
  108. {
  109. // If the file is empty, just create a new file with the
  110. // default settings.
  111. needSave = true;
  112. saveImmediately = true;
  113. }
  114. }
  115. }
  116. else
  117. {
  118. // Create new file after exiting
  119. needSave = true;
  120. }
  121. }
  122. }*/
  123. }
  124. catch (Exception exc)
  125. {
  126. System.Diagnostics.Debug.WriteLine("Settings Load Failure: " + exc.ToString());
  127. }
  128. FileHelper.StorageContainerMRE.Set();
  129. if (saveImmediately)
  130. {
  131. Save();
  132. }
  133. }
  134. /// <summary>
  135. /// Save
  136. /// </summary>
  137. public static void Save()
  138. {
  139. // No need to save if everything is up to date.
  140. if (needSave == false)
  141. return;
  142. needSave = false;
  143. FileHelper.StorageContainerMRE.WaitOne();
  144. FileHelper.StorageContainerMRE.Reset();
  145. // Open a storage container
  146. try
  147. {
  148. /*
  149. StorageDevice storageDevice = FileHelper.XnaUserDevice;
  150. if ((storageDevice != null) && storageDevice.IsConnected)
  151. {
  152. IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);
  153. async.AsyncWaitHandle.WaitOne();
  154. using (StorageContainer container = storageDevice.EndOpenContainer(async))
  155. {
  156. async.AsyncWaitHandle.Close();
  157. using (Stream file = container.CreateFile(SettingsFilename))
  158. {
  159. // Save everything in this class with help of the XmlSerializer.
  160. new XmlSerializer(typeof(GameSettings)).
  161. Serialize(file, defaultInstance);
  162. }
  163. }
  164. }*/
  165. }
  166. catch (Exception exc)
  167. {
  168. System.Diagnostics.Debug.WriteLine("Settings Load Failure: " + exc.ToString());
  169. }
  170. FileHelper.StorageContainerMRE.Set();
  171. }
  172. /// <summary>
  173. /// Sets all of the graphical settings to their minimum possible
  174. /// values and saves the changes.
  175. /// </summary>
  176. public static void SetMinimumGraphics()
  177. {
  178. GameSettings.Default.ResolutionWidth = GameSettings.MinimumResolutionWidth;
  179. GameSettings.Default.ResolutionHeight = GameSettings.MinimumResolutionHeight;
  180. GameSettings.Default.ShadowMapping = false;
  181. GameSettings.Default.HighDetail = false;
  182. GameSettings.Default.PostScreenEffects = false;
  183. GameSettings.Save();
  184. }
  185. /// <summary>
  186. /// Highscores
  187. /// </summary>
  188. string highscores = "";
  189. /// <summary>
  190. /// Highscores
  191. /// </summary>
  192. /// <returns>String</returns>
  193. public string Highscores
  194. {
  195. get
  196. {
  197. return highscores;
  198. }
  199. set
  200. {
  201. if (highscores != value)
  202. needSave = true;
  203. highscores = value;
  204. }
  205. }
  206. /// <summary>
  207. /// Player name
  208. /// </summary>
  209. string playerName = "Player";
  210. /// <summary>
  211. /// Player name
  212. /// </summary>
  213. /// <returns>String</returns>
  214. public string PlayerName
  215. {
  216. get
  217. {
  218. return playerName;
  219. }
  220. set
  221. {
  222. if (playerName != value)
  223. needSave = true;
  224. playerName = value;
  225. }
  226. }
  227. public const int MinimumResolutionWidth = 640;
  228. /// <summary>
  229. /// Resolution width
  230. /// </summary>
  231. int resolutionWidth = 0;
  232. /// <summary>
  233. /// Resolution width
  234. /// </summary>
  235. /// <returns>Int</returns>
  236. public int ResolutionWidth
  237. {
  238. get
  239. {
  240. return resolutionWidth;
  241. }
  242. set
  243. {
  244. if (resolutionWidth != value)
  245. needSave = true;
  246. resolutionWidth = value;
  247. }
  248. }
  249. public const int MinimumResolutionHeight = 480;
  250. /// <summary>
  251. /// Resolution height
  252. /// </summary>
  253. int resolutionHeight = 0;
  254. /// <summary>
  255. /// Resolution height
  256. /// </summary>
  257. /// <returns>Int</returns>
  258. public int ResolutionHeight
  259. {
  260. get
  261. {
  262. return resolutionHeight;
  263. }
  264. set
  265. {
  266. if (resolutionHeight != value)
  267. needSave = true;
  268. resolutionHeight = value;
  269. }
  270. }
  271. /// <summary>
  272. /// Fullscreen
  273. /// </summary>
  274. bool fullscreen = true;
  275. /// <summary>
  276. /// Fullscreen
  277. /// </summary>
  278. /// <returns>Bool</returns>
  279. public bool Fullscreen
  280. {
  281. get
  282. {
  283. return fullscreen;
  284. }
  285. set
  286. {
  287. if (fullscreen != value)
  288. needSave = true;
  289. fullscreen = value;
  290. }
  291. }
  292. bool postScreenEffects = true;
  293. /// <summary>
  294. /// Post screen effects
  295. /// </summary>
  296. /// <returns>Bool</returns>
  297. public bool PostScreenEffects
  298. {
  299. get
  300. {
  301. return postScreenEffects;
  302. }
  303. set
  304. {
  305. if (postScreenEffects != value)
  306. needSave = true;
  307. postScreenEffects = value;
  308. }
  309. }
  310. bool shadowMapping = true;
  311. /// <summary>
  312. /// ShadowMapping
  313. /// </summary>
  314. /// <returns>Bool</returns>
  315. public bool ShadowMapping
  316. {
  317. get
  318. {
  319. return shadowMapping;
  320. }
  321. set
  322. {
  323. if (shadowMapping != value)
  324. needSave = true;
  325. shadowMapping = value;
  326. }
  327. }
  328. bool highDetail = true;
  329. /// <summary>
  330. /// HighDetail
  331. /// </summary>
  332. /// <returns>Bool</returns>
  333. public bool HighDetail
  334. {
  335. get
  336. {
  337. return highDetail;
  338. }
  339. set
  340. {
  341. if (highDetail != value)
  342. needSave = true;
  343. highDetail = value;
  344. }
  345. }
  346. /// <summary>
  347. /// Sound volume
  348. /// </summary>
  349. float soundVolume = 0.8f;
  350. /// <summary>
  351. /// Sound volume
  352. /// </summary>
  353. /// <returns>Float</returns>
  354. public float SoundVolume
  355. {
  356. get
  357. {
  358. return soundVolume;
  359. }
  360. set
  361. {
  362. if (soundVolume != value)
  363. needSave = true;
  364. soundVolume = value;
  365. }
  366. }
  367. /// <summary>
  368. /// Music volume
  369. /// </summary>
  370. float musicVolume = 0.6f;
  371. /// <summary>
  372. /// Music volume
  373. /// </summary>
  374. /// <returns>Float</returns>
  375. public float MusicVolume
  376. {
  377. get
  378. {
  379. return musicVolume;
  380. }
  381. set
  382. {
  383. if (musicVolume != value)
  384. needSave = true;
  385. musicVolume = value;
  386. }
  387. }
  388. /// <summary>
  389. /// Controller sensitivity
  390. /// </summary>
  391. float controllerSensitivity = 0.5f;
  392. /// <summary>
  393. /// Controller sensitivity
  394. /// </summary>
  395. /// <returns>Float</returns>
  396. public float ControllerSensitivity
  397. {
  398. get
  399. {
  400. return controllerSensitivity;
  401. }
  402. set
  403. {
  404. if (controllerSensitivity != value)
  405. needSave = true;
  406. controllerSensitivity = value;
  407. }
  408. }
  409. }
  410. }