Simple XNA Cross Platform Settings Manager

25. January 2011 12:50 by gogman in Development  //  Tags: , , ,   //   Comments (0)

One of the joys of doing XNA development is the ability to do cross-platform applications quite easily. I must admit I am really enjoying being able to reuse 99% of my code on the PC, XBox, and Windows Phone 7.

One of the common things all applications need, regardless of platform, is a way to persist application settings between application runs. While there are built-in features and many open source libraries available to persist application settings, I found that most were overkill for what I needed.

For me, a simple static class the exposes settings as a series of properties on an object are all I really need.

I present my simple SettingsManager.

This class uses IsolatedStorage on all platforms and requires no special permissions to read and write the settings file. It literally is a drop-in solution for persisting your applications settings.

It exposes only two methods:

  • LoadSettings
  • SaveSettings

It also exposes one object that contains your applications settings:

  • Settings

This class is very simple to use. Simply call LoadSettings in your application constructor and then you have access to all your application settings – even on the first run of your application. Any time you change a setting, simply call SaveSettings. Since the class is static, you have access to all of your settings from anywhere in the application which I find helps simplify the code I write since the application settings are now just essentially global variables.

All you need to do to adapt this for your app is simply change the namespace and modify the AppSettings class to contain the properties you wish to persist. Also don’t forget to set the defaults for those properties in the AppSettings class initializer. Setting the defaults is very important as these are the values your application will see when the settings file is initialized.

Ok, enough talk – here’s the code.

#region usings
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
#endregion
 
namespace YourNamespace
{
 
  #region application settings
 
  public class AppSettings
  {
 
    public bool HasRunOnce { get; set; }
    public bool IsFullScreen { get; set; }
    public bool EnableMusic { get; set; }
    public bool EnableSfx { get; set; }
 
    public AppSettings()
    {
      // Create our default settings
      HasRunOnce = true;
      EnableMusic = true;
      EnableSfx = true;
 
      // Since this is cross platform, you can decide what default values to use for a platform.
      // In the case of full screen, phones and XBoxes are always full screen.
      // In the phone an XBox applications, we don't let the user change this setting.
#if WINDOWS
      IsFullScreen = false;
#else
IsFullScreen = true;
#endif
 
    }
  }
 
  #endregion
 
 
  #region settings manager
 
  static class SettingsManager
  {
 
    private static string fileName = "settings.xml";
    public static AppSettings Settings = new AppSettings();
 
 
    public static void LoadSettings()
    {
      // Create our exposed settings class. This class gets serialized to load/save the settings.
      Settings = new AppSettings();
      //Obtain a virtual store for application
#if WINDOWS
      IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForDomain();
#else
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
#endif
      // Check if file is there
      if (fileStorage.FileExists(fileName))
      {
        XmlSerializer serializer = new XmlSerializer(Settings.GetType());
        StreamReader stream = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, fileStorage));
        try
        {
          Settings = (AppSettings)serializer.Deserialize(stream);
          stream.Close();
        }
        catch
        {
          // An error occurred so let's use the default settings.
          stream.Close();
          Settings = new AppSettings();
          // Saving is optional - in this sample we assume it works and the error is due to the file not being there.
          SaveSettings();
          // Handle other errors here
        }
      }
      else
      {
        SaveSettings();
      }
    }
 
 
    public static void SaveSettings()
    {
      //Obtain a virtual store for application
#if WINDOWS
      IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForDomain();
#else
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
#endif
      XmlSerializer serializer = new XmlSerializer(Settings.GetType());
      StreamWriter stream = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Create, fileStorage));
      try
      {
        serializer.Serialize(stream, Settings);
      }
      catch
      {
        // Handle your errors here
      }
      stream.Close();
    }
 
  }
 
  #endregion
 
}

Enjoy!

28 Days Later - Windows Phone 7 and the Samsung Focus

30. November 2010 00:56 by gogman in Technology, Development  //  Tags: ,   //   Comments (0)

ATT_i917-Cetus_front1_400x400November 8th was a good day for lovers of smartphones, social networking, and the Internet. AT&T and Microsoft released three new Windows Phone 7 phones in North America. The HTC Surround, LG Quantum and Samsung Focus.

I picked up a Samsung Focus and have now been using it for 28 days both as a consumer and developer. I decided to take some time and post some of my thoughts on Windows Phone 7 OS, the hardware, and the reaction from the technology press.

This is a long post that I hope you find is not a complete waste of your time.

More...

Yes, I am Still Alive

23. September 2010 02:21 by gogman in Development  //  Tags: ,   //   Comments (2)

I’ve been very busy spending about 10-12 hours a day learning XNA for the PC, Windows Phone 7 as well as learning 3DS Max 2011.

It’s been 15 years or so since I wrote a videogame and never one in 3D. I have to say, I am enjoying it. 3D is not so bad. In some ways it’s actually easier than 2D game development. And oh, the tools, oh the lovely tools! It’s amazingly nice to have to NOT write all the low level code. The last game I wrote utilized Mode X graphics and the display code was done in assembler. It took a very long time to build the tools and libraries required to build the game. Now it’s so nice to be able to concentrate on the high-level code and object management instead of writing all the low level tools and libraries – and do it all for 1920x1080 pixels. What a difference a decade and a half makes :)

Hopefully I’ll have some tech demos up here soon. I didn’t have to deal with pixel shaders, vertex shaders, or frustum culling 15 years ago so there is a lot to learn and catch up on.

About The Bloggers

gogman

gogmanBorn naked, unable to communicate, walk, or feed himself, Gogman overcame these handicaps to become a technologist, decent open water sailor, pretty darn good cook, husband, cat lover, and mediocre blogger.

Gogman works as the CTO at Atomic Goat Studios, an independent game studio start-up located in Southern California developing games for the PC, XBox 360, and Windows Phone 7.

Member Hawaii Yacht Club

dantwo

dantwoFound in a universe that probably exists, Dantwo prides himself on his ability to use the laws of nature to perform complex tasks; such as operating a keyboard.

A graduate student and teaching assistant by trade, he is also found enjoying games, technology, rudimentary philosophy, and observing the universe.

We miss you Andrew

Recent Comments

Comment RSS