SDL C++ IDE can't open .ttf file

I included the ttf file into my project, I copied this ttf into the debug folder, and to the System32 folder, and I installed it on Windows. I used another C++ source found on internet, It doesn't work as well. BUT If I run the binary directly from the "Debug" ( not from the IDE ) folder it works correctly.

Im using CodeBlocks, SDL2.

Upvotes: 1

Views: 202

Answers (1)

user3684240
user3684240

Reputation: 1580

The paths you use in your code are relative to the directory from which your app is ran.

If your .ttf file is in the same directory as your application, you should use SDL_GetBasePath() to figure out where that is:

char* p = SDL_GetBasePath();
if(p == nullptr) { /* TODO: error */ }
std::string ttfPath = std::string(p) + "myfont.ttf";
SDL_free(p); // TODO: exception safety
// now, you can open the file 
TTF_Font* f = TTF_OpenFont(ttfPath.c_str(), 42);
// ...

Upvotes: 1

Related Questions