rpora
11/4/2017 - 8:38 AM

SDL Font

#include "SDL_ttf.h"

if (TTF_Init() != 0) {
  // log error
  // Cleanuo
  SDL_Quit();
  return 1;
}
  
// Load the font  
int fontSize = 24;
std::string fontFile = "../resources/sample.ttf";
  
TTF_Font *mainFont = TTF_OpenFont(fontFile.c_str(), fontSize);  
  
std::string msg = "Hello";


// 1. Create a surface
SDL_Surface *surface = TTF_RenderText_Blended(font, msg.c_str(), {255, 255, 255, 255});

// 2. Render the surface to texture (assuming SDL_Renderer *renderer;)
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);

// 3. Surface not needed anymore
SDL_FreeSurface(surface);

// 4. Query generated texture dimensions (optional)
SDL_Rect renderTarget;
SDL_QueryTexture(texture, nullptr, nullptr, &renderTarget.w, &renderTarget.h);

// 5. Render the texture
SDL_RenderCopy(renderer, texture, nullptr, &renderTarget);