views:

128

answers:

1

I'm writing an application which needs to draw a lot of text - several lines, maybe tens of lines - in Direct3D9. The text can be heavily formatted (i.e. different typefaces, styles, sizes) and can contain unicode symbols from different charsets. Worst of all, it can change on the fly, so it needs to be dynamic (render once display always won't do).

There are two problems with that: first, I'll probably need a lot of calls to D3DXCreateFont, which are supposedly costly (I'm not sure myself). Another approach is to create all the fonts before drawing the multi-part-line, and then just switch between them - is this better? Well, I can also create font objects on-the-fly as I draw the line, add them to some kind of font-object-cache and then look in cache before trying to create a new one? What do you think, which is the best approach?

The second problem is that D3DXFont seem to not understand underline/strike-through font styles. Although D3DFONT is based on LOGFONT, it omits those fields (supports italic though). Let's say I really really need underline/strike-through, what do I do? Is there a way to force ID3DXFont to do underline? Should I just draw those lines myself (how do I do that fast)? Or maybe I should switch to drawing with GDI on HDC and then copying those pixels into texture - will that provide reasonable performance?

A: 

Well, I can also create font objects on-the-fly as I draw the line, add them to some kind of font-object-cache and then look in cache before trying to create a new one? What do you think, which is the best approach?

The caching approach. Don't even try to recreate the fonts on-the-fly whenever you need them, with no caching. I'm unaware of D3DXCreateFont's real cost, but the function is by design not intended to be called too frequently.

Regarding underline/strikeout - you could render the lines yourself on top using IDirect3DDevice9::DrawPrimitiveUP with D3DPT_LINELIST and a pass-through shader to leave the line vertices unchanged and untransformed.

Or maybe I should switch to drawing with GDI on HDC and then copying those pixels into texture - will that provide reasonable performance?

I won't because it needs to transfer the texture contents from system RAM to VRAM.

Alexander Gessler
Thanks! I've used the caching approach, works just fine.
himself