dev in the making

game development, maya and code by brainzizi

Posts Tagged ‘serializations

zCamera Save/Load

leave a comment »


On my zCamera project, I’ve added basic support for saving and loading.
The zBaseCamera, the base class of every camera provides this interface:

public virtual SaveSettings(string path);
public virtual LoadSettings(string path);

Save is done using the IntermediateSerializer, and load is done using XmlReader to support raw .xml loading.
See, the IntermediateSerializer output needs to be compiled at runtime in order for you to load it during your game. So no save/load sessions using just the serializer.

        /// <summary>
        /// Saves the camera settings.
        /// </summary>
        /// <param name="path">The path of the file to write, without the extension.</param>
        public virtual void SaveSettings(string path)
            XmlWriterSettings settings = new XmlWriterSettings { Indent = true };

            string fullPath = manager.Game.Content.RootDirectory + "\\" + path + ".xml";
            if (File.Exists(fullPath))

            XmlWriter writer = XmlWriter.Create(fullPath, settings);
            IntermediateSerializer.Serialize(writer, this, null);
        /// <summary>
        /// Loads the camera settings.
        /// </summary>
        /// <param name="path">The path of the file to read, without the extension.</param>
        public virtual void LoadSettings(string path)
            XmlReader reader = XmlReader.Create(manager.Game.Content.RootDirectory + "\\" + path + ".xml");
            while (reader.Read())
                if (reader.Name == "Name")
                    Name = reader.ReadElementContentAsString();

                if (reader.Name == "Debug")
                    Debug = reader.ReadElementContentAsBoolean();

                if (reader.Name == "FocalDistance")
                    FocalDistance = reader.ReadElementContentAsFloat();

                if (reader.Name == "FieldOfView")
                    FieldOfView = reader.ReadElementContentAsFloat();

                if (reader.Name == "AspectRatio")
                    AspectRatio = reader.ReadElementContentAsFloat();

                if (reader.Name == "NearPlaneDistance")
                    NearPlaneDistance = reader.ReadElementContentAsFloat();

                if (reader.Name == "FarPlaneDistance")
                    FarPlaneDistance = reader.ReadElementContentAsFloat();

                if (reader.Name == "Position")
                    string vector = reader.ReadElementContentAsString();
                    Position = zCameraHelper.ParseFromIntermediateSerializer(vector);

I’ll release the complete source next week.

Written by brainzizizi

08.11.2009 at 07:09