• 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 com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.Input;
21 import com.badlogic.gdx.InputProcessor;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.Pixmap;
24 import com.badlogic.gdx.graphics.Pixmap.Format;
25 import com.badlogic.gdx.graphics.Texture;
26 import com.badlogic.gdx.graphics.Texture.TextureFilter;
27 import com.badlogic.gdx.graphics.g2d.Sprite;
28 import com.badlogic.gdx.graphics.g2d.SpriteCache;
29 import com.badlogic.gdx.math.MathUtils;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 import com.badlogic.gdx.utils.TimeUtils;
32 
33 public class SpriteCacheTest extends GdxTest implements InputProcessor {
34 	int SPRITES = 400 / 2;
35 
36 	long startTime = TimeUtils.nanoTime();
37 	int frames = 0;
38 
39 	Texture texture;
40 	Texture texture2;
41 	SpriteCache spriteCache;
42 	int normalCacheID, spriteCacheID;
43 	int renderMethod = 0;
44 
45 	private float[] sprites;
46 	private float[] sprites2;
47 
48 	@Override
render()49 	public void render () {
50 		if (renderMethod == 0) renderNormal();
51 		;
52 		if (renderMethod == 1) renderSprites();
53 	}
54 
renderNormal()55 	private void renderNormal () {
56 		Gdx.gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
57 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
58 
59 		float begin = 0;
60 		float end = 0;
61 		float draw1 = 0;
62 
63 		long start = TimeUtils.nanoTime();
64 		spriteCache.begin();
65 		begin = (TimeUtils.nanoTime() - start) / 1000000000.0f;
66 
67 		start = TimeUtils.nanoTime();
68 		spriteCache.draw(normalCacheID);
69 		draw1 = (TimeUtils.nanoTime() - start) / 1000000000.0f;
70 
71 		start = TimeUtils.nanoTime();
72 		spriteCache.end();
73 		end = (TimeUtils.nanoTime() - start) / 1000000000.0f;
74 
75 		if (TimeUtils.nanoTime() - startTime > 1000000000) {
76 // Gdx.app.log( "SpriteBatch", "fps: " + frames + ", render calls: " + spriteBatch.renderCalls + ", " + begin + ", " + draw1 +
77 // ", " + draw2 + ", " + drawText + ", " + end );
78 			frames = 0;
79 			startTime = TimeUtils.nanoTime();
80 		}
81 		frames++;
82 	}
83 
renderSprites()84 	private void renderSprites () {
85 		Gdx.gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
86 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
87 
88 		float begin = 0;
89 		float end = 0;
90 		float draw1 = 0;
91 		float draw2 = 0;
92 		float drawText = 0;
93 
94 		long start = TimeUtils.nanoTime();
95 		spriteCache.begin();
96 		begin = (TimeUtils.nanoTime() - start) / 1000000000.0f;
97 
98 		start = TimeUtils.nanoTime();
99 		spriteCache.draw(spriteCacheID);
100 		draw1 = (TimeUtils.nanoTime() - start) / 1000000000.0f;
101 
102 		start = TimeUtils.nanoTime();
103 		spriteCache.end();
104 		end = (TimeUtils.nanoTime() - start) / 1000000000.0f;
105 
106 		if (TimeUtils.nanoTime() - startTime > 1000000000) {
107 // Gdx.app.log( "SpriteBatch", "fps: " + frames + ", render calls: " + spriteBatch.renderCalls + ", " + begin + ", " + draw1 +
108 // ", " + draw2 + ", " + drawText + ", " + end );
109 			frames = 0;
110 			startTime = TimeUtils.nanoTime();
111 		}
112 		frames++;
113 	}
114 
115 	@Override
create()116 	public void create () {
117 		spriteCache = new SpriteCache(1000, true);
118 
119 		texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
120 		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
121 
122 		Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
123 		pixmap.setColor(1, 1, 0, 0.5f);
124 		pixmap.fill();
125 		texture2 = new Texture(pixmap);
126 		pixmap.dispose();
127 
128 		sprites = new float[SPRITES * 6];
129 		sprites2 = new float[SPRITES * 6];
130 		Sprite[] sprites3 = new Sprite[SPRITES * 2];
131 
132 		for (int i = 0; i < sprites.length; i += 6) {
133 			sprites[i] = (int)(Math.random() * (Gdx.graphics.getWidth() - 32));
134 			sprites[i + 1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32));
135 			sprites[i + 2] = 0;
136 			sprites[i + 3] = 0;
137 			sprites[i + 4] = 32;
138 			sprites[i + 5] = 32;
139 			sprites2[i] = (int)(Math.random() * (Gdx.graphics.getWidth() - 32));
140 			sprites2[i + 1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32));
141 			sprites2[i + 2] = 0;
142 			sprites2[i + 3] = 0;
143 			sprites2[i + 4] = 32;
144 			sprites2[i + 5] = 32;
145 		}
146 
147 		for (int i = 0; i < SPRITES * 2; i++) {
148 			int x = (int)(Math.random() * (Gdx.graphics.getWidth() - 32));
149 			int y = (int)(Math.random() * (Gdx.graphics.getHeight() - 32));
150 
151 			if (i >= SPRITES)
152 				sprites3[i] = new Sprite(texture2, 32, 32);
153 			else
154 				sprites3[i] = new Sprite(texture, 32, 32);
155 			sprites3[i].setPosition(x, y);
156 			sprites3[i].setOrigin(16, 16);
157 		}
158 
159 		float scale = 1;
160 		float angle = 15;
161 
162 		spriteCache.beginCache();
163 		for (int i = 0; i < sprites2.length; i += 6)
164 			spriteCache.add(texture2, sprites2[i], sprites2[i + 1], 16, 16, 32, 32, scale, scale, angle, 0, 0, 32, 32, false, false);
165 		for (int i = 0; i < sprites.length; i += 6)
166 			spriteCache.add(texture, sprites[i], sprites[i + 1], 16, 16, 32, 32, scale, scale, angle, 0, 0, 32, 32, false, false);
167 		normalCacheID = spriteCache.endCache();
168 
169 		angle = -15;
170 
171 		spriteCache.beginCache();
172 		for (int i = SPRITES; i < SPRITES << 1; i++) {
173 			sprites3[i].setRotation(angle);
174 			sprites3[i].setScale(scale);
175 			spriteCache.add(sprites3[i]);
176 		}
177 		for (int i = 0; i < SPRITES; i++) {
178 			sprites3[i].setRotation(angle);
179 			sprites3[i].setScale(scale);
180 			spriteCache.add(sprites3[i]);
181 		}
182 		spriteCacheID = spriteCache.endCache();
183 
184 		Gdx.input.setInputProcessor(this);
185 	}
186 
187 	@Override
keyDown(int keycode)188 	public boolean keyDown (int keycode) {
189 		if (keycode != Input.Keys.SPACE) return false;
190 		float scale = MathUtils.random(0.75f, 1.25f);
191 		float angle = MathUtils.random(1, 360);
192 		spriteCache.beginCache(normalCacheID);
193 		for (int i = 0; i < sprites2.length; i += 6)
194 			spriteCache.add(texture2, sprites2[i], sprites2[i + 1], 16, 16, 32, 32, scale, scale, angle, 0, 0, 32, 32, false, false);
195 		for (int i = 0; i < sprites.length; i += 6)
196 			spriteCache.add(texture, sprites[i], sprites[i + 1], 16, 16, 32, 32, scale, scale, angle, 0, 0, 32, 32, false, false);
197 		spriteCache.endCache();
198 		return false;
199 	}
200 
201 	@Override
touchUp(int x, int y, int pointer, int button)202 	public boolean touchUp (int x, int y, int pointer, int button) {
203 		renderMethod = (renderMethod + 1) % 2;
204 		return false;
205 	}
206 
207 	@Override
dispose()208 	public void dispose () {
209 		texture.dispose();
210 		texture2.dispose();
211 		spriteCache.dispose();
212 	}
213 }
214