• 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.tests.utils.GdxTest;
27 import com.badlogic.gdx.tests.utils.OrthoCamController;
28 import com.badlogic.gdx.utils.TimeUtils;
29 
30 public class TileTest extends GdxTest {
31 	static final int LAYERS = 5;
32 	static final int BLOCK_TILES = 25;
33 	static final int WIDTH = 15;
34 	static final int HEIGHT = 10;
35 	static final int TILES_PER_LAYER = WIDTH * HEIGHT;
36 
37 	SpriteCache[] caches = new SpriteCache[LAYERS];
38 	Texture texture;
39 	int[] layers = new int[LAYERS];
40 	OrthographicCamera cam;
41 	OrthoCamController camController;
42 	long startTime = TimeUtils.nanoTime();
43 
44 	@Override
create()45 	public void create () {
46 		cam = new OrthographicCamera(480, 320);
47 		cam.position.set(WIDTH * 32 / 2, HEIGHT * 32 / 2, 0);
48 		camController = new OrthoCamController(cam);
49 		Gdx.input.setInputProcessor(camController);
50 
51 		texture = new Texture(Gdx.files.internal("data/tiles.png"));
52 
53 		Random rand = new Random();
54 		for (int i = 0; i < LAYERS; i++) {
55 			caches[i] = new SpriteCache();
56 			SpriteCache cache = caches[i];
57 			cache.beginCache();
58 			for (int y = 0; y < HEIGHT; y++) {
59 				for (int x = 0; x < WIDTH; x++) {
60 					int tileX = rand.nextInt(5);
61 					int tileY = rand.nextInt(5);
62 					cache.add(texture, x << 5, y << 5, 1 + tileX * 33, 1 + tileY * 33, 32, 32);
63 				}
64 			}
65 			layers[i] = cache.endCache();
66 		}
67 
68 	}
69 
70 	@Override
render()71 	public void render () {
72 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
73 		cam.update();
74 
75 		Gdx.gl.glEnable(GL20.GL_BLEND);
76 		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
77 		for (int i = 0; i < LAYERS; i++) {
78 			SpriteCache cache = caches[i];
79 			cache.setProjectionMatrix(cam.combined);
80 			cache.begin();
81 			for (int j = 0; j < TILES_PER_LAYER; j += BLOCK_TILES) {
82 				cache.draw(layers[i], j, BLOCK_TILES);
83 			}
84 			cache.end();
85 		}
86 
87 		if (TimeUtils.nanoTime() - startTime >= 1000000000) {
88 			Gdx.app.log("TileTest", "fps: " + Gdx.graphics.getFramesPerSecond());
89 			startTime = TimeUtils.nanoTime();
90 		}
91 	}
92 }
93