SDL_ttf

Started by
6 comments, last by Kwizatz 18 years, 3 months ago
Hey, I'm attempting to load a .ttf this->CurrentFont = TTF_OpenFont(FontName, Size); so that is my call, now the problem is it returns 0 if FontName == "arial.ttf" so I placed arial.ttf into my projects folder and it works, but how do I get it to get it from the Windows directory? I guess I just realised something stupid I did considering SDL is cross-platform and all, so does that mean I have to package the font I choose to use with it? Thanks for your help! Much appreciated. Jemburula
What we do in life... Echoes in eternity
Advertisement
Rather than use "arial.ttf" you will use "C:/windows/fonts/arial.ttf", which is the default path for Windows fonts [wink] Note that people that didn't install into the C:/ drive, this will not work, but that's a vast minority of people. Oh and btw, you *cannot* distribute Window's fonts with your program, it is a major copyright violation. You would have ot make your own font or find a free one to use that is royality free.
Yeah I realised the copyright issue otherwise this wouldn't of been an issue :) Thanks for the heads up though! [smile] Pity there is no cross platform standard about a fonts dump folder or something *grumble* [smile] Thanks again Drew!
What we do in life... Echoes in eternity
Well you could always make your own ttf font (it'll cost you though), or you could use a bitmap font.

Learn to make games with my SDL 2 Tutorials

Another question if you wouldn't mind. I've decided to store my TTF_Font*'s in a std::map<string, TTF_Font*> and I haven't had much experience with maps. So I'm ok with deleting one font
TTF_CloseFont(this->Fonts[FontName]);this->Fonts.erase(FontName);

but is there a way to iterate through say if my program is closed and delete all the free Fonts?
What we do in life... Echoes in eternity
Quote:Original post by Jemburula
Another question if you wouldn't mind. I've decided to store my TTF_Font*'s in a std::map<string, TTF_Font*> and I haven't had much experience with maps. So I'm ok with deleting one font
TTF_CloseFont(this->Fonts[FontName]);this->Fonts.erase(FontName);

but is there a way to iterate through say if my program is closed and delete all the free Fonts?


Yes, and actually that way you are doing it right now is kind of dangerous [wink] The way maps work, is when you access using [], if the key does not exist, it is created for you automatically. Needless to say that can pop in a few nasty bugs when you go to remove all your fonts and it seg faults because of a bad pointer. What you are looking for is this:
std::map<string, TTF_Font*> mapName;...std::map<string, TTF_Font*>::iterator itr;for(itr = mapName.begin(); itr!= mapName.end(); itr++){   TTF_CloseFont(itr->second);}mapName.clear();

If you're interested, I have made a templated manager that takes care of all of this. The code can be downloaded here, and this is an example of what you'd do to use it.
// Global variableManager<string, TTF_Font*> FontMgr;// Cleanup functionvoid CloseFont(TTF_Font* font){   TTF_CloseFont(font);   font = 0;}// In your init codeFontMgr.SetProcessFunctionPtr(&CloseFont);// To add in in a font with a reference name of 'font1'FontMgr.Add("font1", TTF_OpenFont("C:/windows/fonts/arial.ttf", 12));// Or you can justTTF_Font* tmpFont = TTF_OpenFont("C:/windows/fonts/arial.ttf", 12);// Mess with settings before handFontMgr.Add("font1", tmpFont);// When you need to get a font, which this is what you do if you use the font in a lot of operations, so you can not have to do re-lookups so many timesTTF_Font* tmpFont = FontMgr.Get("font1");int height = TTF_FontHeight(tmpFont);// But if you just need the font once, can also just do:int height = TTF_FontHeight( FontMgr.Get("font1") );// And if you want to remove a fontFontMgr.Remove("font1", true);// In your deinit code, this clears all the fonts, which it passes each one to our function to be closed firstFontMgr.Clear(true);

Well at least you should be able to look though that code and see how you can use iterators a bit better to speed up map usage and so on. The idea though is to use the .Find function with iterators rather than use the [] operator. That just makes your code a lot more safter and less bug prone [smile]
Using "C:/windows/fonts/arial.ttf" might be a very bad idea. What would happen if they don't have Windows installed in "C:/windows/fonts/arial.ttf"? What would happen if you were looking to port your application to Linux or Mac?

If you want to distribute a font, there is nothing wrong with distributing FreeSans.ttf. Also, SDL_ttf has a nice ability to load fonts from resource memory. TTF_OpenFontRW is probably what you're looking for. You can then compile the font in with your executable and your users wouldn't even know it's there.
Rob Loach [Website] [Projects] [Contact]
You may also use and redistribute BitStream Vera Fonts.

Edit: If you insist on using system fonts though, you can use the %SYSTEMROOT% enviroment variable to find the Windows directory (IE: %SYSTEMROOT%/fonts), make sure you surround the font loading calls with propper #ifdef WIN32 preprocessor directives.

This topic is closed to new replies.

Advertisement