OSDN Git Service

removed lwjgl-debug.jar dependency, freaking hate it, glError after each call? really?
[mikumikustudio/libgdx-mikumikustudio.git] / tests / gdx-tests / src / com / badlogic / gdx / tests / IsoCamTest.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/\r
16 \r
17 package com.badlogic.gdx.tests;\r
18 \r
19 import com.badlogic.gdx.Gdx;\r
20 import com.badlogic.gdx.InputAdapter;\r
21 import com.badlogic.gdx.graphics.Camera;\r
22 import com.badlogic.gdx.graphics.GL10;\r
23 import com.badlogic.gdx.graphics.OrthographicCamera;\r
24 import com.badlogic.gdx.graphics.Texture;\r
25 import com.badlogic.gdx.graphics.g2d.Sprite;\r
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
27 import com.badlogic.gdx.math.Intersector;\r
28 import com.badlogic.gdx.math.Matrix4;\r
29 import com.badlogic.gdx.math.Plane;\r
30 import com.badlogic.gdx.math.Vector3;\r
31 import com.badlogic.gdx.math.collision.Ray;\r
32 import com.badlogic.gdx.tests.utils.GdxTest;\r
33 \r
34 public class IsoCamTest extends GdxTest {\r
35         private static final int TARGET_WIDTH = 480;\r
36         private static final float UNIT_TO_PIXEL = TARGET_WIDTH * 0.15f;\r
37         Texture texture;\r
38         OrthographicCamera cam;\r
39         SpriteBatch batch;\r
40         final Sprite[][] sprites = new Sprite[10][10];\r
41         final Matrix4 matrix = new Matrix4();\r
42 \r
43         @Override\r
44         public void create () {\r
45                 texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));\r
46                 float unitsOnX = (float)Math.sqrt(2) * TARGET_WIDTH / (UNIT_TO_PIXEL);\r
47                 float pixelsOnX = Gdx.graphics.getWidth() / unitsOnX;\r
48                 float unitsOnY = Gdx.graphics.getHeight() / pixelsOnX;\r
49                 cam = new OrthographicCamera(unitsOnX, unitsOnY, 25);\r
50                 cam.position.scl(30);\r
51                 cam.near = 1;\r
52                 cam.far = 1000;\r
53                 matrix.setToRotation(new Vector3(1, 0, 0), 90);\r
54 \r
55                 for (int z = 0; z < 10; z++) {\r
56                         for (int x = 0; x < 10; x++) {\r
57                                 sprites[x][z] = new Sprite(texture);\r
58                                 sprites[x][z].setPosition(x, z);\r
59                                 sprites[x][z].setSize(1, 1);\r
60                         }\r
61                 }\r
62 \r
63                 batch = new SpriteBatch();\r
64 \r
65                 Gdx.input.setInputProcessor(new IsoCamController(cam));\r
66         }\r
67 \r
68         @Override\r
69         public void dispose () {\r
70                 texture.dispose();\r
71                 batch.dispose();\r
72         }\r
73 \r
74         @Override\r
75         public void render () {\r
76                 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
77                 cam.update();\r
78 \r
79                 batch.setProjectionMatrix(cam.combined);\r
80                 batch.setTransformMatrix(matrix);\r
81                 batch.begin();\r
82                 for (int z = 0; z < 10; z++) {\r
83                         for (int x = 0; x < 10; x++) {\r
84                                 sprites[x][z].draw(batch);\r
85                         }\r
86                 }\r
87                 batch.end();\r
88 \r
89                 checkTileTouched();\r
90         }\r
91 \r
92         final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);\r
93         final Vector3 intersection = new Vector3();\r
94         Sprite lastSelectedTile = null;\r
95 \r
96         private void checkTileTouched () {\r
97                 if (Gdx.input.justTouched()) {\r
98                         Ray pickRay = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());\r
99                         Intersector.intersectRayPlane(pickRay, xzPlane, intersection);\r
100                         System.out.println(intersection);\r
101                         int x = (int)intersection.x;\r
102                         int z = (int)intersection.z;\r
103                         if (x >= 0 && x < 10 && z >= 0 && z < 10) {\r
104                                 if (lastSelectedTile != null) lastSelectedTile.setColor(1, 1, 1, 1);\r
105                                 Sprite sprite = sprites[x][z];\r
106                                 sprite.setColor(1, 0, 0, 1);\r
107                                 lastSelectedTile = sprite;\r
108                         }\r
109                 }\r
110         }\r
111 \r
112         public class IsoCamController extends InputAdapter {\r
113                 final Plane xzPlane = new Plane(new Vector3(0, 1, 0), 0);\r
114                 final Vector3 intersection = new Vector3();\r
115                 final Vector3 curr = new Vector3();\r
116                 final Vector3 last = new Vector3(-1, -1, -1);\r
117                 final Vector3 delta = new Vector3();\r
118                 final Camera camera;\r
119 \r
120                 public IsoCamController (Camera camera) {\r
121                         this.camera = camera;\r
122                 }\r
123 \r
124                 @Override\r
125                 public boolean touchDragged (int x, int y, int pointer) {\r
126                         Ray pickRay = camera.getPickRay(x, y);\r
127                         Intersector.intersectRayPlane(pickRay, xzPlane, curr);\r
128 \r
129                         if (!(last.x == -1 && last.y == -1 && last.z == -1)) {\r
130                                 pickRay = camera.getPickRay(last.x, last.y);\r
131                                 Intersector.intersectRayPlane(pickRay, xzPlane, delta);\r
132                                 delta.sub(curr);\r
133                                 camera.position.add(delta.x, 0, delta.z);\r
134                         }\r
135                         last.set(x, y, 0);\r
136                         return false;\r
137                 }\r
138 \r
139                 @Override\r
140                 public boolean touchUp (int x, int y, int pointer, int button) {\r
141                         last.set(-1, -1, -1);\r
142                         return false;\r
143                 }\r
144         }\r
145 \r
146         @Override\r
147         public boolean needsGL20 () {\r
148                 return false;\r
149         }\r
150 \r
151 }\r