/ContentSystem/Rendering/CameraData.cs
C# | 190 lines | 105 code | 16 blank | 69 comment | 8 complexity | 7584ccd056ebfc2d92d946ea5aaf3b08 MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.IO;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.Rendering
- {
- /// <summary>
- /// Helper class to store camera data and save it out as content to use used
- /// in levels or scenes (or just in your game code).
- /// </summary>
- public class CameraData : Content, ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// Version number for this CameraData. If this goes above 1, we need
- /// to support loading older versions as well. Saving is always going
- /// to be the latest version (this one).
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Get (Static)
- /// <summary>
- /// Get and load content based on the content name. This method makes sure
- /// we do not load the same content twice (the constructor is protected).
- /// </summary>
- /// <param name="contentName">Content name we want to load, this is
- /// passed onto the Content System, which will do the actual loading with
- /// help of the Load method in this class.</param>
- /// <returns>The loaded Content object, always unique for the same
- /// name, this helps comparing data.</returns>
- public static CameraData Get(string contentName)
- {
- return Get<CameraData>(contentName, ContentType.Camera);
- }
- #endregion
-
- #region FieldOfView (Public)
- /// <summary>
- /// Field of view, usually around 60.
- /// </summary>
- public float FieldOfView;
- #endregion
-
- #region NearPlane (Public)
- /// <summary>
- /// Near plane, when does the camera start in meters from the camera.
- /// </summary>
- public float NearPlane;
- #endregion
-
- #region FarPlane (Public)
- /// <summary>
- /// Far plane, how far can we look in meters.
- /// </summary>
- public float FarPlane;
- #endregion
-
- #region Path (Public)
- /// <summary>
- /// Optional precomputed camera path.
- /// </summary>
- public new Matrix[] Path;
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create camera data
- /// </summary>
- public CameraData()
- : base(EmptyName, ContentType.Camera)
- {
- Path = new Matrix[0];
- }
-
- /// <summary>
- /// Create camera data by loading it from content, use Get to call this.
- /// Use the static Get method to call this.
- /// </summary>
- /// <param name="contentName">Name of the content.</param>
- protected CameraData(string contentName)
- : base(contentName, ContentType.Camera)
- {
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Load camera data (positions, matrices, etc.) from a stream.
- /// </summary>
- /// <param name="reader">Reader</param>
- public void Load(BinaryReader reader)
- {
- // We currently only support our version, if more versions are added,
- // we need to do different loading code depending on the version here.
- int version = reader.ReadInt32();
- if (version != VersionNumber)
- {
- Log.InvalidVersionWarning(GetType().Name + ": " + Name, version,
- VersionNumber);
- return;
- }
-
- // the field of view
- FieldOfView = reader.ReadSingle();
- // the near
- NearPlane = reader.ReadSingle();
- // and far plane
- FarPlane = reader.ReadSingle();
- // and finally the camera animation path
- int numberOfPathFrames = reader.ReadInt32();
- Path = new Matrix[numberOfPathFrames];
- for (int index = 0; index < numberOfPathFrames; index++)
- {
- Path[index].Load(reader);
- }
- }
-
- /// <summary>
- /// Save camera data (positions, matrices, etc.) to a stream.
- /// </summary>
- /// <param name="writer">Writer</param>
- public void Save(BinaryWriter writer)
- {
- writer.Write(VersionNumber);
- // the field of view
- writer.Write(FieldOfView);
- // the near
- writer.Write(NearPlane);
- // and far plane
- writer.Write(FarPlane);
- // and finally the camera animation path
- writer.Write(Path.Length);
- for (int index = 0; index < Path.Length; index++)
- {
- Path[index].Save(writer);
- }
- }
- #endregion
-
- #region Methods (Private)
-
- #region Load
- /// <summary>
- /// Native load method, will just load the data.
- /// </summary>
- /// <param name="alreadyLoadedNativeData">
- /// The first instance that has already loaded the required content data
- /// of this content class or just 'null' if there is none loaded yet (or
- /// anymore).
- /// </param>
- protected override void Load(Content alreadyLoadedNativeData)
- {
- try
- {
- if (alreadyLoadedNativeData != null)
- {
- // Just clone all data
- CameraData otherData = alreadyLoadedNativeData as CameraData;
- FieldOfView = otherData.FieldOfView;
- NearPlane = otherData.NearPlane;
- FarPlane = otherData.FarPlane;
- Path = otherData.Path;
- // This object cannot be used for cloning now, but the caller (Mesh)
- // can set itself as the NativeClassObject and allow native cloning.
- return;
- }
-
- if (String.IsNullOrEmpty(RelativeFilePath) == false)
- {
- // Load via the ISaveLoadBinary interface methods below.
- // Cloning should not really happen for shaders anyway.
- FileHelper.Load(RelativeFilePath, this);
- }
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to load mesh animation data from file '" +
- RelativeFilePath + "': " + ex);
- FailedToLoad = true;
- }
- }
- #endregion
-
- #endregion
- }
- }
-