OSDN Git Service

[changed] IsoCamTest is now a fully working isometric tile rendering example with...
authorbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 16 Jun 2011 22:33:56 +0000 (22:33 +0000)
committerbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 16 Jun 2011 22:33:56 +0000 (22:33 +0000)
tests/gdx-tests/src/com/badlogic/gdx/tests/IsoCamTest.java

index e8dc495..2ce70b6 100644 (file)
 package com.badlogic.gdx.tests;\r
 \r
 import com.badlogic.gdx.Gdx;\r
+import com.badlogic.gdx.InputProcessor;\r
 import com.badlogic.gdx.graphics.GL10;\r
-import com.badlogic.gdx.graphics.Mesh;\r
 import com.badlogic.gdx.graphics.OrthographicCamera;\r
 import com.badlogic.gdx.graphics.Texture;\r
-import com.badlogic.gdx.graphics.VertexAttribute;\r
-import com.badlogic.gdx.graphics.VertexAttributes.Usage;\r
+import com.badlogic.gdx.graphics.g2d.Sprite;\r
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
+import com.badlogic.gdx.math.Intersector;\r
 import com.badlogic.gdx.math.Matrix4;\r
+import com.badlogic.gdx.math.Plane;\r
 import com.badlogic.gdx.math.Vector3;\r
+import com.badlogic.gdx.math.collision.Ray;\r
 import com.badlogic.gdx.tests.utils.GdxTest;\r
 \r
-public class IsoCamTest extends GdxTest {\r
+public class IsoCamTest extends GdxTest implements InputProcessor {\r
 \r
        @Override public boolean needsGL20 () {\r
                return false;\r
        }\r
 \r
        Texture texture;\r
-       Mesh mesh;\r
        OrthographicCamera cam;\r
-//     Matrix4 projection;\r
-//     Matrix4 view;\r
+       SpriteBatch batch;      \r
+       final Sprite[][] sprites = new Sprite[10][10];\r
+       final Matrix4 matrix = new Matrix4();\r
+       final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);\r
+       final Vector3 intersection = new Vector3();\r
+       Sprite lastSelectedTile = null;\r
        \r
        @Override public void create() {\r
-               mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_pos"));\r
-               mesh.setVertices(new float[] {\r
-                       -1, 0,  1,\r
-                        1, 0,  1,\r
-                        1, 0, -1,\r
-                       -1, 0, -1\r
-               });\r
-               mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });              \r
-               cam = new OrthographicCamera(10, 10);           \r
-               cam.position.set(3, 3, 3);\r
-               cam.lookAt(0, 0, 0);                    \r
+               texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));            \r
+               cam = new OrthographicCamera(10, 10 * (Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth()));                     \r
+               cam.position.set(5, 5, 10);\r
+               cam.direction.set(-1, -1, -1);\r
+               cam.near = 1;\r
+               cam.far = 100;\r
+               \r
+               // rotation matrix so we rotate the x/y plane spritebatch\r
+               // operates on to tze x/z plane.\r
+               matrix.setToRotation(new Vector3(1, 0, 0), 90);\r
+               \r
+               for(int z = 0; z < 10; z++) {\r
+                       for(int x = 0; x < 10; x++) {\r
+                               sprites[x][z] = new Sprite(texture);\r
+                               sprites[x][z].setPosition(x,z);\r
+                               sprites[x][z].setSize(1, 1);\r
+                       }\r
+               }\r
+               \r
+               batch = new SpriteBatch();\r
+               \r
+               Gdx.input.setInputProcessor(this);\r
        }\r
        \r
        @Override public void render() {\r
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
-               \r
                cam.update();           \r
-               Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);\r
-               Gdx.gl10.glLoadMatrixf(cam.projection.val, 0);\r
-               Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);\r
-               Gdx.gl10.glLoadMatrixf(cam.view.val, 0);\r
+                               \r
+               batch.setProjectionMatrix(cam.combined);\r
+               batch.setTransformMatrix(matrix);\r
+               batch.begin();\r
+               for(int z = 0; z < 10; z++) {\r
+                       for(int x = 0; x < 10; x++) {\r
+                               sprites[x][z].draw(batch);\r
+                       }\r
+               }\r
+               batch.end();\r
                \r
-               mesh.render(GL10.GL_TRIANGLES);\r
+               if(Gdx.input.justTouched()) {\r
+                       Ray pickRay = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());\r
+                       Intersector.intersectRayPlane(pickRay, xzPlane, intersection);\r
+                       int x = (int)intersection.x;\r
+                       int z = (int)intersection.z;\r
+                       if(x >= 0 && x < 10 && z >= 0 && z < 10) {\r
+                               if(lastSelectedTile != null) lastSelectedTile.setColor(1, 1, 1, 1);\r
+                               Sprite sprite = sprites[x][z];\r
+                               sprite.setColor(1, 0, 0, 1);\r
+                               lastSelectedTile = sprite;\r
+                       }\r
+               }\r
        }\r
+       \r
+       final Vector3 curr = new Vector3();\r
+       final Vector3 last = new Vector3(-1, -1, -1);\r
+       final Vector3 delta = new Vector3();\r
+       @Override public boolean touchDragged (int x, int y, int pointer) {\r
+               Ray pickRay = cam.getPickRay(x, y);\r
+               Intersector.intersectRayPlane(pickRay, xzPlane, curr);\r
+               \r
+               if(!(last.x == -1 && last.y == -1 && last.z == -1)) {\r
+                       pickRay = cam.getPickRay(last.x, last.y);\r
+                       Intersector.intersectRayPlane(pickRay, xzPlane, delta);                 \r
+                       delta.sub(curr);\r
+                       cam.position.add(delta.x, delta.y, delta.z);\r
+               }\r
+               last.set(x, y, 0);\r
+               return false;\r
+       }\r
+       \r
+       @Override public boolean touchUp(int x, int y, int pointer, int button) {\r
+               last.set(-1, -1, -1);\r
+               return false;\r
+       }\r
+\r
+       @Override public boolean keyDown (int keycode) { return false; }        \r
+       @Override public boolean keyUp (int keycode) { return false; }\r
+       @Override public boolean keyTyped (char character) { return false; }\r
+       @Override public boolean touchDown (int x, int y, int pointer, int button) { return false; }            \r
+       @Override public boolean touchMoved (int x, int y) { return false; }\r
+       @Override public boolean scrolled (int amount) { return false; }\r
 }\r