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.Keys; 21 import com.badlogic.gdx.InputAdapter; 22 import com.badlogic.gdx.graphics.GL20; 23 import com.badlogic.gdx.graphics.g2d.Animation; 24 import com.badlogic.gdx.graphics.g2d.BitmapFont; 25 import com.badlogic.gdx.graphics.g2d.Sprite; 26 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 27 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 28 import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; 29 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 30 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 31 import com.badlogic.gdx.tests.utils.GdxTest; 32 33 public class TextureAtlasTest extends GdxTest { 34 SpriteBatch batch; 35 Sprite badlogic, badlogicSmall, star; 36 TextureAtlas atlas; 37 TextureAtlas jumpAtlas; 38 Animation jumpAnimation; 39 BitmapFont font; 40 float time = 0; 41 ShapeRenderer renderer; 42 create()43 public void create () { 44 batch = new SpriteBatch(); 45 renderer = new ShapeRenderer(); 46 47 atlas = new TextureAtlas(Gdx.files.internal("data/pack")); 48 jumpAtlas = new TextureAtlas(Gdx.files.internal("data/jump.txt")); 49 50 jumpAnimation = new Animation(0.25f, jumpAtlas.findRegions("ALIEN_JUMP_")); 51 52 badlogic = atlas.createSprite("badlogicslice"); 53 badlogic.setPosition(50, 50); 54 55 // badlogicSmall = atlas.createSprite("badlogicsmall"); 56 badlogicSmall = atlas.createSprite("badlogicsmall-rotated"); 57 badlogicSmall.setPosition(10, 10); 58 59 AtlasRegion region = atlas.findRegion("badlogicsmall"); 60 System.out.println("badlogicSmall original size: " + region.originalWidth + ", " + region.originalHeight); 61 System.out.println("badlogicSmall packed size: " + region.packedWidth + ", " + region.packedHeight); 62 63 star = atlas.createSprite("particle-star"); 64 star.setPosition(10, 70); 65 66 font = new BitmapFont(Gdx.files.internal("data/font.fnt"), atlas.findRegion("font"), false); 67 68 Gdx.gl.glClearColor(0, 1, 0, 1); 69 70 Gdx.input.setInputProcessor(new InputAdapter() { 71 public boolean keyUp (int keycode) { 72 if (keycode == Keys.UP) { 73 badlogicSmall.flip(false, true); 74 } else if (keycode == Keys.RIGHT) { 75 badlogicSmall.flip(true, false); 76 } else if (keycode == Keys.LEFT) { 77 badlogicSmall.setSize(512, 512); 78 } else if (keycode == Keys.DOWN) { 79 badlogicSmall.rotate90(true); 80 } 81 return super.keyUp(keycode); 82 } 83 }); 84 } 85 render()86 public void render () { 87 time += Gdx.graphics.getDeltaTime(); 88 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 89 90 renderer.begin(ShapeType.Line); 91 renderer.rect(10, 10, 256, 256); 92 renderer.end(); 93 94 batch.begin(); 95 // badlogic.draw(batch); 96 // star.draw(batch); 97 // font.draw(batch, "This font was packed!", 26, 65); 98 badlogicSmall.draw(batch); 99 // batch.draw(jumpAnimation.getKeyFrame(time, true), 100, 100); 100 batch.end(); 101 } 102 dispose()103 public void dispose () { 104 atlas.dispose(); 105 jumpAtlas.dispose(); 106 batch.dispose(); 107 font.dispose(); 108 } 109 } 110