Ejemplo n.º 1
0
        public virtual LImage SetTileBackground(LImage image, bool isReturn)
        {
            if (image == null)
            {
                return(null);
            }
            int layerWidth  = GetWidth();
            int layerHeight = GetHeight();
            int tileWidth   = image.GetWidth();
            int tileHeight  = image.GetHeight();

            LImage    tempImage = LImage.CreateImage(layerWidth, layerHeight, false);
            LGraphics g         = tempImage.GetLGraphics();

            for (int x = 0; x < layerWidth; x += tileWidth)
            {
                for (int y = 0; y < layerHeight; y += tileHeight)
                {
                    g.DrawImage(image, x, y);
                }
            }
            g.Dispose();
            if (isReturn)
            {
                return(tempImage);
            }
            tempImage.SetFormat(Loon.Core.Graphics.Opengl.LTexture.Format.SPEED);
            SetBackground(tempImage.GetTexture());
            if (tempImage != null)
            {
                tempImage.Dispose();
                tempImage = null;
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static LTexture FilterColor(string res, LColor height, Loon.Core.Graphics.Opengl.LTexture.Format format)
        {
            uint      color = height.GetRGB();
            LImage    tmp   = LImage.CreateImage(res);
            LImage    image = LImage.CreateImage(tmp.GetWidth(), tmp.GetHeight(), true);
            LGraphics g     = image.GetLGraphics();

            g.DrawImage(tmp, 0, 0);
            g.Dispose();
            if (tmp != null)
            {
                tmp.Dispose();
                tmp = null;
            }
            Color[] pixels = image.GetPixels();
            int     size   = pixels.Length;

            for (int i = 0; i < size; i++)
            {
                if (pixels[i].PackedValue == color)
                {
                    pixels[i].PackedValue = LSystem.TRANSPARENT;
                }
            }
            image.SetFormat(format);
            image.SetPixels(pixels, image.GetWidth(), image.GetHeight());
            LTexture texture = image.GetTexture();

            if (image != null)
            {
                image.Dispose();
                image = null;
            }
            return(texture);
        }
Ejemplo n.º 3
0
        public static LTexture FilterLimitColor(string res, LColor start,
                                                LColor end, Loon.Core.Graphics.Opengl.LTexture.Format format)
        {
            int       sred   = start.R;
            int       sgreen = start.G;
            int       sblue  = start.B;
            int       ered   = end.R;
            int       egreen = end.G;
            int       eblue  = end.B;
            LImage    tmp    = LImage.CreateImage(res);
            LImage    image  = LImage.CreateImage(tmp.GetWidth(), tmp.GetHeight(), true);
            LGraphics g      = image.GetLGraphics();

            g.DrawImage(tmp, 0, 0);
            g.Dispose();
            if (tmp != null)
            {
                tmp.Dispose();
                tmp = null;
            }
            Color[] pixels = image.GetPixels();
            int     size   = pixels.Length;

            for (int i = 0; i < size; i++)
            {
                Color pixel = pixels[i];
                if ((pixel.R >= sred && pixel.G >= sgreen && pixel.B >= sblue) &&
                    (pixel.R <= ered && pixel.G <= egreen && pixel.B <= eblue))
                {
                    pixels[i].PackedValue = LSystem.TRANSPARENT;
                }
            }
            image.SetFormat(format);
            image.SetPixels(pixels, image.GetWidth(), image.GetHeight());
            LTexture texture = image.GetTexture();

            if (image != null)
            {
                image.Dispose();
                image = null;
            }
            return(texture);
        }
Ejemplo n.º 4
0
        public virtual void SetField2DBackground(Field2D field, Dictionary <object, object> pathMap,
                                                 string fileName)
        {
            SetField2D(field);
            LImage background = null;

            if (fileName != null)
            {
                LImage tmp = LImage.CreateImage(fileName);
                background = SetTileBackground(tmp, true);
                if (tmp != null)
                {
                    tmp.Dispose();
                    tmp = null;
                }
            }
            else
            {
                background = LImage.CreateImage(GetWidth(), GetHeight(), false);
            }
            int srcWidth  = GetWidth();
            int srcHeight = GetHeight();

            //在C#环境下LGraphics采取像素渲染,得不到硬件加速,此处直接将像素操作方法粘过来了(虽然也快不了几毫秒……)
            int[] dstColors = background.GetIntPixels();
            int[] srcColors = null;
            for (int i = 0; i < field.GetWidth(); i++)
            {
                for (int j = 0; j < field.GetHeight(); j++)
                {
                    int    index = field.GetType(j, i);
                    object o     = CollectionUtils.Get(pathMap, index);
                    if (o != null)
                    {
                        if (o is LImage)
                        {
                            LImage img = (LImage)o;
                            srcColors = img.GetIntPixels();
                            int w = img.Width;
                            int h = img.Height;
                            int y = field.TilesToHeightPixels(j);
                            int x = field.TilesToWidthPixels(i);
                            if (x < 0)
                            {
                                w += x;
                                x  = 0;
                            }
                            if (y < 0)
                            {
                                h += y;
                                y  = 0;
                            }
                            if (x + w > srcWidth)
                            {
                                w = srcWidth - x;
                            }
                            if (y + h > srcHeight)
                            {
                                h = srcHeight - y;
                            }
                            if (img.hasAlpha)
                            {
                                int findIndex = y * srcWidth + x;
                                int drawIndex = 0;
                                int moveFind  = srcWidth - w;
                                for (int col = 0; col < h; col++)
                                {
                                    for (int row = 0; row < w;)
                                    {
                                        if (srcColors[drawIndex] != 0)
                                        {
                                            dstColors[findIndex] = srcColors[drawIndex];
                                        }
                                        row++;
                                        findIndex++;
                                        drawIndex++;
                                    }
                                    findIndex += moveFind;
                                }
                            }
                            else
                            {
                                for (int size = 0; size < h; size++)
                                {
                                    System.Array.Copy(srcColors, size * w, dstColors,
                                                      (y + size) * srcWidth + x, w);
                                }
                            }
                        }
                        else if (o is Actor)
                        {
                            AddObject(((Actor)o), field.TilesToWidthPixels(i),
                                      field.TilesToHeightPixels(j));
                        }
                    }
                }
            }
            background.SetIntPixels(dstColors);
            background.SetFormat(Loon.Core.Graphics.Opengl.LTexture.Format.SPEED);
            SetBackground(background.GetTexture());
            srcColors = null;
            dstColors = null;
            if (background != null)
            {
                background.Dispose();
                background = null;
            }
        }