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.tools.texturepacker; 18 19 import java.util.Random; 20 21 import com.badlogic.gdx.ApplicationAdapter; 22 import com.badlogic.gdx.Gdx; 23 import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 24 import com.badlogic.gdx.graphics.Color; 25 import com.badlogic.gdx.graphics.GL20; 26 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 27 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 28 import com.badlogic.gdx.math.Matrix4; 29 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Page; 30 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect; 31 import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings; 32 import com.badlogic.gdx.utils.Array; 33 34 /** @author Nathan Sweet */ 35 public class TexturePackerTest extends ApplicationAdapter { 36 ShapeRenderer renderer; 37 Array<Page> pages; 38 39 public void create () { 40 renderer = new ShapeRenderer(); 41 } 42 43 public void render () { 44 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 45 46 Settings settings = new Settings(); 47 settings.fast = false; 48 settings.pot = false; 49 settings.maxWidth = 1024; 50 settings.maxHeight = 1024; 51 settings.rotation = false; 52 settings.paddingX = 0; 53 54 if (pages == null) { 55 Random random = new Random(1243); 56 Array<Rect> inputRects = new Array(); 57 for (int i = 0; i < 240; i++) { 58 Rect rect = new Rect(); 59 rect.name = "rect" + i; 60 rect.height = 16 + random.nextInt(120); 61 rect.width = 16 + random.nextInt(240); 62 inputRects.add(rect); 63 } 64 for (int i = 0; i < 10; i++) { 65 Rect rect = new Rect(); 66 rect.name = "rect" + (40 + i); 67 rect.height = 400 + random.nextInt(340); 68 rect.width = 1 + random.nextInt(10); 69 inputRects.add(rect); 70 } 71 72 long s = System.nanoTime(); 73 74 pages = new MaxRectsPacker(settings).pack(inputRects); 75 76 long e = System.nanoTime(); 77 System.out.println("fast: " + settings.fast); 78 System.out.println((e - s) / 1e6f + " ms"); 79 System.out.println(); 80 } 81 82 int x = 20, y = 20; 83 for (Page page : pages) { 84 renderer.setColor(Color.GRAY); 85 renderer.begin(ShapeType.Filled); 86 for (int i = 0; i < page.outputRects.size; i++) { 87 Rect rect = page.outputRects.get(i); 88 renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, 89 rect.height - settings.paddingY); 90 } 91 renderer.end(); 92 renderer.setColor(Color.RED); 93 renderer.begin(ShapeType.Line); 94 for (int i = 0; i < page.outputRects.size; i++) { 95 Rect rect = page.outputRects.get(i); 96 renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, 97 rect.height - settings.paddingY); 98 } 99 renderer.setColor(Color.GREEN); 100 renderer.rect(x, y, page.width + settings.paddingX * 2, page.height + settings.paddingY * 2); 101 renderer.end(); 102 x += page.width + 20; 103 } 104 } 105 106 public void resize (int width, int height) { 107 renderer.setProjectionMatrix(new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight())); 108 } 109 110 public static void main (String[] args) throws Exception { 111 new LwjglApplication(new TexturePackerTest(), "", 640, 480); 112 } 113 } 114