Jump to content
  • Advertisement
Sign in to follow this  
Jemburula

SDL_ttf

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 variable
Manager<string, TTF_Font*> FontMgr;

// Cleanup function
void CloseFont(TTF_Font* font)
{
TTF_CloseFont(font);
font = 0;
}

// In your init code
FontMgr.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 just
TTF_Font* tmpFont = TTF_OpenFont("C:/windows/fonts/arial.ttf", 12);
// Mess with settings before hand
FontMgr.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 times
TTF_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 font
FontMgr.Remove("font1", true);

// In your deinit code, this clears all the fonts, which it passes each one to our function to be closed first
FontMgr.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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!