libgdx - 使用 SpriteBatch 并排渲染两个纹理并使用一个相机

libgdx - Render two textures side by side using SpriteBatch and use one camera

我想并排渲染两个纹理。然后将这两种纹理的这种组合视为一个使用单个相机的精灵。我可以使用 SpriteBatch 并排渲染两个纹理。如何将这两个纹理组合成一个精灵并使用相机获得不同的视图?

目前我用SpriteBatch画了两个纹理后,我正在尝试用精灵画这样的SpriteBatch。但是我在调​​用 sprite.draw() 时得到空指针。

我的最终目标是并排渲染两个图像用作背景,并能够在我的背景上使用相机。有任何想法吗?谢谢

我当前的代码:

public void create () {
    batch = new SpriteBatch();
    //create two texture for 1.jpg and 2.jpg
    //use batch to draw them separately at different position
    img_1 = new Texture("1.jpg"); 
    img_2 = new Texture("2.jpg"); 

    mWorld = new Sprite();
    mWorld.setPosition(0, 0);
    mWorld.setSize(WORLD_WIDTH,WORLD_HEIGHT);
    float aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
    camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * aspectRatio);
    camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
    camera.update();
}

@Override
public void render () {
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(this.img_1, 0, 0, WORLD_WIDTH/2, WORLD_HEIGHT);
    batch.draw(this.img_2, WORLD_WIDTH/2, 0, WORLD_WIDTH/2, WORLD_HEIGHT);

    //getting java nullpointer exception here
    mWorld.draw(batch);
    batch.end();
}

Sprite 只是单个纹理(或 TextureRegion)的包装器(以添加更多功能)。

您只需调用 "new Sprite();" 即可创建一个没有纹理的精灵(因此没有任何内容可绘制,没有图像信息等),因此当您尝试绘制精灵时会出现空指针异常。

既然你想合并两个纹理,为什么不使用外部工具(如 pain.net 或 gimp,..)创建一个纹理(图像)?

如果要将两个纹理绘制为一个对象,需要自己创建class,例如:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class TestGame extends ApplicationAdapter
{

    public class TwoTextures
    {
        private final Texture   tex1, tex2;
        private final float     pos1X, pos1Y, pos2X;

        public TwoTextures(Texture tex1, Texture tex2, float posX, float posY)
        {
            this.tex1 = tex1;
            this.tex2 = tex2;
            this.pos1X = posX;
            this.pos1Y = posY;

            this.pos2X = posX + tex1.getWidth();
        }

        public void draw(Batch batch)
        {
            batch.draw(tex1, pos1X, pos1Y);
            batch.draw(tex2, pos2X, pos1Y);
        }

        public void dispose()
        {
            tex1.dispose();
            tex2.dispose();
        }
    }

    OrthographicCamera  camera;

    Batch               batch;

    TwoTextures         background;

    @Override
    public void create()
    {
        batch = new SpriteBatch();
        camera = new OrthographicCamera();

        background = new TwoTextures(
                new Texture("1.jpg"),
                new Texture("2.jpg"),
                0,
                0);
    }

    @Override
    public void render()
    {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        {
            background.draw(batch);
        }
        batch.end();
    }

    @Override
    public void dispose()
    {
        batch.dispose();
        background.dispose();
    }
}