Scala Native bindings for SDL2_ttf —
TrueType/OpenType text rendering for SDL2 — built on the
sdl2 binding.
Two layers, same as sdl2:
io.github.edadma.sdl2_ttf.extern— raw@externdeclarations (the only place Scala Native FFI types appear).io.github.edadma.sdl2_ttf— the pure-Scala layer you import. Fonts are anAnyValFontwrapper; you render with the familiarsdl2.Colorand get back ansdl2.Surface(or, viaFont.texture, a ready-to-drawsdl2.Texture).
Scala Native–only. You need SDL2, SDL2_gfx (pulled in by sdl2), and SDL2_ttf:
brew install sdl2 sdl2_gfx sdl2_ttf # macOS
# apt-get install libsdl2-dev libsdl2-gfx-dev libsdl2-ttf-dev # Debian/UbuntulibraryDependencies += "io.github.edadma" %%% "sdl2_ttf" % "0.0.1"
// (pulls in io.github.edadma::sdl2 transitively)import io.github.edadma.sdl2.*
import io.github.edadma.sdl2_ttf.*
setMainReady()
init(INIT_VIDEO) // SDL
ttfInit() // SDL2_ttf
val window = createWindow("text", 800, 400)
val renderer = window.createRenderer()
val font = openFont("Foo.ttf", 32)
// Render straight to a texture and draw it at (x, y):
val label = font.texture(renderer, "Hello, world!", Color.fromRGB(0xffffff))
renderer.copy(label, 20, 20)
renderer.present()
// ...or keep the surface yourself:
val surface = font.renderBlended("Hi", Color.White)
val tex = renderer.createTextureFromSurface(surface)
surface.free()
label.destroy(); font.close(); ttfQuit(); quit()ttfInit/ttfQuit are named to avoid clashing with sdl2's init/quit when
both packages are imported. Run the bundled demo (bouncing text) with sbt run.
ttfInit/ttfQuit, openFont (+ index), font style/outline, metrics
(height/ascent/descent/lineSkip), size (measure without rendering), and
the four UTF-8 renderers — renderSolid, renderShaded, renderBlended,
renderBlendedWrapped — plus Font.texture for one-call surface→texture. The
render calls pass SDL_Color by value (a stack CStruct4).