Direct3D.Font using local folder ttf

Started by
13 comments, last by DrunkenHyena 19 years ago
How would I load a font file in a folder relative to the application.startpath? The tuts out there describes using a System.Drawing.Font object, which is created by specifying the string name of the font. Correct me if I'm wrong, but I can't see why you would want to install ttf fonts into a windows directory in order to load fonts in your game. So, yeah, how would I load a local font?
Advertisement
Creating a Private Font Collection (From MSDN) should have pretty much everything you need.
Sirob Yes.» - status: Work-O-Rama.
Also (for those who are using native C++ and want to know), you should be able to use the AddFontResourceEx() and RemoveFontResourceEx() Win32 APIs to accomplish the same thing.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
Quote:Original post by circlesoft
Also (for those who are using native C++ and want to know), you should be able to use the AddFontResourceEx() and RemoveFontResourceEx() Win32 APIs to accomplish the same thing.
I've used them, and they work gerat.
One thingto beware of, is that they only work on Win2k+, so if you're targeting Win9x, you're out of luck.
Quote:Original post by Evil Steve
Quote:Original post by circlesoft
Also (for those who are using native C++ and want to know), you should be able to use the AddFontResourceEx() and RemoveFontResourceEx() Win32 APIs to accomplish the same thing.
I've used them, and they work gerat.
One thingto beware of, is that they only work on Win2k+, so if you're targeting Win9x, you're out of luck.



AddFontResource and RemoveFontResource work fine under Win9x. I think the only difference is that the Ex versions allow you to make the font private.

Stay Casual,KenDrunken Hyena
After I read the PrivateFontCollection tutorial from MSDN, I coded this:

PrivateFontCollection gameFonts = new PrivateFontCollection();gameFonts.AddFontFile( Application.StartupPath + @"\fonts\FFFGALAX.TTF" );Font sFont = new WinFont( gameFonts.Families[0].Name, 6, Fontstyle.Regular, GraphicsUnit.Pixel);	smallFont = new D3DFont( game.device, sFont );


I thought it was working; because I saw the fonts. But when I ran the game from another computer that didn't have the fonts 'installed' into the windows/fonts folder; the fonts that displayed were default fonts. When I looked at this code, I realized that although I'm creating a PrivateFontCollection, I'm using the same code as before to create the Font object; the font's name is passed and created from the installed windows fonts. Obviously this is not what I want. I looked at the other overloads of the Font objects constructor, and there was one that seemed like a possible; it expected a Family object:

Font sFont = new WinFont( gameFonts.Families[0], 6, Fontstyle.Regular, GraphicsUnit.Pixel);


But, still the same result. So, anyone have a suggestion? Thanks.
:'(
Quote:Original post by DrunkenHyena
AddFontResource and RemoveFontResource work fine under Win9x. I think the only difference is that the Ex versions allow you to make the font private.

That should probably be okay, it's not like you really need to keep it private anyways. If they really wanted to get the font anyways, they would just copy it right out of the resource directory [wink]

Quote:
But, still the same result. So, anyone have a suggestion? Thanks.

Here is some example source from MSDN. It looks like they are using the Font class, instead of WinFont.

VOID Example_AddFontFile(HDC hdc){   Graphics              graphics(hdc);   SolidBrush            solidBrush(Color(255, 0, 0, 0));   INT                   found = 0;   INT                   count = 0;   WCHAR                 familyName[50];   FontFamily*           pFontFamily;   PrivateFontCollection privateFontCollection;   // Add three font files to the private collection.   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Arial.ttf");   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Cour.ttf");   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Times.ttf");   // How many font families are in the private collection?   count = privateFontCollection.GetFamilyCount();   // Allocate a buffer to hold the array of FontFamily objects returned by   // the GetFamilies method.   pFontFamily = (FontFamily*)malloc(count * sizeof(FontFamily));   // Get the array of FontFamily objects.   privateFontCollection.GetFamilies(count, pFontFamily, &found);   for(INT j = 0; j < found; ++j)   {      // Get the font family name.      pFontFamily[j].GetFamilyName(familyName);         // Pass the family name and the address of the private collection to a      // Font constructor.      Font* pFont = new Font(familyName, 16, FontStyleRegular,                             UnitPixel, &privateFontCollection);      // Use the font to draw a string.      graphics.DrawString(                          L"Hello",                           5,          // string length                           pFont,                           PointF(10.0f, (REAL)j*25),                           &solidBrush);      delete(pFont);   }   free(pFontFamily);}
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
WinFont is just an alias for the Font class:

using WinFont = System.Drawing.Font;

So, I am using the right class. :(
Can anyone try to get this working? Create a PrivateFontCollection with a font that is not installed into your windows/fonts folder and see if you can get it to load and draw?

This topic is closed to new replies.

Advertisement