Displaying Text

Basic Font

With the introduction to the DirectX sample framework covered in the
previous tutorial, we can build on that to focus on another crucial task…printing
text. We can use text to display status messages for debugging our game, or
for printing out scores to the player. While there are a few methods of getting
text up and running within DirectX, we’re going to cover a powerful font object that comes
bundled with the Direct sample framework, CD3DFont.

Device Object Event Model

In the previous tutorial, you were introduced to the basics of getting an application running
using the sample framework. The next phase of learning about what we’re working with, is to
get a small picture of the event model that the sample framework uses when working
with the Direct3DDevice interface.

The event model that is used is not what you call “space age” in terms of C++ design, but
it does the job it’s meant to do - a lightweight “alert” mechanism. In other words, throughout
the lifetime of your application, various things could happen which can affect the video surface.
For example, the player could minimize the game to open up Hotmail. Or perhaps the player wants
to play your game alongside another one and so on. These “events” need to be accounted for, otherwise
there’s a high probability your game will become corrupt and not function properly.

As we work with the CD3DFont object, we’ll point out this alerting mechanism.

Updated MyApp

class MyApp : public CD3DApplication
{
protected:
	CD3DFont*     m_pFont;              // *new* Font for drawing text

protected:
	HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
	HRESULT OneTimeSceneInit();
	HRESULT InitDeviceObjects();
	HRESULT RestoreDeviceObjects();
	HRESULT InvalidateDeviceObjects();
	HRESULT DeleteDeviceObjects();
	HRESULT FinalCleanup();
	HRESULT Render();
	HRESULT FrameMove();

public:
	MyApp();
	~MyApp();
};

Nothing space-age here. We’re just adding a CD3DFont pointer to our class definition.

InitDeviceObjects()

During the initialization process, once the Direct3DDevice is created, the framework will call
our InitDeviceObjects method which is used to initialize any graphics device object
that we want to use, including the CD3DFont.

/**
*  Name: InitDeviceObjects()
*  Desc: This creates all device-dependent managed objects, such as managed
*       textures and managed vertex buffers.
*/
HRESULT MyApp::InitDeviceObjects()
{
	m_pFont        = new CD3DFont( _T("Arial"), 10, D3DFONT_BOLD );

	// Initialize the font's internal textures
	m_pFont->InitDeviceObjects( m_pd3dDevice );

	return S_OK;
}

It’s fairly self-explanatory. We initialize our CD3DFont object to use the Arial font-family,
of font-size 10. Just for extra “body” to the font, we’re using D3DFONT_BOLD. The next
step in using the object is to call it’s own InitDeviceObjects method.

RestoreDeviceObjects

After the InitDeviceObjects method is called, or whenever the application detects that the direct3D
device has been resized, the RestoreDeviceObjects method is executed by the sample framework.

/**
* Name: RestoreDeviceObjects()
* Desc: Restore device-memory objects and state after a device is created or
*       resized.
*/
HRESULT MyApp::RestoreDeviceObjects()
{
	m_pFont->RestoreDeviceObjects();
  ...

InvalidateDeviceObjects

This method is used by the sample framework to notify any of our Direct3D device
dependent objects that they’re about to be lost. For example, when the player
hits the alt+tab to minimize your game to bring up another one.

HRESULT MyApp::InvalidateDeviceObjects()
{
	m_pFont->InvalidateDeviceObjects();

	return S_OK;
}

DeleteDeviceObjects

This method is executed by the sample framework when the application receives notice from the
player that they wish to exit the game, or in the event that the player chooses a new
Direct3D device alltogether! Say for example, the player wishes to switch the application
from being windowed into a fullscreen display.

HRESULT MyApp::DeleteDeviceObjects()
{

	m_pFont->DeleteDeviceObjects();

	return S_OK;
}

FinalCleanup

The final method that is called from the sample framework occurs just before the application
exits to the Windows operating system. This is pretty much the very last chance to make
sure you’re freeing any allocated memory to prevent any memory leaks.

HRESULT MyApp::FinalCleanup()
{
	SAFE_DELETE( m_pFont );

	return S_OK;
}

Printing Our Text!

Last but not least, after learning the basics behind the DirectX sample framework’s device
object notification system, we can use the actual CD3DFont object to draw
our text!
We only need to use the DrawText method as shown in this snippet:

    // Output statistics
    // DrawText( x-position, y-position, text-color, text )
		m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
		m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

That’s it for this lesson!

Feedback? Comments?

Either comment to this posting, or use our contact form to get in touch with us.

Downloads

dx81-BasicFont.zip




Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>