edadma / sdl2_ttf   0.0.1

ISC License GitHub
Scala versions: 3.x
Scala Native versions: 0.5

sdl2_ttf

Maven Central Last Commit GitHub Scala Version Scala Native Version

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 @extern declarations (the only place Scala Native FFI types appear).
  • io.github.edadma.sdl2_ttf — the pure-Scala layer you import. Fonts are an AnyVal Font wrapper; you render with the familiar sdl2.Color and get back an sdl2.Surface (or, via Font.texture, a ready-to-draw sdl2.Texture).

Requirements

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/Ubuntu

Usage

libraryDependencies += "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.

Coverage

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).