Skip to content

Tutorial 06 Writing text

Oliver Simmons edited this page Feb 5, 2022 · 1 revision

Tutorial 06 - Writing text

Writing text to the screen is easy with SDL_ttf.

See Tutorial 04 - Drawing an image to initialize the window and renderer.

Download full example: 06-text.tgz

Load the SDL_ttf library

As always, load the library separately.

local ttf = require("SDL.ttf")

Initialize the library

We now want to load the library.

assert(ttf.init())

Load a font

Now that we loaded the SDL_ttf library, we must open a font before writing text. A path to a .ttf file must be specified.

local font = assert(ttf.open("DejaVuSans.ttf", 10))

Render the text

The functions Font:renderUtf8 and friends creates surfaces which must be converted later.

local s = assert(font:renderUtf8("Lua-SDL2", "solid", 0xFFFFFF))

Convert and draw

Convert the surface to texture & display.

local text = rdr:createTextureFromSurface(s)

-- This would normally be called in your main loop
rdr:clear()
rdr:copy(text)
rdr:present()

Expected output

Clone this wiki locally