示例#1
0
//        private readonly int totalSpritePixels = 64;

        /// <summary>
        ///     Returns an array of ints that represent a sprite. Each
        ///     int should be mapped to a color
        ///     <paramref name="index" /> to display in the renderer.
        /// </summary>
        /// <param name="index">
        ///     Anint representing the location in memory of the
        ///     sprite.
        /// </param>
        /// <param name="pixelData"></param>
        /// <returns>
        /// </returns>
        public void ReadSpriteAt(int index, int[] pixelData)
        {
            if (index == -1)
            {
                var size = width * height;

                if (pixelData.Length < size)
                {
                    Array.Resize(ref pixelData, size);
                }

                Array.Copy(emptySpriteData, pixelData, size);
            }
            else
            {
                width1 = _texture.width;
//                height1 = _texture.height;

                w = width1 / width;

                tmpX = index % w * width;
                tmpY = index / w * height;

                // Flip y for Unity
                //            tmpY = height1 - tmpY - height;

                _texture.CopyPixels(ref pixelData, tmpX, tmpY, width, height);
            }
        }
        public virtual void ConfigureTilemap()
        {
            var tilemapChip = target.tilemapChip;

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

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

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

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

                if (tilemapChip.columns != columns || tilemapChip.rows != rows)
                {
                    var tmpPixelData = new TextureData(columns, rows);
                    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);
            }
            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++;
        }
示例#3
0
文件: FontChip.cs 项目: kissdesty/SDK
        /// <summary>
        ///     This method converts text into pixel data. It leverages the
        ///     GenerateTextData() method. The supplied <paramref name="pixels" />
        ///     int array, <paramref name="width" /> and
        ///     <paramref name="height" /> values will be set with the data generated
        ///     by the method.
        /// </summary>
        /// <param name="value">The string to convert.</param>
        /// <param name="pixels">An array to set the new pixel data on.</param>
        /// <param name="width">The returned width of the text.</param>
        /// <param name="height">The returned height of the text.</param>
        /// <param name="fontName"></param>
        /// <param name="letterSpacing"></param>
        /// <param name="offset"></param>
        /// <param name="colorID">
        ///     The color id to use when rendering the text. This should match up
        ///     the colors in the ColorChip.
        /// </param>
        public void ConvertTextToPixelData(string value, ref int[] pixels, out int width, out int height, string fontName = "Default", int letterSpacing = 0, int offset = 0)
        {
            if (fontName == "Default")
            {
                fontName = fonts.Keys.First();
            }

            // if no font exists, exit out of the draw call
            if (fonts.ContainsKey(fontName))
            {
                var fontMap = fonts[fontName];
                GenerateTextData(value, engine.spriteChip, fontMap, tmpTextureData, false, letterSpacing, offset);
            }
            else
            {
                tmpTextureData.Resize(1, 1);
            }

            tmpTextureData.CopyPixels(ref pixels);
            width  = tmpTextureData.width;
            height = tmpTextureData.height;
        }
        /// <summary>
        ///     Converts Texture Data into a Texture
        /// </summary>
        /// <param name="textureData"></param>
        /// <param name="colors"></param>
        /// <param name="transColor"></param>
        public void LoadTextureData(TextureData textureData, ColorData[] colors, string transColor = "#ff00ff")
        {
            var tmpBGColor = new ColorData(transColor);

            var bgColor = new Color(tmpBGColor.r, tmpBGColor.g, tmpBGColor.b, 1);

            var w = textureData.width;
            var h = textureData.height;

            if (texture.width != w || texture.height != h)
            {
                texture.Resize(w, h);
            }

            // Get the source pixel data
            var pixels = texture.GetPixels();

            // Copy over all the pixel data from the cache to the tmp pixel data array
            textureData.CopyPixels(ref tmpPixelData, 0, 0, textureData.width, textureData.height);

            int total = pixels.Length;

            // Loop through the array and conver the colors
            for (int i = 0; i < total; i++)
            {
                var colorIndex = tmpPixelData[i];
                if (colorIndex < 0 || colorIndex >= colors.Length)
                {
                    pixels[i] = bgColor;
                }
                else
                {
                    var colorData = colors[colorIndex];

                    pixels[i].r = colorData.r;
                    pixels[i].g = colorData.g;
                    pixels[i].b = colorData.b;
                    pixels[i].a = colorData.a; // TODO should the engine hard code alpha to 1
                }
            }

            texture.SetPixels(pixels);

            if (flip)
            {
                FlipTexture();
            }
        }
示例#5
0
        public static void CovertSpritesToRawData(ref int[] pixelData, int[] spriteIDs, int width, IEngineChips chips)
        {
            var spriteChip = chips.spriteChip;

            var spriteWidth  = spriteChip.width;
            var spriteHeight = spriteChip.height;


            //TODO need to allow flipping and match the draw sprite API
            var realHeight = spriteHeight * MathUtil.CeilToInt(spriteIDs.Length / width);
            var realWidth  = spriteWidth * width;

            var data = new TextureData(realWidth, realHeight);

            //Debug.Log("Draw "+ids.Length + " sprites.");

            var total      = spriteIDs.Length;
            var spriteData = new int[8 * 8];

            for (var i = 0; i < total; i++)
            {
                var id = spriteIDs[i];
                if (id > -1)
                {
                    var newX = MathUtil.FloorToInt(i % width) * spriteWidth;
                    var newY = MathUtil.FloorToInt(i / width) * spriteWidth;
                    newY = realHeight - spriteHeight - newY;
                    spriteChip.ReadSpriteAt(id, spriteData);
                    data.SetPixels(newX, newY, spriteWidth, spriteHeight, spriteData);

                    //DrawSprite(id, newX, newY);
                }
            }

            data.CopyPixels(ref pixelData);

            //var pixelData = data.GetPixels();
            //return pixelData;
        }
示例#6
0
 public static void CloneTextureData(TextureData source, TextureData target)
 {
     source.CopyPixels(ref tmpPixelData);
     target.SetPixels(tmpPixelData);
 }
示例#7
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++;
        }
示例#8
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();
        }
示例#9
0
        /// <summary>
        /// </summary>
        public void Draw()
        {
            int displayX, displayY, mapX, mapY, mapPixelIndex;
            var colorID = -1;

            var mapWidth  = tilemapChip.realWidth;
            var mapHeight = tilemapChip.realHeight;
            var width     = _width;
            int tileColor;

            // Get a reference to the complete tilemap's cached pixel data;
            //var cachedTilemap = tilemapChip.cachedTilemapPixels;
            tilemapChip.ReadCachedTilemap(ref cachedTilemap);

            // Get the current clear flag value
            var clearViewport = clearFlag;

            // Set up the clear boundaries
            var clLeft   = -1;
            var clTop    = -1;
            var clRight  = -1;
            var clBottom = -1;

            // If Clear view port is true, resize the bounds to the clear draw request
            if (clearViewport)
            {
                clLeft   = clearDrawRequest.x.Clamp(0, _width);
                clTop    = clearDrawRequest.y.Clamp(0, _width);
                clRight  = (clLeft + clearDrawRequest.width).Clamp(0, _width);
                clBottom = (clTop + clearDrawRequest.height).Clamp(0, _width);

                //Debug.Log("Clear Bounds clLeft " + clLeft + " clTop " + clTop + " clRight " + clRight + " clBottom " + clBottom);
            }

            // Flag to tell if we should draw the tilemap
            var tilemapViewport = false;

            // Set up the map position
            var tmLeft   = -1;
            var tmTop    = -1;
            var tmRight  = -1;
            var tmBottom = -1;

            // If tilemap draw request exists, configure the coordinates
            if (tilemapDrawRequest != null)
            {
                tmLeft   = tilemapDrawRequest.x.Clamp(0, _width);
                tmTop    = tilemapDrawRequest.y.Clamp(0, _width);
                tmRight  = (tmLeft + tilemapDrawRequest.width).Clamp(0, _width);
                tmBottom = (tmTop + tilemapDrawRequest.height).Clamp(0, _height);
            }

            // A flag to determine if we need to draw the pixel or not
            bool setPixel;

            // Get a local reference to the total number pixels in the display
            var total = totalPixels;

            // Setup the display mask
            if (displayMask.Length != total)
            {
                Array.Resize(ref displayMask, total);
            }

            // Create floats for these values since we use it to repeate later on
            var mwF = (float)mapWidth;
            var mhF = (float)mapHeight;

            // Loop through each of the pixels in the display
            for (var i = 0; i < total; i++)
            {
                // Calculate current display x,y position
                displayX = i % width; // TODO if we don't repeat this it draws matching pixels off by 1 on Y axis?
                displayY = i / width;

                // Calculate map position
                mapX = displayX - tmLeft + scrollX;
                mapY = displayY + tmTop + (mapHeight - height) - scrollY;

                // Flip Y for display to draw clear correctly
                displayY = height - displayY - 1;

                // Calculate if x,y is within the clear boundaries
                clearViewport   = displayX >= clLeft && displayX <= clRight && displayY >= clTop && displayY <= clBottom;
                tilemapViewport = displayX >= tmLeft && displayX <= tmRight && displayY >= tmTop && displayY <= tmBottom;

                // Check to see if we need to clear the display
                if (clearViewport)
                {
                    colorID  = -1;
                    setPixel = true;
                }
                else
                {
                    setPixel = false;
                }

                // Check to see if we need to draw the tilemap
                if (tilemapViewport)
                {
                    // Wrap the map's x,y position
                    mapX = (int)(mapX - Math.Floor(mapX / mwF) * mwF);
                    mapY = (int)(mapY - Math.Floor(mapY / mhF) * mhF);

                    // Calculate the map pixel index
                    mapPixelIndex = mapX + mapY * mapWidth;

                    // Find the color for the tile's pixel
                    tileColor = cachedTilemap[mapPixelIndex];

                    // If there is a pixel color, set it to the colorID and flag to draw the pixel
                    if (tileColor > -1)
                    {
                        colorID  = tileColor;
                        setPixel = true;
                    }
                }

                // If there is a pixel to draw, set it
                if (setPixel)
                {
                    displayPixels[i] = colorID;
                }

                // Always set the color to the mask. -1 will be empty pixels, everything else is ignored
                displayMask[i] = colorID;
            }


            // Copy the UI layer to the display
            uiLayer.CopyPixels(ref displayPixels, true);

            // Loop through all draw requests
            var totalDR = drawRequests.Count;

            for (var i = 0; i < totalDR; i++)
            {
                var draw = drawRequests[i];
                draw.DrawPixels(ref displayPixels, width, height, displayMask);
            }

            // Reset clear flag
            clearFlag = false;

            // Reset Draw Requests after they have been processed
            ResetDrawCalls();
        }