• 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.graphics.Color;
21 import com.badlogic.gdx.graphics.GL20;
22 import com.badlogic.gdx.graphics.Mesh;
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.g2d.SpriteBatch;
27 import com.badlogic.gdx.tests.utils.GdxTest;
28 import com.badlogic.gdx.utils.TimeUtils;
29 
30 public class SpriteBatchShaderTest extends GdxTest {
31 	int SPRITES = 400;
32 
33 	long startTime = TimeUtils.nanoTime();
34 	int frames = 0;
35 
36 	Texture texture;
37 	Texture texture2;
38 // Font font;
39 	SpriteBatch spriteBatch;
40 	int coords[] = new int[SPRITES * 2];
41 	int coords2[] = new int[SPRITES * 2];
42 
43 	Color col = new Color(1, 1, 1, 0.6f);
44 
45 	Mesh mesh;
46 	float vertices[] = new float[SPRITES * 6 * (2 + 2 + 4)];
47 
48 	@Override
render()49 	public void render () {
50 		GL20 gl = Gdx.gl20;
51 		gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
52 		gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
53 
54 		float begin = 0;
55 		float end = 0;
56 		float draw1 = 0;
57 		float draw2 = 0;
58 		float drawText = 0;
59 
60 		long start = TimeUtils.nanoTime();
61 		spriteBatch.begin();
62 		begin = (TimeUtils.nanoTime() - start) / 1000000000.0f;
63 
64 		int len = coords.length;
65 		start = TimeUtils.nanoTime();
66 		for (int i = 0; i < len; i += 2)
67 			spriteBatch.draw(texture, coords[i], coords[i + 1], 0, 0, 32, 32);
68 		draw1 = (TimeUtils.nanoTime() - start) / 1000000000.0f;
69 
70 		start = TimeUtils.nanoTime();
71 		spriteBatch.setColor(col);
72 		for (int i = 0; i < coords2.length; i += 2)
73 			spriteBatch.draw(texture2, coords2[i], coords2[i + 1], 0, 0, 32, 32);
74 		draw2 = (TimeUtils.nanoTime() - start) / 1000000000.0f;
75 
76 		start = TimeUtils.nanoTime();
77 // spriteBatch.drawText(font, "Question?", 100, 300, Color.RED);
78 // spriteBatch.drawText(font, "and another this is a test", 200, 100, Color.WHITE);
79 // spriteBatch.drawText(font, "all hail and another this is a test", 200, 200, Color.WHITE);
80 		drawText = (TimeUtils.nanoTime() - start) / 1000000000.0f;
81 
82 		start = TimeUtils.nanoTime();
83 		spriteBatch.end();
84 		end = (TimeUtils.nanoTime() - start) / 1000000000.0f;
85 
86 		if (TimeUtils.nanoTime() - startTime > 1000000000) {
87 			Gdx.app.log("SpriteBatch", "fps: " + frames + ", render calls: " + spriteBatch.renderCalls + ", " + begin + ", " + draw1
88 				+ ", " + draw2 + ", " + drawText + ", " + end);
89 			frames = 0;
90 			startTime = TimeUtils.nanoTime();
91 		}
92 		frames++;
93 	}
94 
95 	@Override
create()96 	public void create () {
97 		spriteBatch = new SpriteBatch();
98 		texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
99 
100 		Pixmap pixmap = new Pixmap(32, 32, Format.RGB565);
101 		pixmap.setColor(1, 1, 0, 0.7f);
102 		pixmap.fill();
103 		texture2 = new Texture(pixmap);
104 		pixmap.dispose();
105 
106 		for (int i = 0; i < coords.length; i += 2) {
107 			coords[i] = (int)(Math.random() * Gdx.graphics.getWidth());
108 			coords[i + 1] = (int)(Math.random() * Gdx.graphics.getHeight());
109 			coords2[i] = (int)(Math.random() * Gdx.graphics.getWidth());
110 			coords2[i + 1] = (int)(Math.random() * Gdx.graphics.getHeight());
111 		}
112 	}
113 
114 	@Override
dispose()115 	public void dispose () {
116 		spriteBatch.dispose();
117 		texture.dispose();
118 		texture2.dispose();
119 	}
120 }
121