java - LibGDX text not centered in smaller viewport -


i'm working on libgdx game uses smaller viewport.

public static float box_scale = 10; public static final float virtual_width = (int) (320 / box_scale); public static final float virtual_height = (int) (480 / box_scale);  float viewportheight = myconstants.screen.virtual_height; float viewportwidth = myconstants.screen.virtual_height * gdx.graphics.getwidth() / gdx.graphics.getheight(); 


example viewport can have size (32, 48). use scene2d rendering. reason whenever create textbutton text never centered. bitmapfont used button.

freetypefontparameter fontparam = new freetypefontparameter(); fontparam.size = 14;          freetypefontgenerator generator2 = new freetypefontgenerator(gdx.files.internal("data/font.ttf")); labelfont = generator2.generatefont(fontparam); labelfont.setscale(1f / box_scale);  labelfont.setcolor(color.black); 

enter image description here


if set box_scale value 1 textbutton acts normal need simulating box2d world. guess create separate labels each button , position them manually can't figure out why happening. interested if there cleaner solution.

by default, font positions rounded off nearest world game unit. based on assumption font render pixel perfect. in case, don't want pixel perfect font, call:

labelfont.setuseintegerpositions(false); 

also, in fontparam should set use mipmaps, , set minfilter mipmaplinearnearest , magfilter linear. that'll make better, since default filtering set nearest/nearest looks bad if aren't rendering pixel perfect.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -