• 10
• 9
• 13
• 10
• 18

# SDL and text output

This topic is 3931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I'm is using SDL api's to show output on the screen, is it possible to render text on screen with methods other then
TTF_Font *TTF_OpenFont(const char *file, int ptsize)
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)

It seems with these methods I can only load fonts that are in the same directory as the .exe file, ie.:
TTF_Font *font = NULL;
font = TTF_OpenFont( "myfont.ttf", 28 ); //this works, "myfont.tff" is in same directory as the .exe

TTF_Font *font2 = NULL;
font2 = TTF_OpenFont( "C:\Windows\Fonts\myfont.ttf", 28 ); //same "myfont.tff", now in the default windows font folder, doesn't work...

I want to able to load fonts from other directories, any help?

##### Share on other sites
It's because the path is invalid. As it is now, "\W", "\f" and "\m" in "C:\Windows\Fonts\myfont.ttf" are interpreted as escape sequences. You should use either "C:\\Windows\\..." or "C:/Windows/...".

##### Share on other sites
I think you need to use 2 slashes, like c:\\windows\\fonts\\myfont.ttf. Or something along the lines of that.

##### Share on other sites
Oh I see, thanks!
Is it possible to do something like "%SYSTEMROOT%\\Fonts\\myfont.TTF" (I tried this, but didn't work) ?

##### Share on other sites
Quote:
 Original post by HawkAgentOh I see, thanks!Is it possible to do something like "%SYSTEMROOT%\\Fonts\\myfont.TTF" (I tried this, but didn't work) ?

No. You'll have to do the substitution by yourself. I'm pretty sure there is an API to get strings out of symbolic path names. Because explorer is the Windows shell, I'd bet it's in the Windows explorer API. Not being a Windows user though, I can't help you with that. Sorry.

##### Share on other sites
Yes, the Windows API has means of getting standard paths. I believe the exact function is ShGetFolderPath.

edit: apparently that function is deprecated, but look and I'm sure you'll find it.

##### Share on other sites
Do not double up the backslash for file paths. Use a forward slash instead. Even on Windows.

##### Share on other sites
Just as a suggestion, you might want to put #ifdef statements around your system-specific code if you're trying to make it work on multiple platforms.

Like this:
[source="c"]#ifdef WIN32  font=TTF_OpenFont("c:/windows/fonts/myfont.ttf",28);#else  font=TTF_OpenFont("myfont.ttf",28);#endif

When and if you start trying to port to Linux or MacOSX or others you'll be glad you did.