libgdx BitmapFont display text on small scale - libgdx

I have OrthographicCamera with viewport 16x9.
Lets say for example that devices real resolution is 1024x576
this.cam = new OrthographicCamera(16, 9);
I create BitmapFont like this:
FreetypeFontLoader.FreeTypeFontLoaderParameter params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
params.fontFileName = "font.ttf";
params.fontParameters.size = 19;
Font size in pixels iz 19. That is calculated like this: screenHeight(576)/cameraViewportHeight(9)*0.3
0.3 is how big font should be in camera units
Since font is rendered on camera mentioned above, we have to scale font to our camera size:
font.getData().setScale(0.015f);// 0.3/19
I dont know if my math is ok, but this works fine on libgdx version 1.7.1. If i update to any higher version (currently 1.9.2, but i also tried 1.7.2), font just dont display
What am i doing wrong?

Related

Is it possible to change the size of Heiro generated font in the code?-LibGdx

I am using a font generated by Heiro tool.
game.scoreFont = new BitmapFont(Gdx.files.internal("fonts/score.fnt"), false);
I applied it for a label;
private void drawScore() {
Label.LabelStyle scoreStyle = new Label.LabelStyle();// style
scoreStyle.font = game.scoreFont;
scoreLabel2 = new Label(scoreController.getScoreString(), scoreStyle);// label
scoreLabel2.setPosition(scoreBox.getX()+scoreBox.getWidth()/2.7f,scoreBox.getY()+scoreBox.getHeight()/2);
scoreLabel2.setAlignment( Align.center);
}
While generating font,I gave a size of 32.
But same font I want to use for a label somewhere else in the game ,with a little bit bigger size.
Is it possible to increase the size then?or Should I generate a new font of required size with Heiro tool again?
You can scale your BitmapFont by
bitmapFont.getData().setScale(2);
and then use this font in LabelStyle, however you can also scale in this way
labelStyle.font.getData().setScale(2);
It works on reference of BitmapFontso it scaled everywhere, where you're using that BitmapFont in your game.
So you can create two object of BitmapFont with same file and scale one font and use where you want bigger font.
I'll recommend you to create two font with different size using your tools. Scaling is never a good solution.

How to get the natural fontsize of bitmapfont?

If I have a bitmapfont such as;
fonttexture = new Texture(Gdx.files.internal("data/fonttest.png"), true);
font= new BitmapFont(Gdx.files.internal("data/fonttest.fnt"),
new TextureRegion(fonttexture ), true);
Is it possible to retrieve or deduce the natural fontsize used on the associated png texture file?
The *.fnt file itself specifies this on the first line;
info face="Calibri Light" size=32 bold=0 italic=0 charset=""
Its that 32 I am after in code.
I couldn't see a method on BitmapFonts class to directly get it, but there was various other height related attributes possible to get.
I tried recreating the size from those but didn't have much luck.
float baseToAssent = font.getCapHeight()+font.getAscent();
float baseToDecent = -font.getDescent();Decent);
float fontHeight = baseToAssent+baseToDecent; //too small, like 11 not 32
Not being a fonttographer? fontophile?...umm...graphic designer, I am probably putting the pieces together wrong, or maybe just lacking one.
In either case, any way to work out the font size used in the fonttest.png would be helpful.
Thanks
Usecase;
I am manually using that texture with a distance field shader. Knowing the textures natural font size is useful in order to accurately scale the text and lineheight to any requested font size without ever resizing a image and thus losing quality.
Not sure how to get the font size specific, however you can easily get the String width and height of a font using a GlyphLayout, like this:
BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonttest.fnt", false);
GlyphLayout glyph = new GlyphLayout(font, "TextMessage");
float width = glyph.width;
float height = glyph.height;
Read this topic if you want to learn more about GlyphLayout and BitmapFont.
Experimented quite a bit with a 32px font, like yours. The String "W" seemed to be 29px wide, which was the widest I could find, so it's definately not precise if what you want to archive is the size exactly.
You could do a little hacky method, reading the .fnt file as a text file and looking for the Integer after "size=", pretty much like a properties file work, it will be less efficient but will return exact value.

Preffered viewPort to dealing with textButton libgdx

i work with a new game , i have create 2 camera one camera for real world and another (GUI) camera for a static resolution (800 x 480)
private void init () {
camera = new OrthographicCamera(16,9);
camera.position.set(0, 0, 0);
camera.update();
cameraGUI = new OrthographicCamera(800,480);
cameraGUI.position.set(0, 0, 0);
cameraGUI.update();
And i want to use a textButton in my game , so i've create a new stage using a new fitViewPort
fitViewPort=new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
gameController.stage=new Stage(fitViewPort);
then i create my textButtons using my bitmap font and i try it on the Desktop , and it's great
desktop
but when i try another device , the position is not the same
Tablet
then i think for using the cameraGui Width and height for my view port , the font is very bad stretched
any help please :(
Don't create a special camera for GUI, because viewport already has a camera.
Make your viewport width and height fixed, in your case:
fitViewPort = new FitViewport(800, 480);
gameController.stage = new Stage(fitViewPort);
And then just add actors to stage and draw it just by calling method
gameController.stage.draw();
Note: Remember to add gameController.stage.getViewport().update(width, height); in your resize method.
Edit: Using small fixed viewport (800x480 is small) will cause scaling, that is why the font is stretched. For example if you run a 800x480 layout on 1920x1080 display it will be scaled by 225%.
What you can do is generate a font with big size and the set scale to its data fe. bitmapFont20.getData().setScale(0.5F); (in this case font is generated by FreeTypeFontGenerator with size 40). Using this trick you will be able to get a great results. The proof is below. It is viewport of 640x360F * (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth() scaled to 1920x1080. Font is generated with size 72 and then data scale is set to 0.5F
Here is the same text, but without modified data scale.

text in ImageTextButton bigger on higher resolution devices

I am having some trouble specifying the size of text in ImageTextButton. I didn't found any method or property to define text size. I used the code below,but got very small text. I expect text to be bigger on higher resolution devices, since all my code is relative to the dimensions of screen. Is bitmap font the correct approach? How does one code text in ImageTextButton to be bigger on bigger devices?
font = new BitmapFont(Gdx.files.internal("font.fnt"));
ImageTextButton.ImageTextButtonStyle bts = new ImageTextButton.ImageTextButtonStyle();
bts.up = ninePatchDrawable;
bts.font=font;
bts.fontColor= Color.WHITE;
checkButton=new ImageTextButton("Text",bts);
checkButton.setBounds(Gdx.graphics.getWidth() * 0.2f, Gdx.graphics.getHeight() * 0.01f,Gdx.graphics.getWidth() * 0.24f,Gdx.graphics.getHeight()*0.08f);
stage.addActor(checkButton);
Turns out I had to choose different keywords for google search. This solves my problem How to draw smooth text in libgdx?

Unable to play Sprite Images with aspect ratio in Windows Phone 8

I am trying to play sprite images on windows phone 8. But its not playing correctly, if the single sprite image is larger than screen dimensions. I try to reduce the image with aspect ratio by using rectangle and ImageBrush. Still I am facing the same problem. Please find the below scenario.
Scenario :
Sprite Width/Height : 22100 / 516
Single Sprite Width/Height : 850/516
Mobile Dimensions : 800/480
Could anyone can help me out.
So, if I understood your question, all you need to do is:
Create a BitMapImage, don't use ImageBrush or Rectangle..
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(imagePathOrUrl, UriKind.RelativeOrAbsolute);
I suggest you to add also this instruction to prevent memory issues:
bitmapImage.DecodePixelWidth = imageWidth;
Add the BitMapImage to an Image control and set the **Stretch** property for the aspect ratio:
Image img = new Image();
img.Source=bitmapImage;
img.Stretch = Stretch.Uniform;
Stretch property has also other values, so be sure to do some test with the other values to see what value adapts better to your needs.
Let me know if it works!