gpt4 book ai didi

java - 如何在 LibGDX 中绘制 BitmapFont?

转载 作者:搜寻专家 更新时间:2023-10-30 21:00:44 24 4
gpt4 key购买 nike

我敢打赌我做了一些愚蠢的事情,只是似乎没有注意到它。

package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Locked implements ApplicationListener
{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private BitmapFont font;
private CharSequence str = "Hello World!";
private float width;
private float height;

@Override
public void create()
{
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();

camera = new OrthographicCamera(1, height / width);
batch = new SpriteBatch();

texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2);

font = new BitmapFont(Gdx.files.internal("data/digib.fnt"),
Gdx.files.internal("data/digib.png"), false);
}

@Override
public void dispose()
{
batch.dispose();
texture.dispose();
}

@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

batch.setProjectionMatrix(camera.combined);
batch.begin();
font.setColor(0.0f, 0.0f, 0.0f, 1.0f);

//sprite.draw(batch);
font.draw(batch, str, width*0.5f, height*0.5f);
batch.end();
}

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

@Override
public void pause()
{
}

@Override
public void resume()
{
}
}

项目是用他们提供的模板工具gdx-setup-ui.jar生成的正如您在代码中看到的那样,我没有费心去删除默认代码(只是一些简单的绘制代码来呈现 LibGDX Logo )。

因此,对于干净生成的项目,我在这里遵循了本指南 http://code.google.com/p/libgdx-users/wiki/addingText2D

最后得到上面提供的代码。

问题是,为什么 !@#$ing 文本不显示!?我改变了很多次位置,但仍然没有运气:\

我错过了什么吗?

仅供引用:字体很好,我将它们放到另一个游戏中并且可以正常工作。

最佳答案

尝试像这样改变投影矩阵:

Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());

batch.setProjectionMatrix(normalProjection);

关于java - 如何在 LibGDX 中绘制 BitmapFont?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com