PageRenderTime 36ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ContentSystem/Rendering/CameraData.cs

#
C# | 190 lines | 105 code | 16 blank | 69 comment | 8 complexity | 7584ccd056ebfc2d92d946ea5aaf3b08 MD5 | raw file
  1using System;
  2using System.IO;
  3using Delta.Utilities;
  4using Delta.Utilities.Datatypes;
  5using Delta.Utilities.Helpers;
  6
  7namespace Delta.ContentSystem.Rendering
  8{
  9	/// <summary>
 10	/// Helper class to store camera data and save it out as content to use used
 11	/// in levels or scenes (or just in your game code).
 12	/// </summary>
 13	public class CameraData : Content, ISaveLoadBinary
 14	{
 15		#region Constants
 16		/// <summary>
 17		/// Version number for this CameraData. If this goes above 1, we need
 18		/// to support loading older versions as well. Saving is always going
 19		/// to be the latest version (this one).
 20		/// </summary>
 21		private const int VersionNumber = 1;
 22		#endregion
 23
 24		#region Get (Static)
 25		/// <summary>
 26		/// Get and load content based on the content name. This method makes sure
 27		/// we do not load the same content twice (the constructor is protected).
 28		/// </summary>
 29		/// <param name="contentName">Content name we want to load, this is
 30		/// passed onto the Content System, which will do the actual loading with
 31		/// help of the Load method in this class.</param>
 32		/// <returns>The loaded Content object, always unique for the same
 33		/// name, this helps comparing data.</returns>
 34		public static CameraData Get(string contentName)
 35		{
 36			return Get<CameraData>(contentName, ContentType.Camera);
 37		}
 38		#endregion
 39
 40		#region FieldOfView (Public)
 41		/// <summary>
 42		/// Field of view, usually around 60.
 43		/// </summary>
 44		public float FieldOfView;
 45		#endregion
 46
 47		#region NearPlane (Public)
 48		/// <summary>
 49		/// Near plane, when does the camera start in meters from the camera.
 50		/// </summary>
 51		public float NearPlane;
 52		#endregion
 53
 54		#region FarPlane (Public)
 55		/// <summary>
 56		/// Far plane, how far can we look in meters.
 57		/// </summary>
 58		public float FarPlane;
 59		#endregion
 60
 61		#region Path (Public)
 62		/// <summary>
 63		/// Optional precomputed camera path.
 64		/// </summary>
 65		public new Matrix[] Path;
 66		#endregion
 67
 68		#region Constructors
 69		/// <summary>
 70		/// Create camera data
 71		/// </summary>
 72		public CameraData()
 73			: base(EmptyName, ContentType.Camera)
 74		{
 75			Path = new Matrix[0];
 76		}
 77
 78		/// <summary>
 79		/// Create camera data by loading it from content, use Get to call this.
 80		/// Use the static Get method to call this.
 81		/// </summary>
 82		/// <param name="contentName">Name of the content.</param>
 83		protected CameraData(string contentName)
 84			: base(contentName, ContentType.Camera)
 85		{
 86		}
 87		#endregion
 88
 89		#region ISaveLoadBinary Members
 90		/// <summary>
 91		/// Load camera data (positions, matrices, etc.) from a stream.
 92		/// </summary>
 93		/// <param name="reader">Reader</param>
 94		public void Load(BinaryReader reader)
 95		{
 96			// We currently only support our version, if more versions are added,
 97			// we need to do different loading code depending on the version here.
 98			int version = reader.ReadInt32();
 99			if (version != VersionNumber)
100			{
101				Log.InvalidVersionWarning(GetType().Name + ": " + Name, version,
102					VersionNumber);
103				return;
104			}
105
106			// the field of view
107			FieldOfView = reader.ReadSingle();
108			// the near
109			NearPlane = reader.ReadSingle();
110			// and far plane
111			FarPlane = reader.ReadSingle();
112			// and finally the camera animation path
113			int numberOfPathFrames = reader.ReadInt32();
114			Path = new Matrix[numberOfPathFrames];
115			for (int index = 0; index < numberOfPathFrames; index++)
116			{
117				Path[index].Load(reader);
118			}
119		}
120
121		/// <summary>
122		/// Save camera data (positions, matrices, etc.) to a stream.
123		/// </summary>
124		/// <param name="writer">Writer</param>
125		public void Save(BinaryWriter writer)
126		{
127			writer.Write(VersionNumber);
128			// the field of view
129			writer.Write(FieldOfView);
130			// the near
131			writer.Write(NearPlane);
132			// and far plane
133			writer.Write(FarPlane);
134			// and finally the camera animation path
135			writer.Write(Path.Length);
136			for (int index = 0; index < Path.Length; index++)
137			{
138				Path[index].Save(writer);
139			}
140		}
141		#endregion
142
143		#region Methods (Private)
144
145		#region Load
146		/// <summary>
147		/// Native load method, will just load the data.
148		/// </summary>
149		/// <param name="alreadyLoadedNativeData">
150		/// The first instance that has already loaded the required content data
151		/// of this content class or just 'null' if there is none loaded yet (or
152		/// anymore).
153		/// </param>
154		protected override void Load(Content alreadyLoadedNativeData)
155		{
156			try
157			{
158				if (alreadyLoadedNativeData != null)
159				{
160					// Just clone all data
161					CameraData otherData = alreadyLoadedNativeData as CameraData;
162					FieldOfView = otherData.FieldOfView;
163					NearPlane = otherData.NearPlane;
164					FarPlane = otherData.FarPlane;
165					Path = otherData.Path;
166					// This object cannot be used for cloning now, but the caller (Mesh)
167					// can set itself as the NativeClassObject and allow native cloning.
168					return;
169				}
170
171				if (String.IsNullOrEmpty(RelativeFilePath) == false)
172				{
173					// Load via the ISaveLoadBinary interface methods below.
174					// Cloning should not really happen for shaders anyway.
175					FileHelper.Load(RelativeFilePath, this);
176				}
177			}
178			catch (Exception ex)
179			{
180				Log.Warning("Failed to load mesh animation data from file '" +
181				            RelativeFilePath + "': " + ex);
182				FailedToLoad = true;
183			}
184		}
185		#endregion
186
187		#endregion
188	}
189}
190