private void saveButton_Click(object sender, EventArgs e) { DialogResult res = saveFileDialog.ShowDialog(); if (res != DialogResult.OK) { return; } /* * UURM:4 bytes * tiles offset:4 bytes * width: byte * height: byte * num tile textures: 2 bytes * [id:byte. strlen: byte. string]* * (0)012 - texture id. 34 - rotation. 567 - up, (1)012 - right, 345-down, 67 * (2)0 - left. 1 - exists 2 - passible, 34567 - reserved (3)01234567 - reserved */ List <byte> data = new List <byte>(); data.Add(Convert.ToByte('U')); data.Add(Convert.ToByte('U')); data.Add(Convert.ToByte('R')); data.Add(Convert.ToByte('M')); data.Add(0); data.Add(0); data.Add(0); data.Add(0); Dictionary <string, int> tilesMap = mapTiles(); int numTiles = width * height; data.Add(Convert.ToByte(width)); data.Add(Convert.ToByte(height)); byte[] sizeBytes = BitConverter.GetBytes(Convert.ToInt16(tilesMap.Count)); data.Add(sizeBytes[0]); data.Add(sizeBytes[1]); foreach (string texName in tilesMap.Keys) { int id = tilesMap[texName]; data.Add(Convert.ToByte(id)); data.Add(Convert.ToByte(texName.Length)); for (int i = 0; i < texName.Length; ++i) { data.Add(Convert.ToByte(texName[i])); } } int offset = data.Count; byte[] offsetBytes = BitConverter.GetBytes(offset); data[4] = offsetBytes[0]; data[5] = offsetBytes[1]; data[6] = offsetBytes[2]; data[7] = offsetBytes[3]; for (int j = 0; j < height; ++j) { for (int i = 0; i < width; ++i) { TileInfo tile = tiles[i, j]; byte byte1 = 0; byte texID = Convert.ToByte(tilesMap[Path.GetFileName(tile.tileBox.ImageLocation)]); byte1 |= (byte)(texID << 5); byte upWall = getWallType(tile.up); byte rightWall = getWallType(tile.right); byte downWall = getWallType(tile.down); byte leftWall = getWallType(tile.left); byte1 |= (upWall); byte byte2 = 0; byte2 |= (byte)(rightWall << (byte)5); byte2 |= (byte)(downWall << (byte)2); byte2 |= (byte)((leftWall & 6) >> 1); byte byte3 = 0; byte3 |= (byte)((leftWall & 1) << 7); byte3 |= (byte)((tile.exists?(1 << 6):0) | (tile.passible?(1 << 5):0)); data.Add(byte1); data.Add(byte2); data.Add(byte3); data.Add(0); } } FileStream stream = File.Create(saveFileDialog.FileName); BinaryWriter writer = new BinaryWriter(stream); writer.Write(data.ToArray()); writer.Close(); stream.Close(); }
private void openButton_Click(object sender, EventArgs e) { DialogResult res = openFileDialog.ShowDialog(); if (res != DialogResult.OK) { return; } byte[] fileBytes = File.ReadAllBytes(openFileDialog.FileName); if (fileBytes[0] != Convert.ToByte('U') || fileBytes[1] != Convert.ToByte('U') || fileBytes[2] != Convert.ToByte('R') || fileBytes[3] != Convert.ToByte('M')) { return; } /* * UURM:4 bytes * tiles offset:4 bytes * width: byte * height: byte * num tile textures: 2 bytes * [id:byte. strlen: byte. string]* * (0)012 - texture id. 34 - rotation. 567 - up, (1)012 - right, 345-down, 67(2)0 - left * (2)12 - type, 34567 - reserved (3)01234567 - reserved */ int tileOffset = fileBytes[5] << 8 | fileBytes[4]; int width = fileBytes[8]; int height = fileBytes[9]; int numTextures = fileBytes[11] << 8 | fileBytes[10]; int offset = 12; Dictionary <int, string> textureMap = new Dictionary <int, string>(); for (int i = 0; i < numTextures; ++i) { int id = fileBytes[offset++]; int strLen = fileBytes[offset++]; string texName = ""; for (int j = 0; j < strLen; ++j) { texName += Convert.ToChar(fileBytes[offset++]); } textureMap[id] = texName; } widthBox.Text = width.ToString(); heightBox.Text = height.ToString(); tiles = null; resizeRoom(); tiles = new TileInfo[width, height]; for (int j = 0; j < height; ++j) { for (int i = 0; i < width; ++i) { byte byte1 = fileBytes[offset++]; byte byte2 = fileBytes[offset++]; byte byte3 = fileBytes[offset++]; offset++; //skip reserved byte; int textureID = (byte1 & 0xE0) >> 5; int up = byte1 & 7; int right = (byte2 & 0xE0) >> 5; int down = (byte2 & 0x1C) >> 2; int left = (byte2 & 1) << 1 | ((byte3 & 0xC0) >> 7); bool exists = (byte3 & 0x40) != 0; bool passible = (byte3 & 0x20) != 0; TileInfo tile = new TileInfo(); tile.x = i; tile.y = j; tile.tileBox = new PictureBox(); tile.tileBox.Left = i * 25 + 1; tile.tileBox.Top = j * 25 + 1; tile.tileBox.Click += new EventHandler(roomTileClick); tile.tileBox.Width = 25; tile.tileBox.Height = 25; tile.tileBox.ImageLocation = tilesDir + "\\" + textureMap[textureID]; tile.up = getWallType(up); tile.right = getWallType(right); tile.down = getWallType(down); tile.left = getWallType(left); tile.exists = exists; tile.passible = passible; tiles[i, j] = tile; } } resizeRoom(); redrawRoomTiles(); }
private void resizeRoom() { int x = -1; int y = -1; try { x = int.Parse(widthBox.Text); y = int.Parse(heightBox.Text); } catch (System.Exception) { return; } if (x < 1 || x > maxWidth || y < 1 || y > maxHeight) { return; } roomTilesPanel.Width = x * 25 + 4; roomTilesPanel.Height = y * 25 + 4; TileInfo[,] newTiles = new TileInfo[x, y]; for (int i = 0; i < x; ++i) { for (int j = 0; j < y; ++j) { newTiles[i, j] = new TileInfo(); if (i < width && j < height && tiles != null) { TileInfo oldTile = tiles[i, j]; newTiles[i, j] = oldTile; if (i == width - 1) { //newTiles[i, j].right = TileInfo.WallType.Empty; } if (j == height - 1) { //newTiles[i, j].down = TileInfo.WallType.Empty; } if (i == x - 1) { //newTiles[i, j].right = TileInfo.WallType.Wall; } if (j == y - 1) { //newTiles[i, j].down = TileInfo.WallType.Wall; } } else { TileInfo newTile = new TileInfo(); newTile.x = i; newTile.y = j; newTile.tileBox = new PictureBox(); newTile.tileBox.Left = i * 25 + 1; newTile.tileBox.Top = j * 25 + 1; if (i > 0) { newTile.left = newTiles[i - 1, j].right; } else { newTile.left = TileInfo.WallType.Wall; } if (i < x - 1) { newTile.right = TileInfo.WallType.Empty; } else { newTile.right = TileInfo.WallType.Wall; } if (j > 0) { newTile.up = newTiles[i, j - 1].down; } else { newTile.up = TileInfo.WallType.Wall; } if (j < y - 1) { newTile.down = TileInfo.WallType.Empty; } else { newTile.down = TileInfo.WallType.Wall; } newTile.tileBox.Click += new EventHandler(roomTileClick); newTile.tileBox.Width = 25; newTile.tileBox.Height = 25; newTile.tileBox.ImageLocation = "empty.bmp"; //newTile.tileBox.BorderStyle = BorderStyle.FixedSingle; newTiles[i, j] = newTile; } } } horizontalWalls = new PictureBox[x + 1, y + 1]; verticalWalls = new PictureBox[x + 1, y + 1]; for (int i = 0; i <= x; i++) { for (int j = 0; j <= y; j++) { PictureBox horizontal = new PictureBox(); horizontal.Width = 25; horizontal.Height = 2; horizontal.Left = i * 25 + 1; horizontal.Top = j * 25; horizontalWalls[i, j] = horizontal; PictureBox vertical = new PictureBox(); vertical.Width = 2; vertical.Height = 25; vertical.Left = i * 25; vertical.Top = j * 25 + 1; verticalWalls[i, j] = vertical; } } tiles = newTiles; width = x; height = y; redrawRoomTiles(); selectTile(tiles[0, 0]); }