General Development

C# Embedded fonts (how to use a font resource) – dartsman

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
Hello, I am working on a quick game prototype in C# and I am using GDI for the graphics. I've run into a problem that I use some fonts in the game (DrawString) that are not common ones. I would usually use bitmap fonts, but I thought it'd be a lot better if I could just embed the fonts that I use in my game executable.

BTW, I know there are copyright rules with fonts (and other content) and I won't release anything unless I'm sure I'm allowed to use the fonts. Also this would allow me to embed any royalty free fonts if I find any that suit my game.

Can anyone help me with how to do something like this in C#?

------------------
jonwarner.net

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
You don't need to do this, but I like to add a Resource directory off of my main source directory and place all of my resources in there. You can make a Resource/Font directory. Then add the Resource/Font directory into your project. Place your font file in the Font directory and then add your font file to the project. In any case, you need to add your Font file to the project.

It should look something like this in Studio:

Resource
|
|__Font
|
|__MySpecialFont.ttf

Each file in your project has a special build action association. You want to set the build action to Embedded Resource. This will place the resource inside your assembly.

The extraction is a bit more involved. Take a look here for an example, although it does not have a Font example.

[This message has been edited by ssquared (edited November 20, 2007).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
thanks, yeah, I've embedded images before but the main problem is how do I pull out a font from the resource. Images seem a lot nicer to pull out as you can load up an image from the stream... is this the same as what I would need to do for a Font?

------------------
jonwarner.net

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
sorry about double post but:

I think I found what I needed from the following post:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2398181&SiteID=1

I will test it out tonight.

------------------
jonwarner.net

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
Ooops. You are right. Looks like fonts are a different case. If you find out the secret, please post here. I would like to know for future reference. Thanks.
Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
There are two other ways you could do this:

1. Don't embedd the font files in your app's resources, but have your installer include and install them like any other font.

2. store them in your resources as RT_DATA (generic binary), and when your app starts, use FindResource and LockResource to read them into buffers, then write those buffers to temporary files. Your app can then use those temp files like any other font file. (RT_DATA, FindResource and LockResource are what they're called in a C++ project, C# may use different names).

------------------
Brian

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
good ideas...

I'd just prefer to have them included inside the app so that I don't require an installer. Plus it's annoying when programs require you to install something (in this case a Font or two) on your machine.

I wasn't able to do the font thing last night instead I did some pretty neat physics additions to the game. I now have rotating obstacles (if you view the Stickman Steve demo it's an 'upgrade' to the regular blue circle obstacle) which can be paired up to then drag and shoot the ragdolls in the rough direction. So I can also line up a series of these to make a cog like effect where it'll drag them through the series of circles. Both of which look pretty neat.

------------------
jonwarner.net