• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import java.util.Random;
20 
21 import com.badlogic.gdx.Gdx;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.OrthographicCamera;
24 import com.badlogic.gdx.graphics.Texture;
25 import com.badlogic.gdx.graphics.g2d.SpriteCache;
26 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
27 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
28 import com.badlogic.gdx.tests.utils.GdxTest;
29 import com.badlogic.gdx.tests.utils.OrthoCamController;
30 import com.badlogic.gdx.utils.TimeUtils;
31 
32 public class IsometricTileTest extends GdxTest {
33 	static final int LAYERS = 1;
34 	static final int WIDTH = 4;
35 	static final int HEIGHT = 5;
36 	static final int TILES_PER_LAYER = WIDTH * HEIGHT;
37 	static final int TILE_WIDTH = 54;
38 	static final int TILE_HEIGHT = 54;
39 	static final int TILE_HEIGHT_DIAMOND = 28;
40 	static final int BOUND_X = HEIGHT * TILE_WIDTH / 2 + WIDTH * TILE_WIDTH / 2;
41 	static final int BOUND_Y = HEIGHT * TILE_HEIGHT_DIAMOND / 2 + WIDTH * TILE_HEIGHT_DIAMOND / 2;
42 
43 	Texture texture;
44 	SpriteCache[] caches = new SpriteCache[LAYERS];
45 	int[] layers = new int[LAYERS];
46 	OrthographicCamera cam;
47 	OrthoCamController camController;
48 	ShapeRenderer renderer;
49 	long startTime = TimeUtils.nanoTime();
50 
51 	@Override
create()52 	public void create () {
53 		cam = new OrthographicCamera(480, 320);
54 		camController = new OrthoCamController(cam);
55 		Gdx.input.setInputProcessor(camController);
56 
57 		renderer = new ShapeRenderer();
58 		texture = new Texture(Gdx.files.internal("data/isotile.png"));
59 
60 		Random rand = new Random();
61 		for (int i = 0; i < LAYERS; i++) {
62 			caches[i] = new SpriteCache();
63 			SpriteCache cache = caches[i];
64 			cache.beginCache();
65 
66 			int colX = HEIGHT * TILE_WIDTH / 2 - TILE_WIDTH / 2;
67 			int colY = BOUND_Y - TILE_HEIGHT_DIAMOND;
68 			for (int x = 0; x < WIDTH; x++) {
69 				for (int y = 0; y < HEIGHT; y++) {
70 					int tileX = colX - y * TILE_WIDTH / 2;
71 					int tileY = colY - y * TILE_HEIGHT_DIAMOND / 2;
72 					cache.add(texture, tileX, tileY, rand.nextInt(2) * 54, 0, TILE_WIDTH, TILE_HEIGHT);
73 				}
74 				colX += TILE_WIDTH / 2;
75 				colY -= TILE_HEIGHT_DIAMOND / 2;
76 			}
77 
78 			layers[i] = cache.endCache();
79 		}
80 	}
81 
82 	@Override
dispose()83 	public void dispose () {
84 		renderer.dispose();
85 		texture.dispose();
86 		for (SpriteCache cache : caches)
87 			cache.dispose();
88 	}
89 
90 	@Override
render()91 	public void render () {
92 		Gdx.gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
93 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
94 		cam.update();
95 
96 		Gdx.gl.glEnable(GL20.GL_BLEND);
97 		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
98 		for (int i = 0; i < LAYERS; i++) {
99 			SpriteCache cache = caches[i];
100 			cache.setProjectionMatrix(cam.combined);
101 			cache.begin();
102 			cache.draw(layers[i]);
103 			cache.end();
104 		}
105 
106 		renderer.setProjectionMatrix(cam.combined);
107 		renderer.begin(ShapeType.Line);
108 		renderer.setColor(1, 0, 0, 1);
109 		renderer.line(0, 0, 500, 0);
110 		renderer.line(0, 0, 0, 500);
111 
112 		renderer.setColor(0, 0, 1, 1);
113 		renderer.line(0, BOUND_Y, BOUND_X, BOUND_Y);
114 
115 		renderer.setColor(0, 0, 1, 1);
116 		renderer.line(BOUND_X, 0, BOUND_X, BOUND_Y);
117 
118 		renderer.end();
119 	}
120 }
121