SDL_ttf 3.0 Documentation
repository·main·Indexed 20 days ago
https://github.com/libsdl-org/sdl_ttfSDL_ttf 3.0 is a library that allows SDL applications to render text using TrueType fonts by wrapping the FreeType and Harfbuzz libraries. This documentation provides integration guides for CMake, Android Studio, Emscripten, Visual Studio, and Xcode, as well as migration details from version 2.0, including changes to error handling, UTF-8 standardization, and API renames.
What's inside SDL_ttf
- SDL_ttf 3.0 is a library that provides a wrapper around FreeType and Harfbuzz, enabling developers to render TrueType fonts within SDL applications. It facilitates text rendering by leveraging these specialized font and shaping engines.
Migrate text encoding and rendering to UTF-8
mainSDL_ttf 3.0 has standardized on UTF-8.
- Encoding: Functions that previously offered multiple variants (Latin-1, UTF-8, and UCS2) now exclusively accept UTF-8 text.
- Substrings: Rendering functions now include an optional length parameter, allowing you to render specific substrings of a larger text buffer.
- Color Transparency: For background colors, an alpha value of
0is now treated as transparent.
Understand the SDL versioning policy
mainSDL uses an "odd/even" versioning policy to distinguish between stable production releases and development prereleases. This helps developers decide which version to target based on their stability requirements.
Stable Releases
A version is considered stable and suitable for production if both the minor version (second part) and the patch version (third part) are divisible by 2 (e.g.,
3.2.6,3.4.0).- Patch releases (e.g.,
3.2.x): Indicate bugfixes. They are backwards-compatible (code built against3.2.0works with3.2.8), but not necessarily forwards-compatible. - Minor releases (e.g.,
3.4.x): Indicate significant changes or new functionality. They are backwards-compatible (code built against3.2.xworks with3.4.x), but not necessarily forwards-compatible.
Development Prereleases
A version is a development prerelease if the minor version or patch version is not divisible by 2 (e.g.,
3.2.9,3.3.x).- Usage Warning: Prereleases are not suitable for stable software distributions and should be used with caution.
- Compatibility: Prereleases are backwards-compatible with older stable branches (e.g.,
3.2.xcode works with3.3.x), but they are not guaranteed to be backwards-compatible with each other (APIs may change between3.3.0and3.3.1). - Recommendation: Only use a prerelease if you can promptly upgrade to the subsequent stable release (e.g., upgrading from
3.3.xto3.4.0).
- Patch releases (e.g.,
Build SDL_ttf projects for Emscripten
mainOnce your
CMakeLists.txtis configured, useemcmakeandemmaketo perform the build process. This ensures the Emscripten toolchain is correctly applied to the CMake generation and the subsequent make process.- Generate the build files using
emcmake cmake. - Compile the project using
emmake make. - Serve the resulting
builddirectory using a webserver to view the generated.htmloutput in a browser.
emcmake cmake -S . -B build cd build emmake make- Generate the build files using
Build and use SDL_ttf with CMake
mainSDL_ttf supports various development environments. For CMake-based workflows, you can build the library and then include the generated headers and library files in your own project.
To build the included example programs alongside the library, enable the samples option during the CMake configuration step using
-DSDLTTF_SAMPLES=ON.cmake -DSDLTTF_SAMPLES=ON ..Set up SDL_ttf with Visual Studio using subprojects
mainThe recommended way to use SDL_ttf in Visual Studio is to include both SDL and SDL_ttf as subprojects within your solution.
Prerequisites
Ensure you have downloaded the external dependencies by running the following command in the
externaldirectory:./Get-GitModules.ps1Project Configuration Steps
- Create Project: Create a new Visual Studio project using the C++ Empty Project template and add your source file (e.g.,
hello.c) to the Source Files. - Add SDL Subproject: Right-click the solution, select Add > Existing Project, and navigate to the
SDL VisualC/SDLdirectory to addSDL.vcxproj. - Add SDL_ttf Subproject: Right-click the solution, select Add > Existing Project, and navigate to the
SDL_ttf VisualCdirectory to addSDL_ttf.vcxproj. - Configure SDL_ttf References: Select the
SDL_ttfproject, go to Project > Add Reference, and selectSDL3. - Configure SDL_ttf Include Paths:
- Select the
SDL_ttfproject. - Go to Project > Properties.
- Set the Configuration and Platform filters to All Configurations and All Platforms.
- Navigate to VC++ Directories > Include Directories and update the default SDL path to point to your SDL include directories.
- Select the
- Configure Main Project References: Select your main project, go to Project > Add Reference, and select both
SDL3andSDL3_ttf. - Configure Main Project Include Paths:
- Select your main project.
- Go to Project > Properties.
- Set the Configuration and Platform filters to All Configurations and All Platforms.
- Navigate to VC++ Directories > Include Directories and add both the SDL and SDL_ttf include directories.
- Build: Build and run your project.
- Create Project: Create a new Visual Studio project using the C++ Empty Project template and add your source file (e.g.,
Use SDL_ttf as a CMake subproject with vendored libraries
mainThe simplest way to integrate SDL_ttf into your project is to include both SDL and SDL_ttf as subprojects using
add_subdirectory. This approach requires settingSDLTTF_VENDOREDtoONand ensuring the source code for both SDL and SDL_ttf is available in your project'svendored/directory.To ensure that dynamic libraries are placed in the same directory as your executable for easy running, set
CMAKE_RUNTIME_OUTPUT_DIRECTORYandCMAKE_LIBRARY_OUTPUT_DIRECTORYto use the configuration-specific build directory.cmake_minimum_required(VERSION 3.16) project(hello) # Set output directories so dynamic libraries are in the build folder set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>") # Enable vendored mode set(SDLTTF_VENDORED ON) # Add SDL and SDL_ttf as subdirectories add_subdirectory(vendored/SDL EXCLUDE_FROM_ALL) add_subdirectory(vendored/SDL_ttf EXCLUDE_FROM_ALL) # Define your executable add_executable(hello WIN32 hello.c) # Link against the SDL_ttf and SDL3 targets target_link_libraries(hello PRIVATE SDL3_ttf::SDL3_ttf SDL3::SDL3)Use SDL_ttf in a CMake project
mainTo use this package in a CMake-based project, you must copy both the
SDL3_ttf.xcframeworkand thesharedirectory to your local frameworks directory:~/Library/Frameworks.cp -R SDL3_ttf.xcframework share ~/Library/Frameworks/Use SDL_ttf in an Xcode project
mainTo integrate the pre-built SDL_ttf framework into an Xcode project, drag theSDL3_ttf.frameworkfile directly into your project workspace.Build and use SDL_ttf in Visual Studio, Xcode, or Android Studio
mainSDL_ttf provides specific setup instructions and project files for several IDEs:
- Visual Studio (Windows): Use the separate projects located in the
VisualCdirectory. - Xcode (Apple platforms): Use the separate projects located in the
Xcodedirectory. - Android Studio: Follow the specific guide for Android development.
- Emscripten: Use the guide for web-based builds.
- Visual Studio (Windows): Use the separate projects located in the
Use the SDL_ttf Android Studio sample project
mainFor a complete, working example of integrating SDL_ttf into an Android project using Android Studio, refer to the sample repository provided by Ravbug. This sample demonstrates the necessary setup and integration steps for Android environments.
https://github.com/Ravbug/sdl3-sampleBuild and run an SDL_ttf project with CMake
mainAfter configuring your
CMakeLists.txtas described in the subproject guide, follow these steps to build and run your application:Download dependencies: Run the provided scripts to fetch the required source code:
- Linux/macOS:
./external/download.sh - Windows:
./external/Get-GitModules.ps1
- Linux/macOS:
Configure and Build:
cmake -S . -B build cmake --build buildRun the executable:
- Windows: The executable is located in the
Debugsubdirectory of your build folder:cd build/Debug ./hello - Other platforms: The executable is located directly in the
buildfolder:cd build ./hello
- Windows: The executable is located in the
# Configure cmake -S . -B build # Build cmake --build build # Run (Linux/macOS example) cd build ./hello