libgdx - 使用SpriteBatch并排渲染两个纹理并使用一个摄像头

时间:2015-09-27 01:29:28

标签: libgdx textures sprite spritebatch

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

目前,在我用SpriteBatch绘制了两个纹理之后,我试图用精灵绘制这样的SpriteBatch。但是在调用sprite.draw()时我得到了nullpointer。

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

我目前的代码:

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();
}

1 个答案:

答案 0 :(得分:0)

Sprite只是一个Texture(或TextureRegion)的包装器(为了添加更多功能)。

你只需要打电话给#34;新的Sprite();"它会创建一个没有纹理的精灵(所以没有任何东西可以绘制,没有图像信息等),因此当你试图绘制精灵时会得到一个空指针异常。

由于你想要组合两个纹理,为什么不用外部工具(如pain.net或gimp,...)创建单个纹理(图像)?

如果要将两个纹理绘制为单个对象,则需要创建自己的类,例如:

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();
    }
}