예제 #1
0
 public CharacterRasterDrawer(AtlasBuilder atlas, Bitmap texture, Font font)
 {
     this.atlas  = atlas;
     this.font   = font;
     this.canvas = Graphics.FromImage(texture);
     this.canvas.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
     this.brush = new SolidBrush(Color.White);
 }
예제 #2
0
        public CharacterSdfDrawer(AtlasBuilder atlas, Bitmap texture, Font font)
        {
            this.atlas      = atlas;
            this.font       = font;
            this.texture    = texture;
            this.fakeBitmap = new Bitmap(1, 1);
            this.fakeCanvas = Graphics.FromImage(this.fakeBitmap);

            this.bmpData = texture.LockBits(new Rectangle(0, 0, texture.Width, texture.Height), ImageLockMode.ReadWrite, texture.PixelFormat);
            this.bmpPtr  = this.bmpData.Scan0;
        }
예제 #3
0
        private IkonComposite loadAtlas()
        {
            var atlasFile    = new FileInfo(AtlasImagePath);
            var metadataFile = new FileInfo(AtlasIkonPath);
            var rootFolder   = SettingsWinforms.Get.DataRootPath ?? "";
            var extraSprites = new DirectoryInfo(rootFolder + SpritesPath).
                               GetFiles().
                               Where(x => x.Name != atlasFile.Name && x.Name != metadataFile.Name).
                               ToList();

            IkonComposite ikonData;

            using (var ikonParser = new IkonParser(new StreamReader(rootFolder + AtlasIkonPath)))
                ikonData = ikonParser.ParseNext(AtlasTag).To <IkonComposite>();

            using (var atlasImage = new Bitmap(rootFolder + AtlasImagePath))
            {
                if (extraSprites.Any())
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Adding loose sprites to atlas: " + string.Join(" ", extraSprites.Select(x => x.Name)));
#endif
                    var atlasBuilder = new AtlasBuilder(ikonData, SpriteMargin, atlasImage.Size);
                    using (Graphics g = Graphics.FromImage(atlasImage))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                        g.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.None;

                        foreach (var spriteFile in extraSprites)
                        {
                            using (var sprite = new Bitmap(spriteFile.FullName))
                            {
                                var spriteRegion = atlasBuilder.Add(sprite.Size);
                                g.DrawImage(sprite, spriteRegion);

                                var textureCoords = new IkonArray
                                {
                                    serializeSpriteCorner(spriteRegion.Left, spriteRegion.Top, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Right, spriteRegion.Top, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Right, spriteRegion.Bottom, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Left, spriteRegion.Bottom, atlasImage.Size)
                                };
                                ikonData.Add(Path.GetFileNameWithoutExtension(spriteFile.Name), textureCoords);
                            }
                        }
                    }
                }
                this.textureId = TextureUtils.CreateTexture(atlasImage);
                this.Size      = new Vector2(atlasImage.Width, atlasImage.Height);
            }

            return(ikonData);
        }
예제 #4
0
        private IkonComposite loadAtlas()
        {
            var atlasFile    = new FileInfo(AtlasImagePath);
            var metadataFile = new FileInfo(AtlasIkonPath);
            var rootFolder   = SettingsWinforms.Get.DataRootPath ?? "";
            var extraSprites = new DirectoryInfo(rootFolder + SpritesPath).
                               GetFiles().
                               Where(x => x.Name != atlasFile.Name && x.Name != metadataFile.Name).
                               ToList();

            IkonComposite ikonData;

            using (var ikonParser = new IkonParser(new StreamReader(rootFolder + AtlasIkonPath)))
                ikonData = ikonParser.ParseNext(AtlasTag).To <IkonComposite>();

            using (var atlasImage = new Bitmap(rootFolder + AtlasImagePath))
            {
                if (extraSprites.Any())
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Adding loose sprites to atlas: " + string.Join(" ", extraSprites.Select(x => x.Name)));
#endif
                    var atlasBuilder = new AtlasBuilder(ikonData, SpriteMargin, atlasImage.Size);
                    using (Graphics g = Graphics.FromImage(atlasImage))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                        g.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.None;

                        foreach (var spriteFile in extraSprites)
                        {
                            using (var sprite = new Bitmap(spriteFile.FullName))
                            {
                                var spriteRegion = atlasBuilder.Add(sprite.Size);
                                g.DrawImage(sprite, spriteRegion);

                                var textureCoords = new IkonArray
                                {
                                    serializeSpriteCorner(spriteRegion.Left, spriteRegion.Top, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Right, spriteRegion.Top, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Right, spriteRegion.Bottom, atlasImage.Size),
                                    serializeSpriteCorner(spriteRegion.Left, spriteRegion.Bottom, atlasImage.Size)
                                };
                                ikonData.Add(Path.GetFileNameWithoutExtension(spriteFile.Name), textureCoords);
                            }
                        }
                    }
                }

                //TODO(later) move to atlas generator
                var atlasData = new ColorMap(atlasImage);
                for (int y = 0; y < atlasData.Height; y++)
                {
                    for (int x = 0; x < atlasData.Width; x++)
                    {
                        if (atlasData[x, y].A == 0)
                        {
                            var colorSum = new Vector4();
                            var alphaSum = 0f;
                            foreach (var color in atlasData.Subregion(x - 1, y - 1, x + 1, y + 1))
                            {
                                colorSum += new Vector4(color.R, color.G, color.B, 1) * color.A;
                                alphaSum += color.A;
                            }

                            if (alphaSum > 0)
                            {
                                colorSum       /= alphaSum;
                                atlasData[x, y] = new Color4(colorSum.X, colorSum.Y, colorSum.Z, 0);
                            }
                        }
                    }
                }

                this.textureId = TextureUtils.CreateTexture(atlasData);
                this.Size      = new Vector2(atlasImage.Width, atlasImage.Height);
            }

            return(ikonData);
        }