OSDN Git Service

Added Polygon to ShapeRenderer
authorlokoyaw <lokoyaw@gmail.com>
Thu, 31 Jan 2013 10:49:24 +0000 (11:49 +0100)
committerlokoyaw <lokoyaw@gmail.com>
Thu, 31 Jan 2013 10:49:24 +0000 (11:49 +0100)
It would be great to add FilledPolygon too, but I have no clue.

gdx/src/com/badlogic/gdx/graphics/glutils/ShapeRenderer.java

index 047d023..7d14a40 100644 (file)
@@ -89,6 +89,7 @@ public class ShapeRenderer {
                Cone(GL10.GL_LINES), //\r
                FilledCone(GL10.GL_TRIANGLES), //\r
                Curve(GL10.GL_LINES), //\r
+               Polygon(GL10.GL_LINES), //\r
                ;\r
 \r
                private final int glType;\r
@@ -654,6 +655,43 @@ public class ShapeRenderer {
                renderer.color(color.r, color.g, color.b, color.a);\r
                renderer.vertex(x + cx, y + cy, z);\r
        }\r
+       \r
+       /** Draws a polygon in the x/y plane. The vertices must contain at least 3 points (6 floats x,y). The\r
+        * {@link ShapeType} passed to begin has to be {@link ShapeType#Polygon}.\r
+        * @param vertices */\r
+       public void polygon (float[] vertices) {\r
+               if (currType != ShapeType.Polygon) throw new GdxRuntimeException("Must call begin(ShapeType.Polygon)");\r
+               if (vertices.length < 6) throw new IllegalArgumentException("Polygons must contain at least 3 points.");\r
+               if (vertices.length % 2 != 0) throw new IllegalArgumentException("Polygons must have a pair number of vertices.");\r
+               final int numFloats = vertices.length;\r
+               \r
+               checkDirty();\r
+               checkFlush(numFloats);\r
+               \r
+               float firstX = vertices[0];\r
+               float firstY = vertices[1];\r
+               \r
+               for (int i = 0; i < numFloats; i += 2) {\r
+                       float x1 = vertices[i];\r
+                       float y1 = vertices[i + 1];\r
+                       \r
+                       float x2;\r
+                       float y2;\r
+                       \r
+                       if(i + 2 >= numFloats){\r
+                               x2 = firstX;\r
+                               y2 = firstY;\r
+                       }else{\r
+                               x2 = vertices[i + 2];\r
+                               y2 = vertices[i + 3];\r
+                       }\r
+                       \r
+                       renderer.color(color.r, color.g, color.b, color.a);\r
+                       renderer.vertex(x1, y1, 0);\r
+                       renderer.color(color.r, color.g, color.b, color.a);\r
+                       renderer.vertex(x2, y2, 0);\r
+               }\r
+       }\r
 \r
        private void checkDirty () {\r
                if (!matrixDirty) return;\r