地图预览切割碰撞生成

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string result = "";
Dictionary<string, int> dictIgnore = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
string firstPath = "E:/resource/map/";
string[] pathMaps = { "hg-" };
Dictionary<string, int> dict = new Dictionary<string, int>();
Dictionary<string, int> dict2 = new Dictionary<string, int>();
Dictionary<string, int> dict3 = new Dictionary<string, int>();

dict.Add("bq-", 30);
dict.Add("brm-", 22);
dict.Add("fmg-", 54);
dict.Add("cyd-", 16);
dict.Add("shm6-", 2);
dict.Add("fm2-", 4);
dict.Add("fm5-", 4);
dict.Add("nm7-", 4);
dict.Add("zm72-", 4);

if (!Directory.Exists(firstPath + "map/")) Directory.CreateDirectory(firstPath + "map/");
if (!Directory.Exists(firstPath + "mmap/")) Directory.CreateDirectory(firstPath + "mmap/");
if (!Directory.Exists(firstPath + "mapres/")) Directory.CreateDirectory(firstPath + "mapres/");
List<string[,,]> resultList = new List<string[,,]>();
for (int i = 0; i < pathMaps.Length; ++i)
{
resultList.Add(this.dispose(firstPath + pathMaps[i] + ".map"));
int hulve = dict.ContainsKey(pathMaps[i]) ? dict[pathMaps[i]] : 0;//忽略的行数
int hulve2 = dict2.ContainsKey(pathMaps[i]) ? dict2[pathMaps[i]] : 0;//忽略的行数
int hulve3 = dict3.ContainsKey(pathMaps[i]) ? dict3[pathMaps[i]] : 0;//忽略的行数
}

for (int r = 0; r < resultList.Count; ++r)
{
int hulve = dict.ContainsKey(pathMaps[r]) ? dict[pathMaps[r]] : 0;//忽略的行数
int hulve2 = dict2.ContainsKey(pathMaps[r]) ? dict2[pathMaps[r]] : 0;//忽略的行数
int hulve3 = dict3.ContainsKey(pathMaps[r]) ? dict3[pathMaps[r]] : 0;//忽略的行数
string[,,] dataArray = resultList[r];
int mapWidth = dataArray.GetLength(1);
int mapHeight = dataArray.GetLength(0);

int newWidth = mapWidth - hulve3 * 2;
int newHeight = mapHeight - hulve - hulve2;

string resultData = "" + newWidth + "," + newHeight;
for (int i = hulve; i < mapHeight - hulve2; ++i)
{
for (int j = hulve3; j < mapWidth - hulve3; ++j)
{
resultData += "," + dataArray[i, j, 2];
}
}
this.Write(firstPath + "map/" + pathMaps[r] + ".mapag", resultData);


this.ignoreAll(newWidth, newHeight,resultData);
this.dispose(firstPath, pathMaps[r], hulve, hulve2, hulve3);
}

//输出忽略的图片名字
File.AppendAllText(firstPath + "1.txt", result);
}


public string[,,] dispose(string pathMap)
{
FileStream readStream = new FileStream(pathMap, FileMode.Open);
byte[] data = new byte[1024];
readStream.Read(data, 0, 52);
int mapWidth = (int)data[0] + 256 * data[1];
int mapHeight = (int)data[2] + 256 * data[3];
string[,,] dataArray = new string[mapHeight, mapWidth, 3];
int curWidth = 0;
int curHeight = 0;
while (curWidth < mapWidth)
{
int length = readStream.Read(data, 0, 14);
if (length == 0)
{
break;
}
else
{
string peng = "0";
if (data[1] >= 128)
{
data[1] -= 128;
peng = "1";
}
if (data[5] >= 128)
{
data[5] -= 128;
peng = "1";
}

dataArray[mapHeight - 1 - curHeight, curWidth, 0] = "";
dataArray[mapHeight - 1 - curHeight, curWidth, 1] = "";
dataArray[mapHeight - 1 - curHeight, curWidth, 2] = peng;
++curHeight;
if (curHeight >= mapHeight)
{
curHeight = 0;
++curWidth;
}
}
}
readStream.Close();
return dataArray;
}
private void Write(string path, string str)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write(str);
//清空缓冲区
sw.Flush();
//关闭流
sw.Dispose();
sw.Close();
fs.Dispose();
fs.Close();
}


public void ignoreAll(int w, int h,string str)
{
string[] arr = str.Split(',');
//增加
dictIgnore.Clear();
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
if(arr[i * w + j+2] == "0")
{
this.ignoreAll2(j, i, w, h);
}
}
}
}

public void ignoreAll2(int x, int y, int width, int height)
{
int w = 48, h = 32;
int logicX = (int)Math.Floor(((double)x + 0.5) * w / 512), logicY = (int)Math.Floor(((double)y + 0.5) * h / 512);
int logicXAll = (int)Math.Ceiling(((double)width * w - 1) / 512), logicYAll = (int)Math.Ceiling(((double)height * h - 1) / 512);
//增加
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
if (logicX + j >= 0 && logicX + j < logicXAll && logicY + i >= 0 && logicY + i < logicYAll)
{
string str = "" + ((logicY + i) * logicXAll + logicX + j);
while (str.Length < 3) str = "0" + str;
if (!dictIgnore.ContainsKey(str)) dictIgnore.Add(str, 1);
}
}
}
}


public void dispose(string firstPath, string str, int hulve, int hulve2, int hulve3)
{
int hulvePixis = hulve * 32;
int hulvePixis2 = hulve2 * 32;
int hulvePixis3 = hulve3 * 48;
int tile = 512;

Stream s = File.Open(firstPath + str + ".bmp", FileMode.Open);
Image srcImage = Image.FromStream(s);
s.Close();

int resW = srcImage.Width;
int resH = srcImage.Height;
int num = 0;
Bitmap resultImage = new Bitmap(tile, tile);
for (int m = resH - 1 - tile - hulvePixis; m >= -(tile - 1 - hulvePixis2); m = m - tile)
{
for (int n = hulvePixis3; n < resW - hulvePixis3; n = n + tile)
{
Graphics g = Graphics.FromImage(resultImage);
g.Clear(Color.Black);
g.DrawImage(srcImage, new Rectangle(0, 0, tile, tile), new Rectangle(n, m, tile, tile), GraphicsUnit.Pixel);
g.Dispose();
string numStr = "" + num;
while (numStr.Length < 3) numStr = "0" + numStr;
string numStr2 = str + numStr;

bool bDraw = false;
for (int i = 0; i < tile; ++i)
{
for (int j = 0; j < tile; ++j)
{
Color c = resultImage.GetPixel(j, i);
if (c.R > 10 || c.G > 10 || c.B > 10)
{
bDraw = true;
}
}
}
if (bDraw && dictIgnore.ContainsKey(numStr))
{
resultImage.Save(firstPath + "mapres/" + numStr2 + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
else
{
result += "\"" + numStr2 + "\" : 1,\n";
}
++num;
}
}
resultImage.Dispose();

//输出缩略图
{
int newWidth = resW - hulvePixis3 * 2;
int newHeight = resH - hulvePixis - hulvePixis2;
int w, h;
double rate = 1;
if (newWidth > 1136 || newHeight > 640)
{
rate = (1.0 * newWidth / newHeight > 1.0 * 1136 / 640) ? (1.0 * newWidth / 1136) : (1.0 * newHeight / 640);
}
w = (int)(newWidth / rate);
h = (int)(newHeight / rate);

resultImage = new Bitmap(w, h);
Graphics g = Graphics.FromImage(resultImage);
g.Clear(Color.Black);
g.DrawImage(srcImage, new Rectangle(0, 0, w / 2, h / 2), new Rectangle(hulvePixis3, hulvePixis2, newWidth / 2, newHeight / 2), GraphicsUnit.Pixel);
g.DrawImage(srcImage, new Rectangle(0, h / 2, w / 2, h / 2), new Rectangle(hulvePixis3, newHeight / 2 + hulvePixis2, newWidth / 2, newHeight / 2), GraphicsUnit.Pixel);
g.DrawImage(srcImage, new Rectangle(w / 2, 0, w / 2, h / 2), new Rectangle(newWidth / 2 + hulvePixis3, hulvePixis2, newWidth / 2, newHeight / 2), GraphicsUnit.Pixel);
g.DrawImage(srcImage, new Rectangle(w / 2, h / 2, w / 2, h / 2), new Rectangle(newWidth / 2 + hulvePixis3, newHeight / 2 + hulvePixis2, newWidth / 2, newHeight / 2), GraphicsUnit.Pixel);
g.Dispose();
resultImage.Save(firstPath + "mmap/" + str + ".png", System.Drawing.Imaging.ImageFormat.Png);
resultImage.Dispose();
}
srcImage.Dispose();
}
}
}

posted on 2018-01-01 21:33  agchuanqi  阅读(189)  评论(0编辑  收藏  举报