int fontsize = 24;
int t_width = 0;
int t_height = 0;
SDL_Color text_color = {0,0,0};
string fontpath = "my font path";
string text = "text I want to display";
TTF_Font* font = TTF_OpenFont(fontpath.c_str(), fontsize);
SDL_Texture* ftexture = NULL;
if (font == NULL) {
cerr << "Failed the load the font!\n";
cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
}
else {
SDL_Surface* text_surface = TTF_RenderText_Solid(font, text.c_str(), text_color);
if (text_surface == NULL) {
cerr << "Failed to render text surface!\n";
cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
}
else {
ftexture = SDL_CreateTextureFromSurface(renderer, text_surface);
if (ftexture == NULL) {
cerr << "Unable to create texture from rendered text!\n";
}
else {
t_width = text_surface->w;
t_height = text_surface->h;
SDL_FreeSurface(surface);
}
}
}