LibGDX调整窗口字体绘图的大小

时间:2014-10-06 11:41:36

标签: java layout opengl-es libgdx font-size

我在LibGDX中遇到字体绘制问题。 我希望能够调整窗口大小,文本将是可读的。 默认应用程序方向为纵向,如下所示:

Portrait Application

我可以在我的应用程序中开始构建之前更改它,一切正常:

Landscape Application

但我希望能够将应用程序从纵向调整到横向。但是当我尝试它时(例如,从“风景”到“肖像”,文本被拉伸。

Resized Application

我的渲染和调整大小函数代码(它只是从头开始的代码):

public void render () {

    try
    {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //background draw
        //_spriteBatch.begin();
        //_spriteBatch.draw(background, 0, 0);
        //_spriteBatch.end();

        _spriteBatch.begin();
        //sprite.draw(batch);
        elapsedTime += Gdx.graphics.getDeltaTime();
        _spriteBatch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);
        _spriteBatch.end();


        //content draw
        _spriteBatch.begin();

        if(!_isLandscape)
          _spriteBatch.draw(img, 20, 210, 30, 45);

        //NameDayDOM nameDayDOM = new NameDayDOM();
        NameDaySAX nameDaySAX = new NameDaySAX();
        name = nameDaySAX.getCurrentName(_serviceProvider.getAssetHandleResolver().resolve("namesday.xml").file());
       //II _font.draw(_spriteBatch, "Happy Name Day, dear " + name, Gdx.graphics.getWidth() / 7.0f, Gdx.graphics.getHeight() / 2.0f);

        if(!_isLandscape) {
            BitmapFont.TextBounds bounds = _font40.getBounds("Happy Name Day");
            _font40.draw(_spriteBatch, "Happy Name Day", (Gdx.graphics.getWidth()-bounds.width) / 5.0f, Gdx.graphics.getHeight() - 120);
            bounds = _font30.getBounds("dear" + name);
            _font30.draw(_spriteBatch, "dear  " + name, (Gdx.graphics.getWidth()-bounds.width) / 3.2f, Gdx.graphics.getHeight() - 200);
            bounds = _font23.getBounds("Post picture from this venue");
            _font23.draw(_spriteBatch, "Post picture from this venue", (Gdx.graphics.getWidth()-bounds.width) / 9.0f, Gdx.graphics.getHeight() / 4.7f);
            bounds = _font23.getBounds("and tell to world,");
            _font23.draw(_spriteBatch, "and tell to world,", (Gdx.graphics.getWidth()-bounds.width) / 7.0f, Gdx.graphics.getHeight() / 5.5f);
            bounds = _font23.getBounds("that you are enjoying your NameDay!");
            _font23.draw(_spriteBatch, "that you are enjoying your NameDay!", (Gdx.graphics.getWidth()-bounds.width) / 2.0f, Gdx.graphics.getHeight() / 6.5f);
        }else{
            BitmapFont.TextBounds bounds = _font40.getBounds("Happy Name Day");
            _font40.draw(_spriteBatch, "Happy Name Day", (Gdx.graphics.getWidth()-bounds.width) / 14.0f, Gdx.graphics.getHeight() / 1.7f);
            bounds = _font30.getBounds("dear" + name);
            _font30.draw(_spriteBatch, "dear  " + name, (Gdx.graphics.getWidth()-bounds.width) / 11.0f, Gdx.graphics.getHeight() / 2.0f);
            bounds = _font23.getBounds("Post picture from this venue");
            _font23.draw(_spriteBatch, "Post picture from this venue", (Gdx.graphics.getWidth()-bounds.width) / 14.0f, Gdx.graphics.getHeight() / 4.7f);
            bounds = _font23.getBounds("and tell to world,");
            _font23.draw(_spriteBatch, "and tell to world,", (Gdx.graphics.getWidth()-bounds.width) / 11.0f, Gdx.graphics.getHeight() / 5.5f);
            bounds = _font23.getBounds("that you are enjoying your NameDay!");
            _font23.draw(_spriteBatch, "that you are enjoying your NameDay!", (Gdx.graphics.getWidth()-bounds.width) / 9.0f, Gdx.graphics.getHeight() / 6.5f);
        }
    }
    catch(RuntimeException e)
    {
        _log.error("Exception on render: " + e);
        _serviceProvider.getAppStatusListener().onStateChanged(AppState.Error);
    }
    finally {
        _spriteBatch.end();
    }
}

@Override
public void resize(int width , int height){
    try {
        System.out.println(width + " " + height);
        if (height > width) {
            _isLandscape = false;
        } else {
            _isLandscape = true;
        }
    }catch(Exception ex){

    }
}

我做得不好?为什么在调整窗口大小后文本的布局没有改变?

1 个答案:

答案 0 :(得分:0)

尝试添加此内容:

Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); //<--This.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

这是因为你的窗口改变了大小,但OpenGL视口却没有。我强烈建议您使用OrtographicCamera代替。

相关问题