• 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.Texture;
23 import com.badlogic.gdx.graphics.Texture.TextureFilter;
24 import com.badlogic.gdx.graphics.g2d.Batch;
25 import com.badlogic.gdx.graphics.g2d.CpuSpriteBatch;
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
27 import com.badlogic.gdx.graphics.g2d.TextureRegion;
28 import com.badlogic.gdx.math.MathUtils;
29 import com.badlogic.gdx.scenes.scene2d.Actor;
30 import com.badlogic.gdx.scenes.scene2d.Group;
31 import com.badlogic.gdx.scenes.scene2d.Stage;
32 import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
33 import com.badlogic.gdx.scenes.scene2d.utils.TransformDrawable;
34 import com.badlogic.gdx.tests.utils.GdxTest;
35 import com.badlogic.gdx.utils.Align;
36 import com.badlogic.gdx.utils.TimeUtils;
37 import com.badlogic.gdx.utils.viewport.ExtendViewport;
38 import com.badlogic.gdx.utils.viewport.Viewport;
39 
40 public class CpuSpriteBatchTest extends GdxTest {
41 
42 	private static class DrawableActor extends Actor {
43 		private final TransformDrawable drawable;
44 
DrawableActor(TransformDrawable drawable)45 		public DrawableActor (TransformDrawable drawable) {
46 			this.drawable = drawable;
47 			setSize(drawable.getMinWidth(), drawable.getMinHeight());
48 		}
49 
draw(Batch batch, float parentAlpha)50 		public void draw (Batch batch, float parentAlpha) {
51 			Color color = getColor();
52 			batch.setColor(color.r, color.g, color.b, parentAlpha);
53 			drawable.draw(batch, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(),
54 				getRotation());
55 		}
56 	}
57 
58 	private static final int NUM_GROUPS = 1000;
59 
60 	private Stage stage;
61 	private Texture texture;
62 
63 	private long sampleStartTime;
64 	private long sampleFrames;
65 
create()66 	public void create () {
67 		Batch batch = new CpuSpriteBatch();
68 		// batch = new SpriteBatch();
69 
70 		stage = new Stage(new ExtendViewport(500, 500), batch);
71 
72 		Gdx.input.setInputProcessor(stage);
73 
74 		texture = new Texture("data/bobargb8888-32x32.png");
75 		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
76 
77 		TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(texture));
78 
79 		for (int i = 0; i < NUM_GROUPS; i++) {
80 			Group group = createActorGroup(drawable);
81 			stage.addActor(group);
82 		}
83 	}
84 
createActorGroup(TextureRegionDrawable bob)85 	private Group createActorGroup (TextureRegionDrawable bob) {
86 		Actor main = new DrawableActor(bob);
87 		main.setPosition(0, 0, Align.center);
88 
89 		Actor hat = new DrawableActor(bob) {
90 			@Override
91 			public void act (float delta) {
92 				rotateBy(delta * -300);
93 			}
94 		};
95 		hat.setOrigin(Align.center);
96 		hat.setScale(0.5f);
97 		hat.setPosition(0, 21, Align.center);
98 
99 		Group group = new Group() {
100 			@Override
101 			public void act (float delta) {
102 				rotateBy(delta * 120);
103 				setScale(0.9f + 0.2f * MathUtils.cos(MathUtils.degreesToRadians * getRotation()));
104 				super.act(delta);
105 			}
106 		};
107 		group.addActor(main);
108 		group.addActor(hat) ;
109 		// group.setTransform(false);
110 
111 		float margin = 35;
112 		float x = MathUtils.random(margin, stage.getWidth() - margin);
113 		float y = MathUtils.random(margin, stage.getHeight() - margin);
114 		group.setPosition(x, y);
115 		group.setRotation(MathUtils.random(0, 360));
116 
117 		return group;
118 	}
119 
render()120 	public void render () {
121 		Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
122 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
123 
124 		stage.act(Gdx.graphics.getDeltaTime());
125 		stage.draw();
126 
127 		long now = TimeUtils.nanoTime();
128 		sampleFrames++;
129 
130 		if (now - sampleStartTime > 1000000000) {
131 			if (sampleStartTime != 0) {
132 				int renderCalls = ((SpriteBatch)stage.getBatch()).renderCalls;
133 				Gdx.app.log("CpuSpriteBatch", "FPS: " + sampleFrames + ", render calls: " + renderCalls);
134 			}
135 			sampleStartTime = now;
136 			sampleFrames = 0;
137 		}
138 	}
139 
resize(int width, int height)140 	public void resize (int width, int height) {
141 		stage.getViewport().update(width, height, true);
142 	}
143 
dispose()144 	public void dispose () {
145 		stage.dispose();
146 		texture.dispose();
147 	}
148 }
149