在调整大小时缩放libgdx中的纹理

时间:2015-07-01 22:14:55

标签: graphics libgdx game-engine

每次渲染函数调用时,我都会将纹理设置为调整为Gdx.graphics.getWidth()Gdx.graphics.getHeight()。事实上,当我启动应用程序时,纹理设置为全屏(我想要)。但是当我改变应用程序的大小时,纹理会以一种愚蠢的方式绘制,而我希望它始终是全屏的。

以下是代码:

    package com.leopikinc.bobdestroyer;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class BobDestroyer extends ApplicationAdapter {
    SpriteBatch batch;
    Texture mainscreen;
    Music intromusic;
    float VOLUME;

    @Override
    public void create () {
        VOLUME = 1f;
        batch = new SpriteBatch();
        mainscreen = new Texture(Gdx.files.internal("data/FirstLevel.png"));
        intromusic = Gdx.audio.newMusic(Gdx.files.internal("data/IntroMusic.mp3"));
        intromusic.setLooping(true);
        intromusic.setVolume(VOLUME);
        intromusic.play();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(mainscreen, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
    }

    @Override
    public void resize(int width, int height){
    }
}

Nice way

Dumb way

1 个答案:

答案 0 :(得分:0)

您应该使用viewport来处理不同的宽高比。

以下是可以显示全屏纹理的ioctl示例。

StretchViewport

在更改屏幕尺寸之前:

before changing screen sizes 更改屏幕尺寸后:

after changing screen sizes