Page 1 of 1

Handling multiple text sizes

Posted: Fri Jul 16, 2010 8:01 pm
by mv2112
In my engine i want to have freedom with what text size i can render with, but SDL_TTF only lets you choose 1 text size and you cant re-size it without reloading the text which gets slow. Right now i just pre-load all text sizes from 2-42, incrementing by 2 each time, however this is also inefficient and slow, especially on the Wii. How could i handle multiples text sizes efficiently? I have tried resizing the SDL_Surfaces with SDL_gfx but the quality isn't good. Any ideas?

Re: Handling multiple text sizes

Posted: Sat Jul 17, 2010 1:55 am
by X Abstract X
Where's the sense in loading a bunch of sizes you might not use in a particular application? Create a Font Manager and only load the sizes you know your going to be using in the application your building with the engine.

Personally, I load the fonts I need at startup and they are given an integer ID or a string name that I hash. Then I can just render text using any of the fonts I loaded.

Re: Handling multiple text sizes

Posted: Sat Jul 17, 2010 3:13 pm
by mv2112
I had thought about doing that, i was just wondering if there was a better way than just loading the fonts you need.

Re: Handling multiple text sizes

Posted: Sat Jul 17, 2010 5:00 pm
by Ginto8
mv2112 wrote:I had thought about doing that, i was just wondering if there was a better way than just loading the fonts you need.
Well if you could set up a texture and the corresponding texture coords, you could easily do it with OpenGL. If you set up the texture to be in a larger size than the default, you could just scale it down to whatever size you want the text to be, and it wouldn't have anywhere near the loss of quality that creating a texture of smaller-sized characters and scaling it up.

Re: Handling multiple text sizes

Posted: Sat Jul 17, 2010 5:12 pm
by XianForce
Here's what I suggest. Create a Font Manager, which will load and free all fonts for you.

Make a function so you can do something like this:

Code: Select all

FontManager->GetFont("name.ttf", size);
And this function should load the font in that size if it's not already loaded. You could also implement reference counting, so then every time the font is called, a +1 goes to the ref, and everytime something is done using it, a -1 to the ref count.

In this way, when the count hits 0, you actually free the resource.

So let's say there's a standard game font you use for conversation boxes or such... I'll say it's Times New Roman size 12 for sake of example. So since you'll use this font throughout the game, you'll want it to always be in mem so you don't have to wait, so in your game initialization you do:

Code: Select all

FontManager->LoadFont("Times New Roman.ttf", 12);
and in your quit stuff:

Code: Select all

FontManager->ReleaseFont("Times New Roman.ttf", 12);
This way the reference count is at least 1 while the game is running.

I'm not too sure of what you'd store it in, because you essentially have 2 values that can be unique (font and size). I think multimap would work well though. Just have the key as a string, so then you can access all the sizes of that font you have loaded...

Actually, though, I think you can do a map of maps (essentially a 2d map?) So you could do something like: Fonts[font][size] To get your specific font...

Well that's all the thoughts I have on the matter...

Re: Handling multiple text sizes

Posted: Sat Jul 17, 2010 5:53 pm
by mv2112
I tried what Ginto8 said, but with SDL_gfx and it utterly failed. I'm too much of a noob at openGL to attempt it for my engine. I will probably just load the fonts i need on start up. Right now i will keep it the way it is until i know what font sizes i want to use. Thanks for the replies, i will most likely start learning openGL soon :|