Jump to content
  • Advertisement
Sign in to follow this  
23yrold3yrold

Using fonts in DirectX from resource files

This topic is 3328 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 all. I've been trying to improve my DirectX game with a few fancy freeware fonts I've found scattered around the Interwebs. I can embed them in an .rc file and get a valid handle to them at runtime no prob. But with DirectX, I'm used to using D3DXCreateFont() and D3DXCreateFontIndirect(), both of which take a font name and load the font from c:\windows\font. Is there some way I can get a valid DirectX font from the TrueType I'm embedding in the resource file, or do I have to use an installer program to install the font, then load it like any other? I'm kind of hoping there's an option for the former; I could figure out how to do it with NSIS or something like it I imagine, but I'd like to know if it's possible to just keep the fonts in the binary.

Share this post


Link to post
Share on other sites
Advertisement
Wow, I forgot I made this thread. Been a busy couple of days. ^_^

I've heard of AddFontMemResourceEx(); maybe I just didn't pay enough attention to it. So it will register the font in some way that D3DXCreateFontIndirect() will be able to pick up on it? I can't try it right now, but if that's so, that's pretty much what I'm looking for, thanks. [cool]

Share this post


Link to post
Share on other sites
From what I can see, it loads the font from the specified memory for the application privately (and for that currently running instance only). You'd then be able to get a HFONT to it using CreateFont or similar, assuming you knew the typeface name of the font you just loaded (doesn't seem like you can get this info out of the HANDLE AddFontMemResourceEx returns).

So, assuming D3DXCreateFont calls something like CreateFont internally, then it should work out - just AddFontMemResourceEx in the font data, and use the typeface name in your call to D3DXCreateFont.

Share this post


Link to post
Share on other sites
Yup, it worked. I'm going to detail this real quick in case someone else ever needs it; took longer than it really should have for me to clue in. [embarrass]

So basically my resource file is this:
     1 FONT "WME.ttf"
I load it like this ...
     HRSRC res = FindResource(instance, MAKEINTRESOURCE(1), RT_FONT);
HANDLE m_fonthandle;

if (res)
{
HGLOBAL mem = LoadResource(instance, res);
void *data = LockResource(mem);
size_t len = SizeofResource(instance, res);

DWORD nFonts;
m_fonthandle = AddFontMemResourceEx(data, len, NULL, &nFonts);

if(m_fonthandle == 0)
MessageBox(NULL, "Font add fails", "Error", MB_OK);
}
Then I create a font for DirectX in the usual manner ...

LPD3DXFONT myfont = NULL;
D3DXFONT_DESC FontDesc = {15, 0, 400, 0, false, DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_PITCH, "My Font"};
D3DXCreateFontIndirect(pDevice, &FontDesc, &myfont);
And now I can use it like any other ...

myfont->DrawText(NULL, "I win at embedded fonts", -1, &(MakeRect(600, 2, 200, 100)), DT_LEFT, 0xffffffff);
Clean up my crap when I'm done:

RemoveFontMemResourceEx(m_fonthandle);
Works perfectly. [cool] Thanks for the help.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!