Writing a Font Engine
Describes a simple, if inefficient, method to add font support to your game.
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.
[size="5"]Setting it up
First you want to make a new HFONT variable like so:
HFONT fnt;//Fill in the structure with default
// values and use the Arial Font
Fnt = CreateFont(14,0,0,0,0,0,0,0,0,0,0,0,0, "Arial")[size="5"]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[size="5"]Using all of this
Now I will show you an example of how to use all this to use fonts:
//First make the font
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.]
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Ben Hanson
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion