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.GL20; 21 import com.badlogic.gdx.graphics.Texture; 22 import com.badlogic.gdx.graphics.g2d.BitmapFont; 23 import com.badlogic.gdx.graphics.g2d.Sprite; 24 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 25 import com.badlogic.gdx.graphics.g2d.TextureRegion; 26 import com.badlogic.gdx.scenes.scene2d.Actor; 27 import com.badlogic.gdx.scenes.scene2d.Group; 28 import com.badlogic.gdx.scenes.scene2d.Stage; 29 import com.badlogic.gdx.scenes.scene2d.ui.Image; 30 import com.badlogic.gdx.tests.utils.GdxTest; 31 import com.badlogic.gdx.utils.Array; 32 import com.badlogic.gdx.utils.Scaling; 33 import com.badlogic.gdx.utils.viewport.ScalingViewport; 34 35 import java.util.Random; 36 37 public class StagePerformanceTest extends GdxTest { 38 39 Texture texture; 40 TextureRegion[] regions; 41 Stage stage; 42 SpriteBatch batch; 43 BitmapFont font; 44 Sprite[] sprites; 45 boolean useStage = true; 46 47 @Override create()48 public void create () { 49 batch = new SpriteBatch(); 50 font = new BitmapFont(); 51 stage = new Stage(new ScalingViewport(Scaling.fit, 24, 12)); 52 regions = new TextureRegion[8 * 8]; 53 sprites = new Sprite[24 * 12]; 54 55 texture = new Texture(Gdx.files.internal("data/badlogic.jpg")); 56 for (int y = 0; y < 8; y++) { 57 for (int x = 0; x < 8; x++) { 58 regions[x + y * 8] = new TextureRegion(texture, x * 32, y * 32, 32, 32); 59 } 60 } 61 62 Random rand = new Random(); 63 for (int y = 0, i = 0; y < 12; y++) { 64 for (int x = 0; x < 24; x++) { 65 Image img = new Image(regions[rand.nextInt(8 * 8)]); 66 img.setBounds(x, y, 1, 1); 67 stage.addActor(img); 68 sprites[i] = new Sprite(regions[rand.nextInt(8 * 8)]); 69 sprites[i].setPosition(x, y); 70 sprites[i].setSize(1, 1); 71 i++; 72 } 73 } 74 } 75 76 @Override render()77 public void render () { 78 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 79 80 if (useStage) { 81 stage.act(Gdx.graphics.getDeltaTime()); 82 stage.getBatch().disableBlending(); 83 Group root = stage.getRoot(); 84 Array<Actor> actors = root.getChildren(); 85 // for(int i = 0; i < actors.size(); i++) { 86 // actors.get(i).rotation += 45 * Gdx.graphics.getDeltaTime(); 87 // } 88 stage.draw(); 89 } else { 90 batch.getProjectionMatrix().setToOrtho2D(0, 0, 24, 12); 91 batch.getTransformMatrix().idt(); 92 batch.disableBlending(); 93 batch.begin(); 94 for (int i = 0; i < sprites.length; i++) { 95 // sprites[i].rotate(45 * Gdx.graphics.getDeltaTime()); 96 sprites[i].draw(batch); 97 } 98 batch.end(); 99 } 100 101 batch.getProjectionMatrix().setToOrtho2D(0, 0, 480, 320); 102 batch.enableBlending(); 103 batch.begin(); 104 font.setColor(0, 0, 1, 1); 105 font.getData().setScale(2); 106 font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() + (useStage ? ", stage" : "sprite"), 10, 40); 107 batch.end(); 108 109 if (Gdx.input.justTouched()) { 110 useStage = !useStage; 111 } 112 } 113 114 @Override dispose()115 public void dispose () { 116 stage.dispose(); 117 batch.dispose(); 118 font.dispose(); 119 texture.dispose(); 120 } 121 } 122