0

My SDL program contains:

TTF_Init();
TTF_Font *font = TTF_OpenFont("segoeui.ttf",13);
SDL_Color textColor = {0,0,0};
SDL_Color backgroundColor = {34,177,76};
SDL_Surface *myText = TTF_RenderText_Shaded(font,"Some text",textColor,backgroundColor);

When I run the program from the Build and run button in Code::Blocks, there isn't any problem but when I run the program from the folder in Windows Explorer, the window opens and closes directly, and after the window closes, the process isn't running any more and the files stderr.txt and stdout.txt are still there. I've made some tests and found out that it's the line SDL_Surface *myText = TTF_RenderText_Shaded(font,"Some text",textColor,backgroundColor); that seems to end the process just like that like if the End Process button would have been pressed in the task manager.

Why does it do that? How can I fix it?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 2
    Try to put the absolute path of the font – Thomas Ayoub Jul 07 '16 at 10:53
  • @ThomasAyoub Thanks, that worked. The problem with that is that the absolute path of the font depends on the OS and I would like my program to be compatible with any OS. In windows, it's `C:\Windows\Fonts\segoeui.ttf`, and it would surprise me if it were the same in Linux or Mac. The other problem is that maybe the user doesn't have the font on his computer. What I did to solve these problems was to include the font in the project and use the code that I posted, but as I said in the question, that doesn't work. – Donald Duck Jul 07 '16 at 11:12
  • Should be ok with relative path. Probably your current working directory isn't what you think it is, check with e.g. [GetCurrentDirectory](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx) for windows or `getcwd` for *nix. – keltar Jul 07 '16 at 19:35

1 Answers1

1

You should set your font with absolute path and not a relative one. If you plan to do crossplateform deployment, you may want to include something like that:

TTF_Font *font;

#ifdef _WIN32
    font = TTF_OpenFont("WinPath",13); // The windows path
#elif  linux
    font = TTF_OpenFont("LinuxPaht",13); // The linux path
#elif MacOS
    font = TTF_OpenFont("Mac path",13); // The mac path
#endif

if(font == null)
    // Throw an error, return or whatever.

You can get exact directives at Detect Windows or Linux in C, C++

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142