Blitz3D+ Command Reference

SetFont font

Parameters

font - handle of a font returned by LoadFont or LoadFontSheet

Description

Makes a loaded font the one that text commands use from now on.

Loading a font does nothing on its own; this is the switch that puts it into service. After the call, Text, Print and Write all draw with it, and the measuring commands FontWidth, FontHeight, StringWidth and StringHeight all report against it.

Switching is cheap, so treating it as a mode is the normal pattern: SetFont big before the title, SetFont small before the HUD, and back again. Just remember it is a global setting - whatever you selected last is still in force when you get to some other part of your code, which is why stray text often turns up in the wrong size.

The layout numbers change with the font, so measure after you switch, not before.

Your program starts with a built-in default font, so text works before you have loaded anything. If you free the current font with FreeFont, the default is selected again automatically rather than leaving you with a dangling handle - but any other stale handle passed to SetFont is still an error.

See also: LoadFont, FreeFont, Text, FontHeight, StringWidth.

Example

; SetFont Example
; ---------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Two menu fonts plus a small one for the info overlay
menu_font=LoadFont("Arial",24)
pick_font=LoadFont("Arial",32,True,True)
info_font=LoadFont("Arial",14)

; A little game menu
Dim item$(3)
item$(0)="Start Game"
item$(1)="Options"
item$(2)="High Scores"
item$(3)="Quit"

choice=0

While Not KeyDown(1)

    ; Up/Down move the highlight
    If KeyHit(200) And choice>0 Then choice=choice-1
    If KeyHit(208) And choice<3 Then choice=choice+1

    Cls

    For i=0 To 3
        ; SetFont picks the font that all following Text commands use:
        ; the highlighted item gets the big bold italic font
        If i=choice Then
            SetFont pick_font
            Color 255,220,80
        Else
            SetFont menu_font
            Color 160,160,160
        End If
        Text 320,140+i*60,item$(i),True,True
    Next

    ; Switch to the small font for the overlay
    SetFont info_font
    Color 255,255,255
    Text 0,0,"Up/Down: move highlight   Esc: exit"
    Text 0,20,"SetFont swaps fonts freely between Text calls"

    Flip

Wend

End

Index