Reputation: 490
I loaded truetype fonts by using gdx-freetype at the beginning of the game. Loading takes too long, so I want to do the font loading in another thread. But when it finished loading, the fonts doesn't render correctly. Like this
But when I turned the screen off and on again it shows up correctly:
I want to know what toggles this change when the screen turns off and on again, and how should I fix it.
Code:
public class Game extends com.badlogic.gdx.Game {
SpriteBatch batch;
BitmapFont engFont;
AsyncResult<Void> loadResult;
static final int width = 800, height = 480;
@Override
public void create() {
batch = new SpriteBatch();
loadResult = new AsyncExecutor(10).submit(new LoadGameTask(this));
setScreen(new LogoScreen(this));
} // omitted
The LoadGameTask Class:
class LoadGameTask implements AsyncTask<Void> {
Game game;
LoadGameTask(Game game) {
this.game = game;
}
@Override
public Void call() throws Exception {
FreeTypeFontGenerator gen;
gen = new FreeTypeFontGenerator(Gdx.files.internal("data/square.ttf"));
game.engFont = gen.generateFont(100, Chars.chars, false);
gen.dispose();
game.engFont.setColor(1, 1, 1, 1);
game.engFont.getRegion().getTexture().setFilter(
Texture.TextureFilter.Linear, Texture.TextureFilter.Linear
);
return null;
}
The LogoScreen Class
public class LogoScreen implements Screen{
Game game;
OrthographicCamera camera;
public LogoScreen(Game game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, Game.width, Game.height);
}
@Override
public void render(float v) {
Gdx.gl.glClearColor(0.1f, 0.5f, 0.8f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(game.loadResult.isDone()) {
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.engFont.draw(
game.batch,
"Tenplus 한글",
Game.width / 2, Game.height / 2
);
game.batch.end();
}
}//omitted
Upvotes: 2
Views: 434
Reputation: 25177
Usually folks have problems with textures going away after a pause, not showing up. :)
I think loading the textures on a different thread isn't working. Generally, Libgdx doesn't support OpenGL operations on other threads. You may be able to do the part that opens the font file and reads it in on a different thread, but I think you need to make sure the part that creates the font textures happpens on the main thread.
I suspect that the new code to reload generated fonts after a pause (see https://github.com/libgdx/libgdx/pull/896) is fixing your fonts for you after the pause.
Upvotes: 2