• 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.InputAdapter;
22 import com.badlogic.gdx.graphics.Color;
23 import com.badlogic.gdx.graphics.GL20;
24 import com.badlogic.gdx.graphics.OrthographicCamera;
25 import com.badlogic.gdx.graphics.Pixmap;
26 import com.badlogic.gdx.graphics.Pixmap.Format;
27 import com.badlogic.gdx.graphics.Texture.TextureFilter;
28 import com.badlogic.gdx.graphics.g2d.PixmapPacker;
29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
30 import com.badlogic.gdx.graphics.g2d.TextureAtlas;
31 import com.badlogic.gdx.graphics.g2d.TextureRegion;
32 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
33 import com.badlogic.gdx.tests.utils.GdxTest;
34 import com.badlogic.gdx.utils.Array;
35 
36 public class PixmapPackerTest extends GdxTest {
37 
38 	OrthographicCamera camera;
39 	SpriteBatch batch;
40 	ShapeRenderer shapeRenderer;
41 
42 	TextureAtlas atlas;
43 
44 	int pageToShow = 0;
45 	Array<TextureRegion> textureRegions;
46 
47 	@Override
create()48 	public void create () {
49 		batch = new SpriteBatch();
50 		shapeRenderer = new ShapeRenderer();
51 
52 		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
53 		camera.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
54 		camera.update();
55 
56 		Pixmap pixmap1 = new Pixmap(Gdx.files.internal("data/badlogic.jpg"));
57 		Pixmap pixmap2 = new Pixmap(Gdx.files.internal("data/particle-fire.png"));
58 		Pixmap pixmap3 = new Pixmap(Gdx.files.internal("data/isotile.png"));
59 
60 		PixmapPacker packer = new PixmapPacker(1024, 1024, Format.RGBA8888, 8, false);
61 		for (int count = 1; count <= 3; ++count) {
62 			packer.pack("badlogic " + count, pixmap1);
63 			packer.pack("fire " + count, pixmap2);
64 			packer.pack("isotile " + count, pixmap3);
65 		}
66 
67 		atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, false);
68 		Gdx.app.log("PixmapPackerTest", "Number of initial textures: " + atlas.getTextures().size);
69 
70 		packer.setPackToTexture(true);
71 
72 		for (int count = 4; count <= 10; ++count) {
73 			packer.pack("badlogic " + count, pixmap1);
74 			packer.pack("fire " + count, pixmap2);
75 			packer.pack("isotile " + count, pixmap3);
76 		}
77 
78 		pixmap1.dispose();
79 		pixmap2.dispose();
80 		pixmap3.dispose();
81 
82 		packer.updateTextureAtlas(atlas, TextureFilter.Nearest, TextureFilter.Nearest, false);
83 		textureRegions = new Array<TextureRegion>();
84 		packer.updateTextureRegions(textureRegions, TextureFilter.Nearest, TextureFilter.Nearest, false);
85 		Gdx.app.log("PixmapPackerTest", "Number of updated textures: " + atlas.getTextures().size);
86 		Gdx.input.setInputProcessor(new InputAdapter() {
87 			@Override
88 			public boolean keyDown (int keycode) {
89 				if (keycode >= Input.Keys.NUM_0 && keycode <= Input.Keys.NUM_9) {
90 					int number = keycode - Input.Keys.NUM_0;
91 					if (number < textureRegions.size) {
92 						pageToShow = number;
93 					}
94 				}
95 				return super.keyDown(keycode);
96 			}
97 		});
98 	}
99 
100 	@Override
render()101 	public void render () {
102 		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
103 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
104 		int size = Math.min(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
105 		batch.begin();
106 		batch.draw(textureRegions.get(pageToShow), 0, 0, size, size);
107 		batch.end();
108 		shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
109 		shapeRenderer.setColor(Color.GREEN);
110 		shapeRenderer.rect(0, 0, size, size);
111 		shapeRenderer.end();
112 	}
113 
114 	@Override
dispose()115 	public void dispose () {
116 		atlas.dispose();
117 	}
118 }
119