protected override void OnMouseDown(MouseEventArgs e) { var pos = new int2(e.X / TileSize, e.Y / TileSize); if (InputMode == null) { if (e.Button == MouseButtons.Left) { CurrentTemplate = Templates.FirstOrDefault(t => t.Cells.ContainsKey(pos)); if (CurrentTemplate == null) Templates.Add(CurrentTemplate = new Template { Cells = new Dictionary<int2, bool> { { pos, true } } }); Invalidate(); } if (e.Button == MouseButtons.Right) { Templates.RemoveAll(t => t.Cells.ContainsKey(pos)); CurrentTemplate = null; Invalidate(); } } else { TerrainTypes[pos.X, pos.Y] = int.Parse(InputMode); Invalidate(); } }
string ExportTemplate(Template t, int n, string suffix, string dir) { var filename = Path.Combine(dir, "t{0:00}{1}".F(n, suffix)); var totalTiles = t.Width * t.Height; var ms = new MemoryStream(); using (var bw = new BinaryWriter(ms)) { bw.Write((ushort)24); bw.Write((ushort)24); bw.Write((uint)totalTiles); bw.Write((ushort)t.Width); bw.Write((ushort)t.Height); bw.Write((uint)0); // filesize placeholder bw.Flush(); bw.Write((uint)ms.Position + 24); // image start bw.Write((uint)0); // 0 (32bits) bw.Write((uint)0x2c730f8a); // magic? bw.Write((uint)0); // flags start bw.Write((uint)0); // walk start bw.Write((uint)0); // index start var src = surface1.Image; var data = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); unsafe { byte* p = (byte*)data.Scan0; for (var v = 0; v < t.Height; v++) for (var u = 0; u < t.Width; u++) { if (t.Cells.ContainsKey(new int2(u + t.Left, v + t.Top))) { byte* q = p + data.Stride * 24 * (v + t.Top) + 24 * (u + t.Left); for (var j = 0; j < 24; j++) for (var i = 0; i < 24; i++) bw.Write(q[i + j * data.Stride]); } else for (var x = 0; x < 24 * 24; x++) bw.Write((byte)0); /* todo: don't fill with air */ } } src.UnlockBits(data); bw.Flush(); var indexStart = ms.Position; for (var v = 0; v < t.Height; v++) for (var u = 0; u < t.Width; u++) bw.Write(t.Cells.ContainsKey(new int2(u + t.Left, v + t.Top)) ? (byte)(u + t.Width * v) : (byte)0xff); bw.Flush(); var flagsStart = ms.Position; for (var x = 0; x < totalTiles; x++ ) bw.Write((byte)0); bw.Flush(); var walkStart = ms.Position; for (var x = 0; x < totalTiles; x++) bw.Write((byte)0x8); var bytes = ms.ToArray(); Array.Copy(BitConverter.GetBytes((uint)bytes.Length), 0, bytes, 12, 4); Array.Copy(BitConverter.GetBytes(flagsStart), 0, bytes, 28, 4); Array.Copy(BitConverter.GetBytes(walkStart), 0, bytes, 32, 4); Array.Copy(BitConverter.GetBytes(indexStart), 0, bytes, 36, 4); File.WriteAllBytes(filename, bytes); } return filename; }
string ExportTemplate(Template t, int n, string suffix, string dir) { var tileSize = size; var filename = Path.Combine(dir, "{0}{1:00}{2}".F(txtTilesetName.Text, n, suffix)); var totalTiles = t.Width * t.Height; var ms = new MemoryStream(); using (var bw = new BinaryWriter(ms)) { bw.Write((ushort)tileSize); bw.Write((ushort)tileSize); bw.Write((uint)totalTiles); bw.Write((ushort)t.Width); bw.Write((ushort)t.Height); bw.Write((uint)0); // filesize placeholder bw.Flush(); bw.Write((uint)ms.Position + 24); // image start bw.Write((uint)0); // 0 (32bits) bw.Write((uint)0x2c730f8c); // magic? bw.Write((uint)0); // flags start bw.Write((uint)0); // walk start bw.Write((uint)0); // index start Bitmap src = surface1.Image.Clone(new Rectangle(0, 0, surface1.Image.Width, surface1.Image.Height), surface1.Image.PixelFormat); var data = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, src.PixelFormat); unsafe { byte *p = (byte *)data.Scan0; for (var v = 0; v < t.Height; v++) { for (var u = 0; u < t.Width; u++) { if (t.Cells.ContainsKey(new int2(u + t.Left, v + t.Top))) { byte *q = p + data.Stride * tileSize * (v + t.Top) + tileSize * (u + t.Left); for (var j = 0; j < tileSize; j++) { for (var i = 0; i < tileSize; i++) { bw.Write(q[i + j * data.Stride]); } } } else { for (var x = 0; x < tileSize * tileSize; x++) { bw.Write((byte)0); /* TODO: don't fill with air */ } } } } } src.UnlockBits(data); bw.Flush(); var indexStart = ms.Position; for (var v = 0; v < t.Height; v++) { for (var u = 0; u < t.Width; u++) { bw.Write(t.Cells.ContainsKey(new int2(u + t.Left, v + t.Top)) ? (byte)(u + t.Width * v) : (byte)0xff); } } bw.Flush(); var flagsStart = ms.Position; for (var x = 0; x < totalTiles; x++) { bw.Write((byte)0); } bw.Flush(); var walkStart = ms.Position; for (var x = 0; x < totalTiles; x++) { bw.Write((byte)0x8); } var bytes = ms.ToArray(); Array.Copy(BitConverter.GetBytes((uint)bytes.Length), 0, bytes, 12, 4); Array.Copy(BitConverter.GetBytes(flagsStart), 0, bytes, 28, 4); Array.Copy(BitConverter.GetBytes(walkStart), 0, bytes, 32, 4); Array.Copy(BitConverter.GetBytes(indexStart), 0, bytes, 36, 4); File.WriteAllBytes(filename, bytes); } return(filename); }