Blitz3D+ Command Reference

FreeFont font

Parameters

font - handle of the font to delete

Description

Deletes a font and releases the resources it was using.

Fonts are cheap compared with images, but a game that loads a fresh set of them per screen will leak steadily if it never frees the old ones. Free the ones belonging to a screen or level you have left behind.

If the font you free happens to be the current one, the built-in default font is selected automatically, so text keeps working instead of crashing on a dangling handle. Any other variable still holding the freed handle is stale, though, and passing it to SetFont is an error - set your variable to 0 after freeing so you can tell.

This works on handles from LoadFontSheet as well as LoadFont. You do not need to free anything before your program exits; that is done for you.

See also: LoadFont, SetFont, LoadFontSheet, FreeImage.

Example

; FreeFont Example
; ----------------

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

; A big title font, and a small font that is never freed
big_font=LoadFont("Arial",36,True)
info_font=LoadFont("Arial",14)

While Not KeyDown(1)

    ; Space frees the title font and releases its memory
    If KeyHit(57) And big_font<>0 Then
        SetFont info_font
        ; Never draw with a freed handle - select another font first
        FreeFont big_font
        big_font=0
    End If

    ; R loads the font again, giving a fresh handle
    If KeyHit(19) And big_font=0 Then
        big_font=LoadFont("Arial",36,True)
    End If

    Cls

    If big_font<>0 Then
        SetFont big_font
        Color 255,220,80
        Text 320,220,"36pt TITLE FONT LOADED",True,True
    Else
        SetFont info_font
        Color 160,160,160
        Text 320,220,"font freed - memory released",True,True
    End If

    SetFont info_font
    Color 255,255,255
    Text 0,0,"Space: FreeFont the title font   R: reload it   Esc: exit"
    Text 0,20,"big_font handle: "+big_font

    Flip

Wend

End

Index