Here is some example code that I've been meaning to cook up for awhile, that shows a short example of how to use SDL_ttf to render text for use with OpenGL. NeHe's freetype code seems to involve rendering of each letter into a texture, which is slight overkill for what many people would like to do. (it IS more efficient, however, since it doesn't involve the creation and deletion of textures for dynamic data)
Anyway, for reference, a simple method to render text in opengl using SDL_ttf.

Is there a way to render multi-lined text (using SDL_tff), or do you have to split it manually and reder it separated?It seems that SDL_tff (or freetype) doesn't support the newline character. Does anyone knows if i'm worng?

Original post by bewatchedIs there a way to render multi-lined text (using SDL_tff), or do you have to split it manually and reder it separated?It seems that SDL_tff (or freetype) doesn't support the newline character. Does anyone knows if i'm worng?

Not that I know of. My example just did three calls to the render function to render three lines.

Yes, it's nice and simple solution, hovewer, rendering each frame the same text on SDL_Surface by TTF_RenderText (which allocates memory -> which is slow), creating new texture, blitting one to another etc. was (in case of my code) horribly slow.

First I decided to add special function that would render text only once, and that rendered texture could be reused. But (by coincidence) I found on SDL mailing list one mail from Bob Pendleton, who has written SDL_TFF + OpenGL text rendering engine, by caching glyphs that were previously used. I've built similiar system, and it's working amazingly fast - renders multiple lines of text (yup, multiline rendering) without losing one frame, while for previous one the ratio was 5 frames / one function call :-/

I could post it here if anyone was interested... but be warned, it's rather big... very big piece of code :-)

Ok... you asked for it :-)
Remember though that you probably won't be able to use it after simple copy & paste, there are too many things and references to things that are specific to my engine. If you want to see UML diagram of my font manager, it's here:

Header of file fontMgr.h, contains interface of FontMgr, which is manager responsible for loading, managing, releasing etc. fonts and for passing them to Renderer.

//! Used only by Renderer.//! Returned value isn't modified but due to problems making one function const-correct rest had to adjust//! itself (I forgot about mutable :-) )
TGlyphsContainer * GetGlyphsContainer(constchar * _name, usint _size);

And the last (but not least one), some functions from Renderer, they are doing actual rendering to the screen. I'm doing 2d game, so they don't need to change view matrices all the time - I've assumed that when you call them, you have ortographic projection enabled etc.

As you can see, 80% of above code isn't responsible for the actual text rendering :-/ rather for loading, releasing, managing fonts, reference counting etc. Anyway, I hope someone will find it usefull.

How can I render OPENGL textures with SDL_TTF fonts and transparent background?I compiled the first example and the textures have black background.Also, I would like how to obtain a HDC (Handle Device Context) of a Main Window that is created with SDL library.

Original post by cyberpukHow can I render OPENGL textures with SDL_TTF fonts and transparent background?

That's an OpenGL specific issue, I'm not sure if it's possible, I've tried it in the past using this and couln't get it to come out how I wanted it to, so maybe someone with more OpenGL knowledge can answer that one.

Quote:

Also, I would like how to obtain a HDC (Handle Device Context) of a Main Window that is created with SDL library.

#include <SDL_syswm.h>#include <windows.h>...// global or a static local variableSDL_SysWMinfo pInfo;...// In init function just need to call onceSDL_VERSION(&pInfo.version);SDL_GetWMInfo(&pInfo); ...// Get's the DC based on the window's HWNDHWND myHwnd = pInfo.window;HDC myDC = GetDC(myHwnd);