示例#1
0
        public void ClearCache()
        {
            cachedTileMap.Clear();

            var invalidLayer = layers[(int)Layer.Invalid];

            var total = invalidLayer.Length;

            for (var i = 0; i < total; i++)
            {
                invalidLayer[i] = -1;
            }

            Invalidate();
        }
示例#2
0
        // TODO this should be a step in the exporter
        protected virtual void ConfigurePixelData()
        {
            tmpTextureData = new TextureData(width, height);
            tmpTextureData.Clear();

            currentStep++;
        }
示例#3
0
文件: FontChip.cs 项目: kissdesty/SDK
        /// <summary>
        ///     This method is responsible for converting a string of text into
        ///     TextureData. This is done by converting each character in the string
        ///     into an id that maps to the sprites stored in the FontChip. The ids
        ///     are based on ASCII values. The font class starts at ASCII
        ///     <paramref name="value" /> for an empty space and will scale up from
        ///     there based on how many sprites are in the FontChip. By default it
        ///     supports up to ASCII 128 but can support more as long as the sprites
        ///     exist.
        /// </summary>
        /// <param name="value">
        ///     The string to be converted into pixel data.
        /// </param>
        /// <param name="spriteChip"></param>
        /// <param name="fontMap"></param>
        /// <param name="textureData">
        ///     A reference to a <see cref="TextureData" /> class to store the pixel
        ///     data in.
        /// </param>
        /// <param name="stripEmptyLines">
        ///     Ignore empty lines in the supplied text. By default this is set to
        ///     false.
        /// </param>
        /// <param name="letterSpacing"></param>
        /// <param name="offset"></param>
        public void GenerateTextData(string value, SpriteChip spriteChip, int[] fontMap, TextureData textureData, bool stripEmptyLines = false, int letterSpacing = 0, int offset = 0)
        {
            // Strip out any tabs
            value = value.Replace("\t", "     ");

            var result = value.Split(new[] { "\n", "\r\n" },
                                     stripEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);

            Array.Reverse(result);

            var totalLines = result.Length;

            //spriteChip = this;

            var cWidth  = spriteChip.width;
            var cHeight = spriteChip.height;

            // index text
            var tWidth  = 0;
            var tHeight = totalLines;

            for (var i = 0; i < totalLines; i++)
            {
                tWidth = Math.Max(tWidth, result[i].Length);
            }

            var realWidth  = (cWidth + letterSpacing) * tWidth;
            var realHeight = cHeight * tHeight;

            if (textureData.width != realWidth || textureData.height != realHeight)
            {
                textureData.Resize(realWidth, realHeight);
            }

            textureData.Clear();

            // convert each line into a sprite id

            var charOffset = 32;

            var tmpData = new int[spriteChip.width * spriteChip.height];

            for (var i = 0; i < totalLines; i++)
            {
                var line       = result[i];
                var characters = line.Length;
                for (var j = 0; j < characters; j++)
                {
                    var character = line[j];
                    var spriteID  = Convert.ToInt32(character) - charOffset;

                    //Debug.Log("Char " + character + " " + spriteID);
                    spriteChip.ReadSpriteAt(fontMap[spriteID], tmpData);

                    textureData.MergePixels(j * (cWidth + letterSpacing), i * cHeight, cWidth, cHeight, tmpData);
                }
            }
        }
示例#4
0
 /// <summary>
 ///     Clears the <see cref="TextureData" /> to a default color index of -1
 ///     and also resets the cache. This removes all sprites from the chip.
 /// </summary>
 public void Clear()
 {
     cache          = new string[totalSprites];
     pixelDataCache = new int[totalSprites][];
     _texture.Clear();
 }
示例#5
0
        public void ExportGame(string path, IEngine engine, SaveFlags saveFlags)
        {
            Reset();

//            exporting = true;

            // Make sure that the texture factory has the current mask color if any images are going to be exported.
//            textureFactory.maskColor = new ColorData(engine.colorChip.maskColor);

//            Console.WriteLine("MASK COLOR " + engine.colorChip.maskColor);

//            Console.WriteLine("Export Game " + path);

            var watch = Stopwatch.StartNew();

            // Save the engine so we can work with it during loading
            targetEngine = engine;

            // Step 1. Load the system snapshot
            if ((saveFlags & SaveFlags.System) == SaveFlags.System)
            {
                AddExporter(new SystemExporter(path + "data.json", targetEngine));
            }

            // Step 2 (optional). Load up the Lua script
            if ((saveFlags & SaveFlags.Code) == SaveFlags.Code)
            {
//                //var scriptExtension = ".lua";
//
//                var paths = files.Keys.Where(s => textExtensions.Any(x => s.EndsWith(x))).ToList();
//
//                foreach (var fileName in paths)
//                {
//                    parser = LoadScript(fileName, files[fileName]);
//                    AddExporter(parser);
//                }
            }

            // Step 3 (optional). Look for new colors
            if ((saveFlags & SaveFlags.Colors) == SaveFlags.Colors)
            {
                var colorChip = targetEngine.colorChip;

                AddExporter(new ColorPaletteExporter(path + "colors.png", colorChip, new PNGWriter()));
            }

            // Step 4 (optional). Look for color map for sprites and tile map
            if ((saveFlags & SaveFlags.ColorMap) == SaveFlags.ColorMap)
            {
                if (targetEngine.GetChip(ColorMapParser.chipName, false) is ColorChip colorChip)
                {
                    AddExporter(new ColorPaletteExporter(path + "color-map.png", colorChip, new PNGWriter()));
                }
            }

            // Step 5 (optional). Look for new sprites
            if ((saveFlags & SaveFlags.Sprites) == SaveFlags.Sprites)
            {
//                Console.WriteLine("Export Sprite");

                var imageExporter = new PNGWriter();

                AddExporter(new SpriteExporter(path + "sprites.png", targetEngine, imageExporter));
//                var spriteChip = targetEngine.spriteChip;
//
//                var pixelData = spriteChip.texture.GetPixels();
//
//                //TODO need to crop the pixel data so we only save out what we need
//
//                AddExporter(new ImageExporter("sprite.cache.png", pixelData, spriteChip.textureWidth, spriteChip.textureHeight, targetEngine.colorChip.colors, textureFactory));
////
////
//                parser = LoadSprites(files);
//                if (parser != null)
//                    AddExporter(parser);
            }

            // Step 6 (optional). Look for tile map to load
//            if ((saveFlags & SaveFlags.Tilemap) == SaveFlags.Tilemap)
//            {
//                var imageExporter = new PNGWriter();
//
//                var tmp = new TilemapExporter(path + "tilemap.png", targetEngine, imageExporter);
//                AddExporter(tmp);
//            }
//
//            if ((saveFlags & SaveFlags.TilemapFlags) == SaveFlags.TilemapFlags)
//            {
//                var imageExporter = new PNGWriter();
//
//                AddExporter(new TilemapFlagExporter(path + "tilemap-flags.png", targetEngine, imageExporter));
//            }

//            if ((saveFlags & SaveFlags.FlagColors) == SaveFlags.FlagColors)
//            {
//                AddExporter(new FlagColorExporter(path + "flags.png", targetEngine, textureFactory));
//                //AddExporter(new FlagTileExporter(path + "flags.png", targetEngine, textureFactory));
//            }

//            if ((saveFlags & SaveFlags.TileColorOffset) == SaveFlags.TileColorOffset)
//            {
//                AddExporter(new TileColorOffsetExporter(path + "tile-color-offsets.json", targetEngine));
//            }

//
            // Step 7 (optional). Look for fonts to load
            if ((saveFlags & SaveFlags.Fonts) == SaveFlags.Fonts)
            {
                var fontChip       = targetEngine.fontChip;
                var spriteChip     = targetEngine.spriteChip;
                var tmpTextureData = new TextureData(96, 64);

                var fonts = fontChip.fonts;

                foreach (var font in fonts)
                {
                    var name    = font.Key;
                    var sprites = font.Value;

                    // Clear the texture
                    tmpTextureData.Clear();

                    // Loop through all the characters and copy their texture data over
                    var total = sprites.Length;
                    for (var i = 0; i < total; i++)
                    {
                    }
                }


//                var fontExtension = ".font.png";
//
//                var paths = files.Keys.Where(s => s.EndsWith(fontExtension)).ToArray();
//
//                foreach (var fileName in paths)
//                {
//                    var fontName = fileName.Split('.')[0];
//
//                    parser = LoadFont(fontName, files[fileName]);
//                    if (parser != null)
//                        AddExporter(parser);
//                }
            }
//


//                if ((saveFlags & SaveFlags.TilemapFlags) == SaveFlags.TilemapFlags)
//                {
////                    ExportUtil.CreateTileMapFlagTexture(ref tex, engine);
////                    SaveTextureToFile(path, "tilemap-flags", tex);
//                }
//
            if ((saveFlags & SaveFlags.Tilemap) == SaveFlags.Tilemap)
            {
                AddExporter(new TilemapJsonExporter(path + "tilemap.json", targetEngine));
            }


            // Step 8 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Meta) == SaveFlags.Meta)
            {
                AddExporter(new MetadataExporter(path + "info.json", targetEngine));
            }

            // Step 9 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Sounds) == SaveFlags.Sounds)
            {
                AddExporter(new SoundExporter(path + "sounds.json", targetEngine));
            }

            // Step 10 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Music) == SaveFlags.Music)
            {
                AddExporter(new MusicExporter(path + "music.json", targetEngine));
            }

            // Step 11 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.SaveData) == SaveFlags.SaveData)
            {
                AddExporter(new SavedDataExporter(path + "saves.json", targetEngine));
            }

            totalParsers    = exporters.Count;
            currentParserID = 0;

            watch.Stop();

//            UnityEngine.Debug.Log("Game Exporter Setup Time - " + watch.ElapsedMilliseconds);
        }
示例#6
0
        public virtual void ConfigureTilemap()
        {
            var tilemapChip = target.tilemapChip;

            if (data.ContainsKey("layers"))
            {
                var layers   = data["layers"] as List <object>;
                var tileSets = data["tilesets"] as List <object>;

                var total = layers.Count;

                for (int i = 0; i < total; i++)
                {
                    var layer   = layers[i] as Dictionary <string, object>;
                    var tileSet = tileSets[i] as Dictionary <string, object>;

                    try
                    {
                        var layerType = (TilemapChip.Layer)Enum.Parse(typeof(TilemapChip.Layer), ((string)layer["name"]));

                        var offset = (int)(long)tileSet["firstgid"];

                        var columns = (int)(long)layer["width"];
                        var rows    = (int)(long)layer["height"];

                        var rawLayerData = layer["data"] as List <object>;

                        int[] dataValues = rawLayerData.ConvertAll(x => ((int)(long)x) - offset < -1 ? -1 : ((int)(long)x) - offset).ToArray();

                        if (columns != tilemapChip.columns || rows > tilemapChip.rows)
                        {
                            // Create texture data that matches the memory of the tilemap chip
                            var tmpPixelData = new TextureData(tilemapChip.columns, tilemapChip.rows);
                            tmpPixelData.Clear();

                            var jsonData = new TextureData(columns, rows);
                            jsonData.Clear();
                            jsonData.SetPixels(0, 0, columns, rows, dataValues);

                            var tmpCol = columns > tilemapChip.columns ? tilemapChip.columns : columns;
                            var tmpRow = rows > tilemapChip.rows ? tilemapChip.rows : rows;

                            if (tmpCol > columns)
                            {
                                tmpCol = columns;
                            }

                            if (tmpRow > rows)
                            {
                                tmpRow = rows;
                            }

                            var tmpData = new int[tmpCol * tmpRow];

                            jsonData.CopyPixels(ref tmpData, 0, 0, tmpCol, tmpRow);

                            tmpPixelData.SetPixels(0, 0, tmpCol, tmpRow, tmpData);

                            tmpPixelData.CopyPixels(ref dataValues, 0, 0, tmpPixelData.width, tmpPixelData.height);

//							var jsonMap = new TextureData(columns, rows);
//							jsonMap.SetPixels(0, 0, columns, rows, dataValues);
//
//
//							Debug.Log("Resize " + tilemapChip.columns +"x"+tilemapChip.rows + " " + columns + "x"+rows);
//
//							var tmpPixelData = new TextureData(columns, rows);
//							tmpPixelData.Clear();
//
//							var totalData = dataValues.Length;
//
//							for (int j = 0; j < totalData; j++)
//							{
//								var pos = target.gameChip.CalculatePosition(j, columns);
//
//
//
//
//
//							}
//							tmpPixelData.SetPixels(0, 0, columns, rows, dataValues);
//
//							Array.Resize(ref dataValues, tilemapChip.total);
//
                            tmpPixelData.CopyPixels(ref dataValues, 0, 0, tilemapChip.columns, tilemapChip.rows);
                        }

                        Array.Copy(dataValues, tilemapChip.layers[(int)layerType], dataValues.Length);

                        // TODO need to make sure that the layer is the same size as the display chip

                        // TODO copy the tilemap data over to layer correctly
                    }
                    catch (Exception e)
                    {
                        // Just igonre any layers that don't exist
                        throw new Exception("Unable to parse 'tilemap.json' file. It may be corrupt. Try deleting it and creating a new one.");
                    }
                }
            }

            currentStep++;
        }
示例#7
0
        public virtual void ConfigureTilemapV1()
        {
            var tilemapChip = target.TilemapChip;


            if (Data.ContainsKey("layers"))
            {
                var layers   = Data["layers"] as List <object>;
                var tileSets = Data["tilesets"] as List <object>;

                var total = layers.Count;

                for (var i = 0; i < total; i++)
                {
                    try
                    {
                        var layer = layers[i] as Dictionary <string, object>;

                        var layerType = (string)layer["type"];

                        if (layerType == "tilelayer")
                        {
                            var tileSet = tileSets[i] as Dictionary <string, object>;


                            var offset = (int)(long)tileSet["firstgid"];

                            var columns = (int)(long)layer["width"];
                            var rows    = (int)(long)layer["height"];

                            var rawLayerData = layer["data"] as List <object>;

                            var dataValues = rawLayerData
                                             .Select(x => (int)(long)x - offset < -1 ? -1 : (int)(long)x - offset).ToArray();

                            if (columns != tilemapChip.columns || rows > tilemapChip.rows)
                            {
                                // Create texture data that matches the memory of the tilemap chip
                                var tmpPixelData = new TextureData(tilemapChip.columns, tilemapChip.rows);
                                tmpPixelData.Clear();

                                var jsonData = new TextureData(columns, rows);
                                jsonData.Clear();
                                jsonData.SetPixels(0, 0, columns, rows, dataValues);

                                var tmpCol = columns > tilemapChip.columns ? tilemapChip.columns : columns;
                                var tmpRow = rows > tilemapChip.rows ? tilemapChip.rows : rows;

                                if (tmpCol > columns)
                                {
                                    tmpCol = columns;
                                }

                                if (tmpRow > rows)
                                {
                                    tmpRow = rows;
                                }

                                var tmpData = new int[tmpCol * tmpRow];

                                jsonData.CopyPixels(ref tmpData, 0, 0, tmpCol, tmpRow);

                                tmpPixelData.SetPixels(0, 0, tmpCol, tmpRow, tmpData);

                                tmpPixelData.CopyPixels(ref dataValues, 0, 0, tmpPixelData.width, tmpPixelData.height);

                                tmpPixelData.CopyPixels(ref dataValues, 0, 0, tilemapChip.columns, tilemapChip.rows);
                            }


                            for (var j = 0; j < tilemapChip.total; j++)
                            {
                                var tile = tilemapChip.tiles[j];

                                if ((string)layer["name"] == "Sprites")
                                {
                                    tile.spriteID = dataValues[j];
                                }
                                else if ((string)layer["name"] == "Flags")
                                {
                                    tile.flag = dataValues[j];
                                }

                                // tile.Invalidate();
                            }
                        }
                        else if (layerType == "objectgroup")
                        {
                            var objects = layer["objects"] as List <object>;

                            var totalTiles = objects.Count;

                            for (var j = 0; j < totalTiles; j++)
                            {
                                var tileObject = objects[j] as Dictionary <string, object>;

                                var column = (int)Math.Floor((float)(long)tileObject["x"] / 8);
                                var row    = (int)Math.Floor((float)(long)tileObject["y"] / 8) - 1;

                                var tile = tilemapChip.GetTile(column, row);

                                var gid = (uint)(long)tileObject["gid"];

                                var idMask = (1 << 30) - 1;

                                tile.spriteID = (int)(gid & idMask) - 1;

                                var hMask = 1 << 31;

                                tile.flipH = (hMask & gid) != 0;

                                var vMask = 1 << 30;

                                tile.flipV = (vMask & gid) != 0;

                                var properties = tileObject["properties"] as List <object>;

                                //								int flagID = -1;
                                //								int colorOffset = 0;

                                for (var k = 0; k < properties.Count; k++)
                                {
                                    var prop = properties[k] as Dictionary <string, object>;

                                    var propName = (string)prop["name"];

                                    if (propName == "flagID")
                                    {
                                        tile.flag = (int)(long)prop["value"];
                                    }
                                    else if (propName == "colorOffset")
                                    {
                                        tile.colorOffset = (int)(long)prop["value"];
                                    }
                                }

                                // tile.Invalidate();
                            }
                        }

                        tilemapChip.Invalidate();
                        // TODO need to make sure that the layer is the same size as the display chip

                        // TODO copy the tilemap data over to layer correctly
                    }
                    catch
                    {
                        // Just ignore any layers that don't exist
                        throw new Exception(
                                  "Unable to parse 'tilemap.json' file. It may be corrupt. Try deleting it and creating a new one.");
                    }
                }
            }

            StepCompleted();
        }
示例#8
0
 public void ClearUILayer()
 {
     // TODO this is immediate and needs to be part of the draw call stack
     uiLayer.Clear();
 }
示例#9
0
        public void ExportGame(string path, IEngine engine, SaveFlags saveFlags, bool useSteps = true)
        {
            Clear();

            // Save the engine so we can work with it during loading
            targetEngine = engine;

            // Step 1. Load the system snapshot
            if ((saveFlags & SaveFlags.System) == SaveFlags.System)
            {
                AddExporter(new SystemExporter(path + "data.json", targetEngine));
            }

            // Step 3 (optional). Look for new colors
            if ((saveFlags & SaveFlags.Colors) == SaveFlags.Colors)
            {
                var colorChip = targetEngine.ColorChip;

                AddExporter(new ColorPaletteExporter(path + "colors.png", colorChip, new PNGWriter()));
            }

            // Step 4 (optional). Look for color map for sprites and tile map
            if ((saveFlags & SaveFlags.ColorMap) == SaveFlags.ColorMap)
            {
                if (targetEngine.GetChip(ColorMapParser.chipName, false) is ColorChip colorChip)
                {
                    AddExporter(new ColorPaletteExporter(path + "color-map.png", colorChip, new PNGWriter()));
                }
            }

            // Step 5 (optional). Look for new sprites
            if ((saveFlags & SaveFlags.Sprites) == SaveFlags.Sprites)
            {
                //                Console.WriteLine("Export Sprite");

                var imageExporter = new PNGWriter();

                AddExporter(new SpriteExporter(path + "sprites.png", targetEngine, imageExporter));
            }

            // Step 7 (optional). Look for fonts to load
            if ((saveFlags & SaveFlags.Fonts) == SaveFlags.Fonts)
            {
                var fontChip       = targetEngine.FontChip;
                var spriteChip     = targetEngine.SpriteChip;
                var tmpTextureData = new TextureData(96, 64);

                var fonts = fontChip.fonts;

                foreach (var font in fonts)
                {
                    var name    = font.Key;
                    var sprites = font.Value;

                    // Clear the texture
                    tmpTextureData.Clear();

                    // Loop through all the characters and copy their texture data over
                    var total = sprites.Length;
                    for (var i = 0; i < total; i++)
                    {
                    }
                }
            }

            if ((saveFlags & SaveFlags.Tilemap) == SaveFlags.Tilemap)
            {
                AddExporter(new TilemapJsonExporter(path + "tilemap.json", targetEngine));
            }

            // Step 8 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Meta) == SaveFlags.Meta)
            {
                AddExporter(new MetadataExporter(path + "info.json", targetEngine));
            }

            // Step 9 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Sounds) == SaveFlags.Sounds)
            {
                AddExporter(new SoundExporter(path + "sounds.json", targetEngine));
            }

            // Step 10 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.Music) == SaveFlags.Music)
            {
                AddExporter(new MusicExporter(path + "music.json", targetEngine));
            }

            // Step 11 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.SaveData) == SaveFlags.SaveData)
            {
                AddExporter(new SavedDataExporter(path + "saves.json", targetEngine));
            }

            // Step 11 (optional). Look for meta data and override the game
            if ((saveFlags & SaveFlags.MetaSprites) == SaveFlags.MetaSprites)
            {
                AddExporter(new MetaSpriteExporter(path + "meta-sprites.json", targetEngine));
            }

            StartExport(useSteps);
        }