Example #1
0
		public Editor(string fileName)
			: this()
		{
			this.currentFile = new FileInfo(fileName);
			if (!this.currentFile.Exists)
			{
				MessageBox.Show(string.Format("Couldn't find file \"{0}\".", fileName));
				return;
			}

			try
			{
				this.terrain = Terrain.Read(fileName);
			}
			catch (Exception bug)
			{
				MessageBox.Show(bug.ToString(), "Failed to load terrain.");
			}

			if (this.terrain != null)
			{
				this.initialize();
				Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;
			}
			else
			{
				this.currentFile = null;
			}
		}
Example #2
0
		public static void WriteHeightmap(Stream stream, Terrain terrain)
		{
			StreamWriter writer = new StreamWriter(stream, Encoding.ASCII, 4096, true);

			writer.WriteLine("P2");
			writer.WriteLine("{0} {1}", terrain.Width.ToString(CultureInfo.InvariantCulture), terrain.Height.ToString(CultureInfo.InvariantCulture));
			writer.WriteLine("65535");

			for (int y = 0; y < terrain.Height; y++)
			{
				for (int x = 0; x < terrain.Width; x++)
				{
					writer.Write(((int)terrain.HeightMap[x, y] - terrain.HeightMapMin).ToString(CultureInfo.InvariantCulture));
					writer.Write(" ");
				}
				writer.WriteLine();
			}

			writer.Close();
		}
Example #3
0
		public static void ReadHeightmap(Stream stream, Terrain terrain)
		{
			string header = readToken(stream);
			if (header != "P2")
				throw new NotSupportedException("Formats other than ASCII graymaps (P2) are not supported.");

			int width = int.Parse(readToken(stream), CultureInfo.InvariantCulture);
			if (width != terrain.Width)
				throw new Exception("Width mismatch.");

			int height = int.Parse(readToken(stream), CultureInfo.InvariantCulture);
			if (height != terrain.Height)
				throw new Exception("Height mismatch.");

			readToken(stream); // max.

			for (int y = 0; y < terrain.Height; y++)
			{
				for (int x = 0; x < terrain.Width; x++)
				{
					int value = int.Parse(readToken(stream), CultureInfo.InvariantCulture);
					if (value < 0 || value > 65535)
						throw new InvalidDataException("Invalid value.");

					value += short.MinValue;
					terrain.HeightMap[x, y] = (short)value;
				}
			}
			
			terrain.UpdateMinMax();
			
			int translation = -terrain.HeightMapMin;

			if (terrain.HeightMapMax + translation > short.MaxValue)
				translation = short.MaxValue - terrain.HeightMapMax;

			if (translation > 0)
				terrain.Translate(translation);
		}
Example #4
0
		public Editor(Terrain terrain)
			: this()
		{
			this.terrain = terrain;
			this.initialize();
		}
Example #5
0
		private void openTerrain(object sender, EventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Filter = terrainFileFilter;
			dialog.InitialDirectory = Properties.Settings.Default.OpenFileInitialDirectory;
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				if (this.terrain == null)
				{
					try
					{
						this.terrain = Terrain.Read(dialog.FileName);
					}
					catch (Exception bug)
					{
						MessageBox.Show(bug.ToString(), "Failed to load terrain.");
					}

					if (this.terrain != null)
					{
						this.currentFile = new FileInfo(dialog.FileName);
						Properties.Settings.Default.OpenFileInitialDirectory = this.currentFile.DirectoryName;

						this.initialize();
					}
				}
				else
				{
					Editor editor = new Editor(dialog.FileName);
					editor.Show();
				}
			}
		}
Example #6
0
		/// <summary>
		/// Reads a terrain from a stream.
		/// </summary>
		/// <param name="stream">The data stream.</param>
		/// <returns></returns>
		public static Terrain Read(Stream stream)
		{
			BinaryReader reader = new BinaryReader(stream);

			if (reader.ReadUInt32() != 0x52524554u) // 'TERR'
				throw new Exception("Invalid magic number.");

			int version = reader.ReadInt32();

			if(version < 1 || version > 3)
				throw new NotSupportedException(string.Format("Version {0} is not supported.", version));

			reader.ReadUInt16(); // some other width?
			reader.ReadUInt16(); // some other height?

			int width = reader.ReadUInt16() * 2;
			int height = reader.ReadUInt16() * 2;

			Terrain terrain = new Terrain(width, height);

			for (int y = 0; y < height; y += 4)
			{
				for (int x = 0; x < width; x += 4)
				{
					// height map
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							short value = reader.ReadInt16();
							terrain.HeightMap[x + cx, y + cy] = value;
							if (value < terrain.heightMapMin) terrain.heightMapMin = value;
							if (value > terrain.heightMapMax) terrain.heightMapMax = value;
						}
						if (version < 3) reader.ReadInt16();
					}
					if (version < 3) reader.ReadBytes(10);

					// normal map
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.NormalMap[x + cx, y + cy] = reader.ReadByte();
						}
						if (version < 3) reader.ReadByte();
					}
					if (version < 3) reader.ReadBytes(5);

					// color map
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.ColorMap[x + cx, y + cy].R = reader.ReadByte();
							terrain.ColorMap[x + cx, y + cy].G = reader.ReadByte();
							terrain.ColorMap[x + cx, y + cy].B = reader.ReadByte();
						}
						if (version < 3) reader.ReadBytes(3);
					}
					if (version < 3) reader.ReadBytes(15);

					// alpha map 1
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.AlphaMap1[x + cx, y + cy] = reader.ReadByte();
						}
						if (version < 3) reader.ReadByte();
					}
					if (version < 3) reader.ReadBytes(5);

					// alpha map 2
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.AlphaMap2[x + cx, y + cy] = reader.ReadByte();
						}
						if (version < 3) reader.ReadByte();
					}
					if (version < 3) reader.ReadBytes(5);

					// alpha map 3
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.AlphaMap3[x + cx, y + cy] = reader.ReadByte();
						}
						if (version < 3) reader.ReadByte();
					}
					if (version < 3) reader.ReadBytes(5);

					// cliff map
					for (int cy = 0; cy < 4; cy++)
					{
						for (int cx = 0; cx < 4; cx++)
						{
							terrain.CellMap[x + cx, y + cy] = (CellType)reader.ReadByte();
						}
						if (version < 3) reader.ReadByte();
					}
					if (version < 3) reader.ReadBytes(5);

					// info map
					terrain.InfoMap[x / 4, y / 4] = reader.ReadUInt32();

					// ???
					if (version < 3) reader.ReadBytes(25);
					if (version == 2) reader.ReadByte();
				}
			}

			return terrain;
		}