Games Different sized text output

Space

Member
Licensed User
I want to write a game and I noticed that the text output is different in size on different devices.

Drawing with:
B4X:
    cnv.DrawText( "Test", 50,200, xui.CreateFont2(Main.ninjaFont,32), 0xffffffff, "LEFT" )
    
    Dim rect As B4XRect
    rect.Initialize( 45,110,380,210 )
    cnv.DrawRect( rect, 0xffffffff, False, 1 )

In my example, the rectangle and the text should always be equal to each other.

But they are not...

Only Windows and my Android Tablet are the same.

What am I doing wrong?
Can someone please nudge me in the right direction?
 

Attachments

  • android handy.png
    30.3 KB · Views: 153
  • android tablet.png
    12.6 KB · Views: 165
  • windows.png
    8.5 KB · Views: 155

Space

Member
Licensed User
Thank you Erel. This works.

But is there a way to determine the output of the text in pixels? (Pos and font size)

I'm working on a small game and it would be difficult to switch to dip.
 

Space

Member
Licensed User
I've now written a function that returns a font with a fixed pixel height.

B4X:
Sub GetRealHeightFont( fontHeightInPixel As Float ) As B4XFont

    Dim fontHeight As Float = 50
    Dim realHeight As Float = 1000
    Do While Abs(realHeight - fontHeightInPixel) > 1
        Dim font As B4XFont = xui.CreateDefaultFont( fontHeight )
        Dim r As B4XRect = cnv.MeasureText( "Wq", font )
        Dim realHeight As Float = r.Bottom-r.Top
        fontHeight = fontHeight / ( realHeight / fontHeightInPixel )
    Loop

    Return font
End Sub

I think it's a bit laborious, but it works on my Android test devices.

Thanks for the help
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…