zoukankan      html  css  js  c++  java
  • libgdx 图片打包 TexturePacker

    在OpenGL中,绘制图像需要绑定纹理,绑定纹理的代价很高消耗很大。

    试想,有1000张不同的小图片需要绘制,就需要绑定1000次。

    把这1000张小图,打包成一张大图,绑定时一次就够了。参照SQL批处理来理解吧。

    当然实际情况怎么打包,每关打一个包,UI打一个包等等这些我们不讨论。

    文档地址:https://github.com/libgdx/libgdx/wiki/Texture-packer

    libgdx内置了打包工具,命令是:

    // OS X / Linux
    java -cp gdx.jar:extensions/gdx-tools/gdx-tools.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir [outputDir] [packFileName]
    
    // WINDOWS
    java -cp gdx.jar;extensions/gdx-tools/gdx-tools.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir [outputDir] [packFileName]

    随便找几张小图,我们试一下

    localhost:libgdx-1.3.1 HanHongmin$ java -cp gdx.jar:extensions/gdx-tools/gdx-tools.jar com.badlogic.gdx.tools.texturepacker.TexturePacker /Users/HanHongmin/Documents/export
    export
    Packing.......
    Writing 256x128: /Users/HanHongmin/Documents/export-packed/pack.png

    只输入了一个inputDir 参数,默认生成了xxx-packed的文件夹,里面有张pack.png的大图,还有一个pack.atlas文件。

     1 pack.png
     2 size: 242,117
     3 format: RGBA8888
     4 filter: Nearest,Nearest
     5 repeat: none
     6 backed
     7   rotate: false
     8   xy: 152, 27
     9   size: 90, 90
    10   orig: 90, 90
    11   offset: 0, 0
    12   index: -1
    13 replay
    14   rotate: false
    15   xy: 2, 37
    16   size: 148, 80
    17   orig: 148, 80
    18   offset: 0, 0
    19   index: -1
    20 startGame
    21   rotate: false
    22   xy: 2, 2
    23   size: 99, 33
    24   orig: 99, 33
    25   offset: 0, 0
    26   index: -1

    xy应该就是每张小图的其实位置,看起来是大图的左上角是(0,0)点。其他参数很好理解,不过不是很有必要读懂它。

    这个还可以打包子文件夹!在inputDir参数的路径文件夹下建两个子文件夹,各方上几张图片试试。

    文件夹中还可以放置pack.json来配置打包,查看文档吧!

    使用打包的文件

     1 public class PackerTester extends ApplicationAdapter {
     2     private Stage stage;
     3     private TextureAtlas atlas;
     4     private Texture packed;
     5 
     6     @Override
     7     public void create () {
     8         Gdx.app.setLogLevel(Application.LOG_DEBUG);
     9         stage = new Stage();
    10 
    11 
    12         atlas = new TextureAtlas(Gdx.files.internal("packedimages/pack.atlas"));
    13         TextureAtlas.AtlasRegion region = atlas.findRegion("menu/backed");//找出来是那张大图
    14         packed = region.getTexture();
    15         Image imgPacked = new Image(packed);
    16         imgPacked.setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    17         stage.addActor(imgPacked);
    18         Sprite sprite = atlas.createSprite("game/backed");
    19         Image img = new Image(sprite);
    20         //NinePatch patch = atlas.createPatch("patchimagename");
    21         stage.addActor(img);
    22     }
    23 
    24     @Override
    25     public void render () {
    26         Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    27         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    28         stage.act();
    29         stage.draw();
    30     }
    31 
    32     @Override
    33     public void dispose() {
    34 
    35         atlas.dispose();
    36         stage.dispose();
    37         super.dispose();
    38     }
    39 
    40     @Override
    41     public void resize(int width, int height) {
    42         stage.getViewport().update(width,height);
    43         super.resize(width, height);
    44     }
    45 }

    上面代码中,game和menu分别是打包时的两个子文件夹。

     

  • 相关阅读:
    linux下QT程序输出乱码解决方法
    Qt中新建类构造函数的初始化参数列表
    移植tslib1.4至mini2440
    Linux中总线设备驱动模型及平台设备驱动实例
    igntie实现数据同步
    django-初始化举例
    django-总体
    django-admin层
    django-view层
    django-template层
  • 原文地址:https://www.cnblogs.com/hanhongmin/p/3964014.html
Copyright © 2011-2022 走看看