コード例 #1
0
 private LImage PackImage()
 {
     CheckPacked();
     if (packing)
     {
         if (temps.IsEmpty())
         {
             throw new InvalidOperationException("Nothing to Pack !");
         }
         int maxWidth  = 0;
         int maxHeight = 0;
         int totalArea = 0;
         for (int i = 0; i < temps.Size(); i++)
         {
             PackEntry entry  = (PackEntry)temps.Get(i);
             int       width  = entry.image.GetWidth();
             int       height = entry.image.GetHeight();
             if (width > maxWidth)
             {
                 maxWidth = width;
             }
             if (height > maxHeight)
             {
                 maxHeight = height;
             }
             totalArea += width * height;
         }
         Loon.Core.Geom.Point.Point2i size = new Loon.Core.Geom.Point.Point2i(CloseTwoPower(maxWidth),
                                                                              CloseTwoPower(maxHeight));
         bool fitAll = false;
         loop : {
             while (!fitAll)
             {
                 int area = size.x * size.y;
                 if (area < totalArea)
                 {
                     NextSize(size);
                     continue;
                 }
                 Node root = new Node(size.x, size.y);
                 for (int i = 0; i < temps.Size(); i++)
                 {
                     PackEntry entry    = (PackEntry)temps.Get(i);
                     Node      inserted = root.Insert(entry);
                     if (inserted == null)
                     {
                         NextSize(size);
                         goto loop;
                     }
                 }
                 fitAll = true;
             }
         }
         int srcWidth  = size.x;
         int srcHeight = size.y;
         //由于LGraphics操作较耗时,此处直接处理像素(其实也快不了几毫秒,未来将改写为C#混合C/C++)
         LImage image     = LImage.CreateImage(srcWidth, srcHeight, useAlpha);
         int[]  dstPixels = image.GetIntPixels();
         int[]  srcPixels = null;
         for (int i = 0; i < temps.Size(); i++)
         {
             PackEntry entry = (PackEntry)temps.Get(i);
             LImage    img   = entry.image;
             srcPixels = img.GetIntPixels();
             int w = img.Width;
             int h = img.Height;
             int x = entry.bounds.left;
             int y = entry.bounds.top;
             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 (srcPixels[drawIndex] != 0)
                         {
                             dstPixels[findIndex] = srcPixels[drawIndex];
                         }
                         row++;
                         findIndex++;
                         drawIndex++;
                     }
                     findIndex += moveFind;
                 }
             }
             else
             {
                 for (int count = 0; count < h; count++)
                 {
                     System.Array.Copy(srcPixels, count * w, dstPixels,
                                       (y + count) * srcWidth + x, w);
                 }
             }
         }
         image.SetIntPixels(dstPixels);
         srcPixels = null;
         dstPixels = null;
         packing   = false;
         return(image);
     }
     return(null);
 }
コード例 #2
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;
            }
        }