Recent Resources
-
GLSL 4.0: Discarding Fragments to Create a Perf...
-
GLSL 4.0: Using Subroutines to Select Shader Fu...
-
Building a Complete Board-based Puzzle Game wit...
-
JIRA: Programming Workflows
-
.NET Generics 4.0: Container Patterns and Best...
-
Raw Meat: Game Design Tips from Team Meat's...
-
Sedge: An Automated Error Reporting Tool
Writing a Font Engine
By Ben Hanson | Published May 17 2000 07:30 PM in Game Programming
Introduction
This article is just going to show you how to implement fonts into your game. The target is mostly for beginners. I will show you the easiest way(in my opinion) to do it. This tutorial will be in 16-bit Direct X and assumes that you have everything set up. It will use lpddsback as the back buffer.
Setting it up
First you want to make a new HFONT variable like so:
Drawing Text
Making the function to draw text using GDI is straight forward enough so I will just show you the function:
Using all of this
Now I will show you an example of how to use all this to use fonts:
[Editor's note: Although this technique is simple and straightforward, it is also quite slow compared to other methods. For games that only have limited text output or that don't require a high degree of performance, it should work well, but for higher-end games, there are much faster techniques available.]
This article is just going to show you how to implement fonts into your game. The target is mostly for beginners. I will show you the easiest way(in my opinion) to do it. This tutorial will be in 16-bit Direct X and assumes that you have everything set up. It will use lpddsback as the back buffer.
Setting it up
First you want to make a new HFONT variable like so:
HFONT fnt;Then you want to fill in the with the CreateFont Function:
//Fill in the structure with defaultWhat that does is tell windows to use Arial with all the default values. The next step is creating a function to use this information
// values and use the Arial Font
Fnt = CreateFont(14,0,0,0,0,0,0,0,0,0,0,0,0, "Arial")
Drawing Text
Making the function to draw text using GDI is straight forward enough so I will just show you the function:
void Draw_Text(char *text, int x,int y,
COLORREF color,
LPDIRECTDRAWSURFACE lpdds,
HFONT fnt)
{
//this function draws text in 16-bit DX mode
// with the selected font
HDC dc; // the dc
// get the dc from surface
lpdds->GetDC(&dc)
// set the colors
SetTextColor(dc,color);
// set background mode to transparent
// so black isn't copied
SetBkMode(dc, TRANSPARENT);
// draw the text using the font
SelectObject(dc, fnt);
TextOut(dc,x,y,text,strlen(text));
// release the dc
lpdds->ReleaseDC(dc);
} // end Draw_Text
Using all of this
Now I will show you an example of how to use all this to use fonts:
//First make the fontAnd that's all there is to it. You can change the font size by changing the 14 in the above example, and you can change the font face by changing the text in parentheses. For more information look in your compilers on-line documentation(assuming MSVC) for CreateFont. That will tell you what every parameter does.
HFONT fnt;
Fnt = CreateFont(14,0,0,0,0,0,0,0,0,0,0,0,0,"Arial");
//Draw the text in Arial
Draw_Text("This text is in Arial at Size 14",
0,0,RGB(255, 255, 255), lpddsback, fnt);
[Editor's note: Although this technique is simple and straightforward, it is also quite slow compared to other methods. For games that only have limited text output or that don't require a high degree of performance, it should work well, but for higher-end games, there are much faster techniques available.]


















