Textheight [SOLVED]

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
I must be missing something here and going crazy of understanding what. So I try to write the font height instead of a number and the screen goes black for me?

code_language.cerberus:
Strict
Import mojo2

Function Main:Int()
    New CGame()
    Return 0
End

Class CGame Extends App

    Field canvas:Canvas
    Field str:String
    Field myFont:Font
 
    Method OnCreate:Int()
        canvas = New Canvas
        SetSwapInterval 1
        SetUpdateRate 0
         myFont = Font.Load( "mojo_font2.png", 32, 96, True )
       
        str="This is a test for half alpha [[0x7FFF0000]]RED[[0xFFFFFFFF]]. This is a test for [[0xFF00FF00]]GREEN[[0xFFFFFFFF]]. This is a test for [[0xFF0000FF]]BLUE[[0xFFFFFFFF]].~n"
        str+="This is a test for [[0xFF00FFFF]]CYAN[[0xFFFFFFFF]]. This is a test for [[0xFFFF00FF]]MAGENTA[[0xFFFFFFFF]]. This is a test for [[0xFFFFFF00]]YELLOW[[0xFFFFFFFF]].~n ~n" ' Bug ~n~n will not work, so you need to have a space between them.
        str+="This is a test for wrapping. This is a test for wrapping. This is a test for wrapping. This is a test for wrapping. This is a test for wrapping. This is a test for wrapping. This is a test for wrapping. This is a test for wrapping."
     
        Return 0
    End
     
    Method OnUpdate:Int()
        Return 0
    End
     
    Method OnRender:Int()
        canvas.Clear
        canvas.SetFont myFont
        DrawColourText 0,0, str
     
        canvas.DrawText String(4),150,150
       ' canvas.DrawText String(myFont.TextHeight("")),150,150 '  Try uncomment this. Why does this give a black screen?
     
        canvas.Flush
        Return 0
    End
 
    Method DrawColourText:Void( x:Int=0, y:Int=0, str:String="", width:Int=9, height:Int=14, colour:Int=$FFFFFFFF)
        Local xx:Int=x, yy:Int=y, index:Int, p1:Int, tmp:String
        While index < str.Length()
            If str[ index .. index+4 ]<>"[[0x"
            p1=str.Find( " ", index )
            If p1=-1 p1=str.Length()
            tmp=ProcessString(str[ index .. p1 ])
            If xx+(tmp.Length()*width)>DeviceWidth() Or str[index]="~n"[0]
            xx=x
            yy+=height
            If str[index]="~n"[0] index+=1
            Endif
            canvas.DrawText String.FromChar(str[index]),xx,yy
            xx += width
            index += 1
            Else
            p1=str.Find("]]", index+4)
            If p1=-1 Error("Error: Hex without enclosing brackets. Starting from "+index)
            If (p1-(index+4))<>8 Then Error("Error: Value must be 32 bit hexadecimal (max 8 hex digits) starting with 0x or enclosing brackets missing. Starting at from "+index)
            colour=Hex2Int( str[ (index+4) .. p1 ] )
            canvas.SetColor ((colour Shr 16)&$FF)/255 ,((colour Shr 8)&$FF)/255,(colour&$FF)/255
            canvas.SetAlpha Normalize((colour Shr 24)&$FF)
            index+=(p1-(index+4))+6
            Endif
        Wend
    End

    Method ProcessString:String(str:String)
        Local checks:String[]=[ "[[0x", "~n" ], p1:Int, i:Int
        While i<checks.Length()
            p1=str.Find(checks[i])
            If p1=-1 Then p1=str.Length()
            str=str[ .. p1 ]
            i += 1
        Wend
        Return str
    End
 
    Method CheckRectangle:Bool(x : Int,y : Int, x1:Int, y1:Int, w:Int,h:Int)
        Return (x >= x1) And (y >= y1) And (x <= x1 + w) And (y <= y1 + h)
    End Method
     
    Method Normalize:Float( val:Int )
        Return Float(val)/255.0
    End

    Method Hex2Int:Int( s:String )
        Local hexdigit:Int, i:Int, n:Int
        s=s.ToUpper()
        While i<s.Length()
            If s[i]>="0"[0] And s[i]<="9"[0]
                hexdigit=s[i]-"0"[0]
            Elseif s[i]>="A"[0] And s[i]<="F"[0]
                hexdigit=s[i]-"A"[0]+10
            Else
                Error("Error: Invalid hex digit")
            End
            n=(n Shl 4)+hexdigit
            i+=1
        Wend
        Return n
    End
End
 
Last edited:

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
Whops, I wrote mojo_font2.png, I thought that there was 2 standard fonts.

mojo_font.png works perfectly ;)
 

Jimmy

New member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
569
I love the the fact that giving font.TextHeight en empty string font.TextHeight("") gives the
actual maximum general font height, while giving it any other kind of string outputs the actual height of that string.

Very elegant
 
Last edited:
Top Bottom