Libgdx 遮盖例子

来自 261954621 群友分享
下面是使用到的纹理

grass.png
grass.png

mask.png
mask.png

package osg.net.abcs.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GdxMask implements ApplicationListener {
    public static void main(String[] args) {
        new LwjglApplication(new GdxMask());
    }

    OrthographicCamera cam;
    SpriteBatch batch;
    Texture bg, sprite, alphaMask;

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

        sprite = new Texture("assets/data/grass.png");
        alphaMask = new Texture("assets/data/mask.png");
        alphaMask.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    }

    @Override
    public void resize (int width, int height) {
        cam.setToOrtho(false, width, height);
        batch.setProjectionMatrix(cam.combined);
    }

    private void drawBackground(SpriteBatch batch) {
        //regular blending mode
        batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);


        //... draw background entities/tiles here ...


        //flush the batch to the GPU
        batch.flush();
    }

    private void drawAlphaMask(SpriteBatch batch, float x, float y, float width, float height) {
        //disable RGB color, only enable ALPHA to the frame buffer
        Gdx.gl.glColorMask(false, false, false, true);

        //change the blending function for our alpha map
        batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);

        //draw alpha mask sprite(s)
        batch.draw(alphaMask, x, y, width, height);

        //flush the batch to the GPU
        batch.flush();
    }

    private void drawForeground(SpriteBatch batch, int clipX, int clipY, int clipWidth, int clipHeight) {
        //now that the buffer has our alpha, we simply draw the sprite with the mask applied
        Gdx.gl.glColorMask(true, true, true, true);
        batch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);

        //The scissor test is optional, but it depends 
        Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
        Gdx.gl.glScissor(clipX, clipY, clipWidth, clipHeight);

        //draw our sprite to be masked
        batch.draw(sprite, 0, 0, 250, 250);

        //remember to flush before changing GL states again
        batch.flush();

        //disable scissor before continuing
        Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        //start the batch
        batch.begin();

        //draw background
        drawBackground(batch);


        //the sprite we want the circle mask applied to
        int x = 25;
        int y = 50;
        int spriteWidth = 200;
        int spriteHeight = 200;

        //draw the alpha mask
        drawAlphaMask(batch, x, y, spriteWidth, spriteHeight);

        //draw our foreground elements
        drawForeground(batch, x, y, spriteWidth, spriteHeight);

        batch.end();
    }

    @Override
    public void pause () {
    }

    @Override
    public void resume () {
    }

    @Override
    public void dispose () {
        batch.dispose();
        alphaMask.dispose();
        sprite.dispose();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值