public static void BuildResources(AtlasProject atlasProj) { //1. load resouce //2. convert buffer to C# code List <ConvertedFile> selectedItems = new List <ConvertedFile>(); foreach (AtlasItemSourceFile f in atlasProj.Items) { if (f.IsConfig) { continue; } //------ //load and convert ConvertedFile convertedFile = new ConvertedFile(); convertedFile.Compression = BinCompression.Deflate; convertedFile.itemSourceFile = f; convertedFile.Data = BinToHexUtils.ReadBinaryAndConvertToHexArr(convertedFile, convertedFile.Compression); string url2 = f.Link.Replace("\\", "_"); url2 = url2.Replace("//", "_"); url2 = url2.Replace(".", "_"); convertedFile.Name = url2;//field name selectedItems.Add(convertedFile); } BuildCsSource(atlasProj, selectedItems); }
public static void BuildFontAtlas(AtlasProject atlasProj) { foreach (AtlasItemSourceFile atlasSourceFile in atlasProj.Items) { if (atlasSourceFile.Kind == AtlasItemSourceKind.FontAtlasConfig && atlasSourceFile.FontBuilderConfig != null) { FontBuilderConfig config = atlasSourceFile.FontBuilderConfig; foreach (FontBuilderTask builderTask in config.BuilderTasks) { //1. create glyph-texture-bitmap generator var glyphTextureGen = new GlyphTextureBitmapGenerator(); glyphTextureGen.SetSvgBmpBuilderFunc(SvgBuilderHelper.ParseAndRenderSvg); //2. generate the glyphs if (builderTask.TextureKind == TextureKind.Msdf) { glyphTextureGen.MsdfGenVersion = 3; } Typography.OpenFont.Typeface typeface = atlasProj.GetTypeface(config.FontFilename); //TODO: add other font styles RequestFont reqFont = new RequestFont(typeface.Name, builderTask.Size, FontStyle.Regular); string textureName = typeface.Name.ToLower() + "_" + reqFont.FontKey; string outputDir = Path.GetDirectoryName(atlasProj.OutputFilename); FontAtlasBuilderHelper builderHelper = new FontAtlasBuilderHelper(); builderHelper.TextureInfoFilename = outputDir + Path.DirectorySeparatorChar + textureName; builderHelper.OutputImgFilename = outputDir + Path.DirectorySeparatorChar + textureName + ".png"; builderHelper.Build(glyphTextureGen, typeface, builderTask.Size, builderTask.TextureKind, builderTask.TextureBuildDetails.ToArray(), reqFont.FontKey ); } } } }
private void LstProjectList_SelectedIndexChanged(object sender, EventArgs e) { if (lstProjectList.SelectedItem is AtlasProject atlasProj) { _selectedAtlasItemSourceFile = null;//reset _currentAtlasProj = atlasProj; //read project detail //extract only interested files //eg. image files if (!atlasProj.Isloaded) { //load content atlasProj.LoadProjectDetail(); } //show detail listBox1.Items.Clear(); foreach (AtlasItemSourceFile file in atlasProj.Items) { listBox1.Items.Add(file); } } }
static void BuildCsSource(AtlasProject atlasProj, List <ConvertedFile> selectedItems) { //7. create an atlas file in a source file version, user can embed the source to file //easy, just read .info and .png then convert to binary buffer //PART1: string timestamp = DateTime.Now.ToString("s"); { StringBuilder outputFile = new StringBuilder(); outputFile.AppendLine("//AUTOGEN, " + timestamp); outputFile.AppendLine("//source: " + atlasProj.FullFilename); outputFile.AppendLine("//tools: " + System.Windows.Forms.Application.ExecutablePath); string onlyFilename = Path.GetFileNameWithoutExtension(atlasProj.Filename); //TODO: config this outputFile.AppendLine("namespace " + atlasProj.CsSourceNamespace + "{"); outputFile.AppendLine("public partial class RawResourceData{"); foreach (ConvertedFile f in selectedItems) { if (f.Compression != BinCompression.None) { outputFile.AppendLine("///<summary>"); outputFile.AppendLine("///compression: " + f.Compression + ",org_file_length=" + f.OriginalFileLength + ", compressed_length=" + f.CompressedFileLength); outputFile.AppendLine("///</summary>"); } outputFile.AppendLine("public readonly byte[] " + f.Name + "=" + f.Data.ToString() + ";"); } outputFile.AppendLine("}"); //class outputFile.AppendLine("}"); //namespace string dirname = Path.GetDirectoryName(atlasProj.OutputFilename); File.WriteAllText(dirname + Path.DirectorySeparatorChar + "x_" + onlyFilename + "_Resource_AUTOGEN.cs", outputFile.ToString()); } }
static void BuildAtlasInEmbededSourceVersion(AtlasProject atlasProj, string info, string img, Dictionary <string, ushort> imgUrlDic) { //7. create an atlas file in a source file version, user can embed the source to file //easy, just read .info and .png then convert to binary buffer //PART1: string timestamp = DateTime.Now.ToString("s"); { StringBuilder outputFile = new StringBuilder(); outputFile.AppendLine("//AUTOGEN, " + timestamp); outputFile.AppendLine("//source: " + atlasProj.FullFilename); outputFile.AppendLine("//tools: " + System.Windows.Forms.Application.ExecutablePath); string onlyFilename = Path.GetFileNameWithoutExtension(atlasProj.Filename); //TODO: config this outputFile.AppendLine("namespace " + atlasProj.CsSourceNamespace + "{"); outputFile.AppendLine("public partial class RawAtlasData{"); outputFile.AppendLine("public static readonly string NAME=\"" + onlyFilename + "\";"); outputFile.AppendLine("//img_links:"); foreach (string url in imgUrlDic.Keys) { outputFile.AppendLine("// " + url); } outputFile.AppendLine(""); outputFile.AppendLine("//items names"); outputFile.AppendLine("public static class Names{"); foreach (string url in imgUrlDic.Keys) { string url2 = url.Replace("\\", "_"); url2 = url2.Replace("//", "_"); url2 = url2.Replace(".", "_"); outputFile.AppendLine("public const string " + url2 + "=\"" + url + "\";"); } outputFile.AppendLine("}"); StringBuilder info_sb = BinToHexUtils.ReadBinaryAndConvertToHexArr(info, BinCompression.None); StringBuilder img_sb = BinToHexUtils.ReadBinaryAndConvertToHexArr(img, BinCompression.None); outputFile.AppendLine("//bitmap_atlas_info"); outputFile.AppendLine("//" + info); outputFile.AppendLine("public static readonly byte[] info=" + info_sb.ToString() + ";"); outputFile.AppendLine("//bitmap_atlas_total_img"); outputFile.AppendLine("//" + img); outputFile.AppendLine("public static readonly byte[] img=" + img_sb.ToString() + ";"); outputFile.AppendLine("}"); outputFile.AppendLine("}"); string dirname = Path.GetDirectoryName(atlasProj.OutputFilename); File.WriteAllText(dirname + Path.DirectorySeparatorChar + "x_" + onlyFilename + "_Atlas_AUTOGEN.cs", outputFile.ToString()); } //---------------------- //PART2: autogen atlas binder { StringBuilder outputFile = new StringBuilder(); outputFile.AppendLine("//AUTOGEN, " + timestamp); outputFile.AppendLine("//source: " + atlasProj.FullFilename); outputFile.AppendLine("//tools: " + System.Windows.Forms.Application.ExecutablePath); string onlyFilename = Path.GetFileNameWithoutExtension(atlasProj.Filename); outputFile.AppendLine("using PixelFarm.Drawing;"); outputFile.AppendLine("namespace " + atlasProj.CsSourceNamespace + "{"); outputFile.AppendLine("public partial class Binders{"); foreach (string url in imgUrlDic.Keys) { string url2 = url.Replace("\\", "_"); url2 = url2.Replace("//", "_"); url2 = url2.Replace(".", "_"); outputFile.AppendLine("public readonly AtlasImageBinder " + url2 + "=new AtlasImageBinder(RawAtlasData.NAME, \"" + url + "\");"); } outputFile.AppendLine(@" static bool s_registered; public Binders(){ if(!s_registered){ try{ PixelFarm.Platforms.InMemStorage.AddData(RawAtlasData.NAME + "".info"", RawAtlasData.info); PixelFarm.Platforms.InMemStorage.AddData(RawAtlasData.NAME + "".png"", RawAtlasData.img); }catch(System.Exception ex){ } s_registered= true; } } "); outputFile.AppendLine("}"); //class outputFile.AppendLine("}"); //namespace string dirname = Path.GetDirectoryName(atlasProj.OutputFilename); File.WriteAllText(dirname + Path.DirectorySeparatorChar + "x_" + onlyFilename + "_Atlas_AUTOGEN_BINDERS.cs", outputFile.ToString()); } }
public static void BuildBitmapAtlas(AtlasProject atlasProj, Func <string, MemBitmap> imgLoader, bool test_extract = false) { //demonstrate how to build a bitmap atlas List <AtlasItemSourceFile> fileList = atlasProj.Items; //1. create builder var bmpAtlasBuilder = new SimpleBitmapAtlasBuilder(); ushort index = 0; Dictionary <string, ushort> imgDic = new Dictionary <string, ushort>(); foreach (AtlasItemSourceFile f in fileList) { if (f.Kind != AtlasItemSourceKind.Image) { continue; } //3. load a bitmap BitmapAtlasItemSource atlasItem = null; using (MemBitmap itemBmp = imgLoader(f.AbsoluteFilename)) { //4. get information about it atlasItem = new BitmapAtlasItemSource(itemBmp.Width, itemBmp.Height); atlasItem.SetImageBuffer(MemBitmap.CopyImgBuffer(itemBmp)); } atlasItem.UniqueInt16Name = index; //5. add to builder bmpAtlasBuilder.AddItemSource(atlasItem); //get relative filename string imgPath = "//" + f.Link; imgDic.Add(imgPath, index); index++; //------------ #if DEBUG if (index >= ushort.MaxValue) { throw new NotSupportedException(); } #endif //------------ } if (imgDic.Count == 0) { //no file return; } string atlasInfoFile = atlasProj.OutputFilename + ".info"; string totalImgFile = atlasProj.OutputFilename + ".png"; //5. merge all small images into a bigone using (MemBitmap totalImg = bmpAtlasBuilder.BuildSingleImage(false)) { bmpAtlasBuilder.ImgUrlDict = imgDic; bmpAtlasBuilder.SetAtlasInfo(TextureKind.Bitmap, 0); //font size //6. save atlas info and total-img (.png file) bmpAtlasBuilder.SaveAtlasInfo(atlasInfoFile); totalImg.SaveImage(totalImgFile); } //---------------------- //7. create an atlas file in a source file version, user can embed the source to file //easy, just read .info and .png then convert to binary buffer BuildAtlasInEmbededSourceVersion(atlasProj, atlasInfoFile, totalImgFile, imgDic); //---------------------- //test, read data back //---------------------- if (test_extract) { bmpAtlasBuilder = new SimpleBitmapAtlasBuilder(); SimpleBitmapAtlas bitmapAtlas = bmpAtlasBuilder.LoadAtlasInfo(atlasInfoFile)[0]; // MemBitmap totalAtlasImg = imgLoader(totalImgFile); bitmapAtlas.SetMainBitmap(imgLoader(totalImgFile), true); //----- for (int i = 0; i < index; ++i) { if (bitmapAtlas.TryGetItem((ushort)i, out AtlasItem bmpMapData)) { //test copy data from bitmap MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height); itemImg.SaveImage("test1_atlas_item" + i + ".png"); } } //test, { if (bitmapAtlas.TryGetItem(@"\chk_checked.png", out AtlasItem bmpMapData)) { MemBitmap itemImg = totalAtlasImg.CopyImgBuffer(bmpMapData.Left, bmpMapData.Top, bmpMapData.Width, bmpMapData.Height); itemImg.SaveImage("test1_atlas_item_a.png"); } } } }