OSDN Git Service

[added] TexturePacker, duplicate border pixels in padding.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 16 Dec 2010 07:08:17 +0000 (07:08 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 16 Dec 2010 07:08:17 +0000 (07:08 +0000)
extensions/image-packer/src/com/badlogic/gdx/imagepacker/TexturePacker.java

index c9aea07..62c358c 100644 (file)
@@ -272,15 +272,16 @@ public class TexturePacker {
                int usedPixels = 0;\r
                for (int i = images.size() - 1; i >= 0; i--) {\r
                        Image image = images.get(i);\r
-                       Node node = root.insert(image, canvas, false);\r
+                       Node node = root.insert(image, false);\r
                        if (node == null) {\r
-                               if (settings.rotate) node = root.insert(image, canvas, true);\r
+                               if (settings.rotate) node = root.insert(image, true);\r
                                if (node == null) continue;\r
                        }\r
                        usedPixels += image.getWidth() * image.getHeight();\r
                        images.remove(i);\r
                        if (canvas != null) {\r
                                System.out.println("Packing... " + image.name);\r
+                               node.writePackEntry();\r
                                Graphics2D g = (Graphics2D)canvas.getGraphics();\r
                                if (image.rotate) {\r
                                        g.translate(node.left, node.top);\r
@@ -288,6 +289,17 @@ public class TexturePacker {
                                        g.translate(-node.left, -node.top);\r
                                        g.translate(-image.getWidth(), 0);\r
                                }\r
+                               if (settings.duplicatePadding) {\r
+                                       int amount = settings.padding / 2;\r
+                                       int imageWidth = image.getWidth();\r
+                                       int imageHeight = image.getHeight();\r
+                                       g.drawImage(image, node.left, node.top - amount, node.left + imageWidth, node.top, 0, 0, imageWidth, 1, null);\r
+                                       g.drawImage(image, node.left, node.top + imageHeight, node.left + imageWidth, node.top + imageHeight + amount, 0,\r
+                                               imageHeight - 1, imageWidth, imageHeight, null);\r
+                                       g.drawImage(image, node.left - amount, node.top, node.left, node.top + imageHeight, 0, 0, 1, imageHeight, null);\r
+                                       g.drawImage(image, node.left + imageWidth, node.top, node.left + imageWidth + amount, node.top + imageHeight,\r
+                                               imageWidth - 1, 0, imageWidth, imageHeight, null);\r
+                               }\r
                                g.drawImage(image, node.left, node.top, null);\r
                                if (image.rotate) {\r
                                        g.translate(image.getWidth(), 0);\r
@@ -374,7 +386,7 @@ public class TexturePacker {
        }\r
 \r
        private class Node {\r
-               final int left, top, width, height;\r
+               int left, top, width, height;\r
                Node child1, child2;\r
                Image image;\r
 \r
@@ -388,12 +400,12 @@ public class TexturePacker {
                /**\r
                 * Returns true if the image was inserted. If canvas != null, an entry is written to the pack file.\r
                 */\r
-               public Node insert (Image image, BufferedImage canvas, boolean rotate) throws IOException {\r
+               public Node insert (Image image, boolean rotate) throws IOException {\r
                        if (this.image != null) return null;\r
                        if (child1 != null) {\r
-                               Node newNode = child1.insert(image, canvas, rotate);\r
+                               Node newNode = child1.insert(image, rotate);\r
                                if (newNode != null) return newNode;\r
-                               return child2.insert(image, canvas, rotate);\r
+                               return child2.insert(image, rotate);\r
                        }\r
                        int imageWidth = image.getWidth();\r
                        int imageHeight = image.getHeight();\r
@@ -408,7 +420,6 @@ public class TexturePacker {
                        if (neededWidth == width && neededHeight == height) {\r
                                this.image = image;\r
                                image.rotate = rotate;\r
-                               write(canvas);\r
                                return this;\r
                        }\r
                        int dw = width - neededWidth;\r
@@ -420,12 +431,10 @@ public class TexturePacker {
                                child1 = new Node(left, top, width, neededHeight);\r
                                child2 = new Node(left, top + neededHeight, width, height - neededHeight);\r
                        }\r
-                       return child1.insert(image, canvas, rotate);\r
+                       return child1.insert(image, rotate);\r
                }\r
 \r
-               private void write (BufferedImage canvas) throws IOException {\r
-                       if (canvas == null) return;\r
-\r
+               void writePackEntry () throws IOException {\r
                        String imageName = image.name;\r
                        imageName = imageName.replace("\\", "/");\r
 \r
@@ -595,7 +604,8 @@ public class TexturePacker {
                public TextureFilter defaultFilterMag = TextureFilter.Linear;\r
                public int alphaThreshold = 0;\r
                public boolean pot = true;\r
-               public int padding = 0;\r
+               public int padding = 1;\r
+               public boolean duplicatePadding;\r
                public boolean debug = false;\r
                public boolean rotate = false;\r
                public int minWidth = 16;\r