【问题标题】:LibGDX resize window font drawingLibGDX 调整大小窗口字体绘图
【发布时间】:2014-11-30 16:03:42
【问题描述】:

我在 LibGDX 中绘制字体有问题。 我希望能够调整窗口大小并且文本将是可读的。 默认应用方向是纵向,像这样:

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

但我希望能够将应用程序的大小从纵向调整为横向。但是当我尝试它时(例如,从 "landscape" 到 "portrait" ,文本被拉伸了。

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

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){

    }
}

我做错了什么?为什么调整窗口大小后文本的布局没有改变?

【问题讨论】:

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


    【解决方案1】:

    尝试添加这个:

    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。

    【讨论】:

    • 谢谢,看来我得用OrtographicCamera了。但我不确定,以什么方式。请问你能提供一些样品吗?我只需要在正确调整大小后绘制文本字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2011-01-22
    • 2021-08-18
    • 2014-10-10
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多