Sign in to follow this  

SDL_ttf not opening font

Recommended Posts

I am deeply confused as to why this won't open the font:
	bool FONT = false;
	TTF_Font* Font = NULL;

	Font = TTF_OpenFont("arial.ttf", 14);
	if(Font == NULL)
		std::cout << TTF_GetError() << std::endl;
		FONT = true;
arial.ttf is in the same file as the .exe but it just isn't working. SDL_ttf is initialized. Also I'm not sure how to find out the error because there's no console :3, I remember something about stdout or something along those lines from ages ago when I dabbled with SDL

Share this post

Link to post
Share on other sites
SDL, at least the version I have, redirects cout to a text file name stdout.txt . It will probably be placed in the same folder as your executable, in the bin\debug path or whatever it is.

Or, you can do it yourself with std::ofstream.

#include <fstream>

std::ofstream errfile("my_error_file.txt");

if(Font == NULL){
errfile << "error loading font: " << TTF_GetError() << std::endl;

More info on std::ofstream.

Share this post

Link to post
Share on other sites
Ok, the problem was that arial.ttf was in the wrong folder, now don't get an error message.

The text won't render though;

void Text(std::string Text, TTF_Font *Font, SDL_Color Colour, SDL_Rect *Position) {
SDL_Surface *Initial;
SDL_Surface *Intermediary;
SDL_Rect Rect;
int W, H;
unsigned int Texture;

Initial = TTF_RenderText_Blended(Font, Text.c_str(), Colour);
W = NextPowerOfTwo(Initial->w);
H = NextPowerOfTwo(Initial->h);

Intermediary = SDL_CreateRGBSurface(0, W, H, 32, 0x00ff0000,
0x0000ff00, 0x000000ff, 0xff000000);

SDL_BlitSurface(Initial, 0, Intermediary, 0);

glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, W, H, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, Intermediary->pixels);


glBindTexture(GL_TEXTURE_2D, Texture);
glColor3f(Colour.r, Colour.g, Colour.b);

glTexCoord2f(0.0f, 1.0f); glVertex2f(Position->x, Position->y);
glTexCoord2f(1.0f, 1.0f); glVertex2f(Position->x + W, Position->y);
glTexCoord2f(1.0f, 0.0f); glVertex2f(Position->x + W, Position->y + H);
glTexCoord2f(0.0f, 0.0f); glVertex2f(Position->x, Position->y + H);


Position->w = Initial->w;
Position->h = Initial->h;

glDeleteTextures(1, &Texture);

/*Errors << "Text OGL Error: " << glGetError() << std::endl;
Errors << "Text SDL Error: " << SDL_GetError() << std::endl;*/


inline int Round(double x) { return (int)(x + 0.5); }

inline int NextPowerOfTwo(int x) {
double LogBase = log((double)x) / log(2.0f);
return Round(pow(2, ceil(LogBase)));


if(FONT) {
TextColour.r = 0;
TextColour.g = 0;
TextColour.b = 0;

TextPosition.x = View[0]/2;
TextPosition.y = View[1]/2;
Text("G'day, kind sir", Font, TextColour, &TextPosition);


I used this:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this