libgdx 1.5.5, help font loading-.ttf - java

Hi I'm making a asteroids clone using java and libGDX and have had some trouble trying to load in a ttf font and simply displaying it and i have no idea what I'm doing wrong I've looked at many sites but they all have outdated information (I'm using the latest version of freetype).
Here is code:
SpriteBatch sb;
BitmapFont font;
sb = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"), true);
sb.setColor(1,1,1,1);
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();
I've created a folder in my assets folder called fonts fyi.
P.S. players score is just 0
EDIT: the sb.setColor and everything preceding is in its own render method but i just put it here for simplicity purposes. The purpose of this post is that I'm not sure HOW to use a ttf font type and display it in the latest version of libGDX.

Ok i figured it out. Finally! I needed to use a freeTypeFontGenerator like this:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12;
BitmapFont font = generator.generateFont(parameter);
generator.dispose();
then just this in the draw/render method!:
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();

Related

Freetypes' parameter.color doesn't seem to be working

In the game I'm making I'm rendering text on screen and I'd like to have white text with a black border but it doesn't seem to let me change the foreground and border colours to what I want.
This is how I create my fonts using a class I made called Font:
public BitmapFont font;
public Font(String fontName, int size, boolean border)
{
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.local("font/"+fontName)); // Load in .ttf
// Custom font settings
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = size;
parameter.spaceX = 1;
if (border)
{
// Must fix this!!!
parameter.color = Color.WHITE;
parameter.borderColor = Color.BLACK;
parameter.borderWidth = 1;
}
this.font = generator.generateFont(parameter);
generator.dispose();
}
Here is how I've initialised the font in my HUD class:
arial24 = new Font("arial.ttf", 24, true);
arial24.font.setColor(0, 0, 0, 1);
And this is how I've rendered it:
arial24.font.draw(Game.getBatch(), message, Game.getCamera().position.x - 80, Game.getCamera().position.y + 75);
I expect it to output something like this
But instead it comes out like this
Everything seems right according to the github pages and I've been searching through forums for hours and I just can't find an answer.
arial24.font.setColor(0, 0, 0, 1);
Why are you blending to black if you want white? Just delete this line or change it to
arial24.font.setColor(1f, 1f, 1f, 1f);

Save and load the font generated using FreeTypeFontGenerator

In my Android App, I am using FreeTypeFontGenerator for generating a font of different size using the TTF file. This takes nearly 3 sec to start my App. I cam e to know that the font can be generated when the app is opened very first time and saved for future openings / use.
Questions
How to save the font ?
How to access / load the saved font in future openings / use
Does this need the permission of external storage from Marshmallow version ?
Below is the sample of my code relate to FreeTypeFontGenerator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/boldarial.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 47;
fontRatingRec = generator.generateFont(parameter);
fontRatingRec.setColor(70f / 255, 104f / 255, 163f / 255, 1f);
fontRatingRec.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
parameter.size = 54;
fontRatingButtonRec = generator.generateFont(parameter);
fontRatingButtonRec.setColor(45f / 255, 64f / 255, 98f / 255, 1f);
fontRatingButtonRec.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
generator.dispose();

Libgdx Skin not Updating when Changing Font Programmatically

First, I loaded the skin as a json file. In the file, I put two BitmapFont defintions ("font" and "big-font") and left them practically blank (I did load a dummy ".fnt" file to prevent the parser from complaining). I then generated two FreeType Fonts using the following code:
//start font generator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
//default font (the sizes are arbitrary, since it did not work...)
parameters.size = 10;
BitmapFont defaultFont = generator.generateFont(parameters);
//big font
parameters.size = 100;
BitmapFont bigFont = generator.generateFont(parameters);
And set them as the skin fonts like so:
skin.add("font", defaultFont, BitmapFont.class);
skin.add("big-font", bigFont, BitmapFont.class);
The problem is that the gui (particularly the TextButton, which uses the "font" font) does not seem to care about what I did programmatically and keeps using the dummy font that I've set in the json file.
What am I doing wrong? Is there a better way to integrate ttf fonts and json skin parsing?
Thanks in advance for any help.
Follow these steps :
First, create your font from a TTF file
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 10;
BitmapFont defaultFont = generator.generateFont(parameter);
generator.dispose(); // don't forget to dispose to avoid memory leaks!
Create a blank skin using default constructor and add your font to the skin.
Skin skin = new Skin();
skin.add("font",defaultFont);
Add your atlas file to your skin.
skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
Load your Json file to your skin.
skin.load(Gdx.files.internal("mySkin.json"));
then, in your JSON file you can reference “font”
{
font: font
}

libGDX freetypefont artifacts

When creating a BitmapFont in libGDX from a .ttf file with FreeTypeFontGenerator the resulting font has serious artifacts around it:
Those artifacts only appear when using mipmapping, so does it have something to do with transparent pixels? I have tried adding padding to the resulting font, but it does not work. I also tried using linear filtering but the resulting font looks pixelated.
Code I am using to generate the fonts:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 300;
parameter.color = Color.WHITE;
parameter.characters = "0123456789";
parameter.kerning = true;
parameter.magFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.minFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.genMipMaps = true;
font = generator.generateFont(parameter);

Libgdx nine patch text button height issue

I am using a TextButton to create a dialog bubble effect in my game.
I have created the nine patch file as on the picture
everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.
You can see that a multilines text extends the TextButton height but not correctly.
Here is the code
Load the bitmap font
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;
Initialize the TextButtonStyle
NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;
actual rendering
dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);
Does someone see what the problem is?
I finally found the problem.
I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.
Bad font
Good font
You can see that the base line and capheight are not correctly setted on the bad font.
I hope this will help other people facing the same problem.

Categories