Пример #1
0
        private void checkFiles()
        {
            var saveFolder = new DirectoryInfo(this.saveFolderPath);
            var saveFiles  = new Dictionary <SavedGameInfo, DateTime>();
            var saveNames  = new HashSet <string>();

            if (!saveFolder.Exists)
            {
                this.nextSaveNumber = 1;
                this.games          = new LinkedList <SavedGameInfo>();
                return;
            }

            foreach (var file in saveFolder.EnumerateFiles("*." + SaveNameExtension))
            {
                saveNames.Add(file.Name);

#if DEBUG
                if (file.Length == 0)
                {
                    System.Diagnostics.Trace.TraceWarning("Empty save file: " + file.Name);
                    continue;
                }
#endif

                using (var parser = new IkonParser(file.OpenText()))
                {
#if !DEBUG
                    try
                    {
#endif
                    var versionData = parser.ParseNext() as IkonArray;
                    var title       = parser.ParseNext().To <string>();
                    var previewData = parser.ParseNext() as IkonBaseObject;
                    saveFiles.Add(
                        new SavedGameInfo(
                            title,
                            previewData,
                            file
                            ),
                        file.LastWriteTimeUtc
                        );
#if !DEBUG
                }
                catch (IOException)
                {
                    //TODO(later) Notify about corrupted save file
                }
#endif
                }
            }

            for (this.nextSaveNumber = 1;
                 saveNames.Contains(SaveNamePrefix + this.nextSaveNumber + "." + SaveNameExtension);
                 this.nextSaveNumber++)
            {
                ;
            }
            this.games = new LinkedList <SavedGameInfo>(saveFiles.OrderByDescending(x => x.Value).Select(x => x.Key));
        }
Пример #2
0
        public static void ColorLoader(IEnumerable <TracableStream> dataSources)
        {
            var colorList = new List <Color>();

            foreach (var source in dataSources)
            {
                var       parser = new IkonParser(source.Stream);
                IkonArray colorsData;

                try
                {
                    colorsData = parser.ParseNext().To <IkonComposite>()[ColorsKey].To <IkonArray>();
                }
                catch (IOException e)
                {
                    throw new IOException(source.SourceInfo, e);
                }
                catch (FormatException e)
                {
                    throw new FormatException(source.SourceInfo, e);
                }

                foreach (var item in colorsData)
                {
                    var colorData = item.To <IkonArray>();
                    colorList.Add(Color.FromArgb(
                                      colorData[0].To <int>(),
                                      colorData[1].To <int>(),
                                      colorData[2].To <int>()
                                      ));
                }
            }

            Colors = colorList.ToArray();
        }
Пример #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 void checkFiles()
        {
            var saveFolder = new DirectoryInfo(this.saveFolderPath);
            var saveFiles  = new Dictionary <SavedGameInfo, DateTime>();
            var saveNames  = new HashSet <string>();

            if (!saveFolder.Exists)
            {
                this.nextSaveNumber = 1;
                this.games          = new LinkedList <SavedGameInfo>();
                return;
            }

            foreach (var file in saveFolder.EnumerateFiles("*." + SaveNameExtension))
            {
                saveNames.Add(file.Name);

                using (var parser = new IkonParser(file.OpenText())) {
#if !DEBUG
                    try
                    {
#endif
                    var rawData = parser.ParseNext() as IkonComposite;
                    saveFiles.Add(
                        new SavedGameInfo(
                            rawData[SaveGameTitleKey].To <string>(),
                            rawData[MainGame.TurnKey].To <int>(),
                            rawData,
                            file
                            ),
                        file.LastWriteTimeUtc
                        );
#if !DEBUG
                }
                catch (IOException)
                {
                    //TODO(later) Notify about corrupted save file
                }
#endif
                }
            }

            for (this.nextSaveNumber = 1;
                 saveNames.Contains(SaveNamePrefix + this.nextSaveNumber + "." + SaveNameExtension);
                 this.nextSaveNumber++)
            {
                ;
            }
            this.games = new LinkedList <SavedGameInfo>(saveFiles.OrderByDescending(x => x.Value).Select(x => x.Key));
        }
Пример #5
0
        public static void ColorLoader(IEnumerable <NamedStream> dataSources)
        {
            var colorList = new List <Color>();

            using (var parser = new IkonParser(dataSources))
                foreach (var item in parser.ParseNext().To <IkonComposite>()[ColorsKey].To <IkonArray>())
                {
                    var colorData = item.To <IkonArray>();
                    colorList.Add(Color.FromArgb(
                                      colorData[0].To <int>(),
                                      colorData[1].To <int>(),
                                      colorData[2].To <int>()
                                      ));
                }

            Colors = Array.AsReadOnly(colorList.ToArray());
        }
Пример #6
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);
        }