Search code examples
c++directxdirectx-9

How to efficiently and correctly resize ID3DXFont


I am looking to resize a DirectX font object in a for loop. See below for an example:

for ( int i = 1; i < 20; i++ ) {
    // font size should be i
}

It would be inefficient to create a font for every single possible size so I was wondering if I could create some sort of wrapper or my own font renderer that allows me to do so with good performance?

If I don't make much sense, then see the another example below:

// DirectX9
// calling D3DXCreateFont every loop would be incredibly inefficient.

ID3DXFont *example_font;
D3DXCreateFont( device, 14 /* font size - we want this to change */, ..., &example_font );

How can I achieve this? I tried using scale/rotate/transform to simply change the size but the font becomes blurry so I'm looking for a more "correct" solution.

Thank you.


Solution

  • The problem you are encountering is a fundamental limitation of 'raster bitmap' fonts as opposed to 'vector' fonts like TrueType. D3DXFont is a 'raster bitmap' solution that captures a TrueType font into a bitmap at a specific size. This can be scaled/rotated somewhat and still holds up, but you get artifacts if you change the size a lot as you have seen.

    It is extremely fast, however, because it's just drawing textures. You can also capture fonts at a few different sizes and pick one based on the resolution.

    D3DXFont is part of D3DX9 which means it is deprecated. Of course Direct3D 9 is legacy too, so you should look at Direct3D 11 and SpriteFont/SpriteBatch in the DirectX Tool Kit for a modern 'raster bitmap' font solution.

    The correct solution for high-quality, highly scalable text drawing is to use a 'vector font' drawing system like DirectWrite.