SDL and text output

Started by
6 comments, last by samuraicrow 16 years, 10 months ago
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?
Advertisement
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/...".
I think you need to use 2 slashes, like c:\\windows\\fonts\\myfont.ttf. Or something along the lines of that.
Oh I see, thanks!
Is it possible to do something like "%SYSTEMROOT%\\Fonts\\myfont.TTF" (I tried this, but didn't work) ?
Quote:Original post by HawkAgent
Oh 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.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Do not double up the backslash for file paths. Use a forward slash instead. Even on Windows.
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.

This topic is closed to new replies.

Advertisement