我认真地打赌我做了一些愚蠢的事情,似乎无法注意到它.
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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
该项目是使用他们提供的模板工具生成的gdx-setup-ui.jar 正如您在代码中看到的那样,我没有费心去除默认代码(只是一些简单的绘图代码来渲染LibGDX徽标).
因此,通过干净生成的项目,我在此处遵循了本指南 http://code.google.com/p/libgdx-users/wiki/addingText2D
最后使用上面提供的代码到达.
问题是,为什么不会!@#$ ing文字显示!?我改变了这么多次,但仍然没有运气:
我错过了什么?
仅供参考:字体很好,我把它们放到另一个游戏中然后就可以了.
小智 8
尝试更改投影矩阵,如下所示:
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(normalProjection);
Run Code Online (Sandbox Code Playgroud)
小智 8
我所做的就是
spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/nameOfFont.fnt"),
Gdx.files.internal("data/nameOfFont.png"), false);
Run Code Online (Sandbox Code Playgroud)
并在渲染方法
spriteBatch.begin();
font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
font.draw(spriteBatch, "some string", 25, 160);
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)
您可以在我的博客上阅读更多相关信息:http://algorhymes.wordpress.com/2012/11/17/javalibgdx-fonts/
就个人而言,我不是将所有字体转换为.fnt格式的忠实粉丝.如果某种字体需要不同的尺寸,则必须花费大量时间(和应用空间)进行所有转换.
您可以使用FreeType扩展并直接从.ttf加载
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateFont(15);
BitmapFont font22 = generator.generateFont(22);
generator.dispose();
Run Code Online (Sandbox Code Playgroud)
更多信息在这里
渲染的方式与watis解释的相同.
| 归档时间: |
|
| 查看次数: |
29295 次 |
| 最近记录: |