【问题标题】:Draw a frame with textures in libgdx在 libgdx 中绘制带有纹理的框架
【发布时间】:2017-12-03 19:34:53
【问题描述】:

我正在尝试在 Libgdx 中绘制具有两个纹理的框架。我的纹理只是一个白色方块,我用 SpriteBatch 缩放并绘制到屏幕上。颜色设置为batch.setColor()。现在我想在中间画一个黑色矩形和一个较小的不透明矩形,所以它看起来像一个框架。

batch.begin();
batch.setColor(new Color(0,0,0,1)); //Black
batch.draw(rectangle, 0,0,100,100);
batch.setColor(new Color(0,0,0,0)); //Transparent
batch.draw(rectangle, 25,25,50,50);
batch.end();

我正在使用这个混合功能:
Gdx.gl.glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR); 现在的问题是,当我绘制它时,它只显示黑色矩形,因为透明矩形绘制在它上面。我想画它,我可以通过第二个矩形看到我之前画的东西,所以它就像一个框架。 我的问题是:我怎样才能做到这一点?

Example Background

How it should look

How it looks

【问题讨论】:

    标签: android opengl-es libgdx blending


    【解决方案1】:

    首先,您不应该在 render() 方法中创建新的 Color 实例,因为它在每一帧都被调用,这会导致内存泄漏。例如,在 create() 中创建颜色。对于黑色,您可以使用 Color.BLACK 而不是创建自己的实例。

    回到你的问题。 你可以简单地使用TextureRegion 类。这是一个示例代码:

    public class MyGdxGame extends ApplicationAdapter {
    
        SpriteBatch batch;
        Texture whiteRectangle;
        TextureRegion backgroundRegion;
    
        @Override
        public void create() {
            //initializing batch, whiteTexture, etc...
    
            //background, the one with a turtle from you example
            Texture backgroundTexture = ...;
    
            backgroundRegion = new TextureRegion(backgroundTexture);
            //to set a region (part of the texture you want to draw),
            //you can use normalized coordinates (from 0 to 1), or absolute values in pixels
            backgroundRegion.setRegion(0.25f, 0.25f, 0.75f, 0.75f);
        }
    
        @Override
        public void render() {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.begin();
    
            //black background
            batch.setColor(Color.BLACK);
            batch.draw(whiteRectangle, 0, 0, 100, 100);
    
            //on top of that background draw the backgroundRegion
            batch.setColor(Color.WHITE);
            batch.draw(backgroundRegion, 25,25,50,50);
    
            batch.end();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 2019-08-04
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2014-09-05
      • 2016-06-29
      相关资源
      最近更新 更多