Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Fonts, resource files, and Visual C++ 2008 Express

This thread is locked; no one can reply to it. rss feed Print
Fonts, resource files, and Visual C++ 2008 Express
23yrold3yrold
Member #1,134
March 2001
avatar

Hey all. Good news; I may have an actual game to show off. Just trying to get through the final details ...

One of those details is using a font in my game. Now, I know how to use the font, and I have it working now, but what I want to do is embed it as a resource, or even just load it from a data folder (right now it only works if the font is in c:\windows\font). Visual Studio doesn't seem to have a mechanism for managing resource files like my old Visual C++ 4.0 did and I haven't touched .rc files in years to boot, and Google isn't turning up anything useful. Anyone got any tips on how to embed .ttf files in a Windows executable using Visual C++ 2008 Express?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

verthex
Member #11,340
September 2009
avatar

So wheres the game?

Oscar Giner
Member #2,207
April 2002
avatar

Visual C++ Express editions lack the resource editor. If you know someone who has a comercial version you can ask him a favour ;) Once you have the resource file done I think you can include it in the project and build with the express version.

23yrold3yrold
Member #1,134
March 2001
avatar

Editing the resource file isn't an issue; it's only a few fonts. I'm just not sure how to include the font in the .rc file and include the file in the project. Visual C++ Express is completely devoid of any menu or property options related to resources it seems ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

torhu
Member #2,727
September 2002
avatar

The syntax is here:
http://msdn.microsoft.com/en-us/library/aa381012%28VS.85%29.aspx

As for including the file, did you try adding it through the project menu in msvc?

23yrold3yrold
Member #1,134
March 2001
avatar

Okay, I seem to have the font, but I'm not sure how to "use" it. I appear to be getting a valid HANDLE from AddFontMemResourceEx(), but I don't know how to proceed, or if I'm already messing up. For DirectX, I've been calling D3DXCreateFontIndirect() to load an installed font. I'd like to be able to use it directly from the resource file, but I don't know how to intergrate that into DirectX. Is there a function I can use to make a LPD3DXFONT that accepts a HANDLE? Is there a way to register the font and then use D3DXCreateFontIndirect() with the font's name? Can I install the font from the resource file into c:\windows\font, maybe involving messing with the registry a bit? It's hard to search for a lot of this stuff without running into a ton of unrelated subject material, someone give me a little push in the right direction ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Tobias Dammers
Member #2,604
August 2002
avatar

Can't you install fonts through an installer project?

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

23yrold3yrold
Member #1,134
March 2001
avatar

I'd kinda like to figure out how to do it both ways. Something about installing something that sticks around if they uninstall rubs me the wrong way ...

EDIT: I like how Googling for this issue brings up this thread on the first page ...

EDIT: Fixed, I didn't realize AddFontMemResourceEx() registered the font in a way that made it accessible like any other. 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 your crap when you're done:

  RemoveFontMemResourceEx(m_fonthandle);

Maybe this will help someone else someday.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Go to: