Книга: DirectX 8 Programming Tutorial

Adding Text

Adding Text

The first and easiest thing to do is add some 2D text to your scene. For this, I have created a simple wrapper class called CFont. The code for this class is shown below.

CFont::CFont(LPDIRECT3DDEVICE8 pD3DDevice, LPSTR pFontFace, int nHeight, bool fBold, bool fItalic, bool fUnderlined) {
 HFONT hFont;
 m_pD3DDevice = pD3DDevice;
 int nWeight = FW_NORMAL;
 DWORD dwItalic = 0;
 DWORD dwUnderlined = 0;
 if (fBold) {
  nWeight = FW_BOLD;
 }
 if (fItalic) {
  dwItalic = 1;
 }
 if (fUnderlined) {
  dwUnderlined = 1;
 }
 hFont = CreateFont(nHeight, 0, 0, 0, nWeight, dwItalic, dwUnderlined, 0, ANSI_CHARSET, 0, 0, 0, 0, pFontFace);
 D3DXCreateFont(m_pD3DDevice, hFont, &m_pFont);
 LogInfo("<li>Font loaded OK");
}
CFont::~CFont() {
 SafeRelease(m_pFont);
 LogInfo("<li>Font destroyed OK");
}
void CFont::DrawText(LPSTR pText, int x, int y, D3DCOLOR rgbFontColour) {
 RECT Rect;
 Rect.left = x;
 Rect.top = y;
 Rect.right = 0;
 Rect.bottom = 0;
 m_pFont->Begin();
 m_pFont->DrawTextA(pText, –1, &Rect, DT_CALCRECT, 0); //Calculate the size of the rect needed
 m_pFont->DrawTextA(pText, –1, &Rect, DT_LEFT, rgbFontColour); //Draw the text
 m_pFont->End();
}

In the constructor of CFont, we use the CreateFont function to get a handle to a new font (HFONT). Now, to use this font in your DirectX application you need to create a font object for the device. To do this, we use the D3DXCreateFont function, passing in the device pointer and font handle. We then store the resulting font pointer (LPD3DXFONT) as a member variable of the CFont class.

In the destructor we simply release the stored font object.

The final function, DrawText, renders the text. We pass in the text to render, the x and y position of the upper left corner (in screen coordinates) and the colour of the text. We use two calls to the DrawTextA method. The first calculates the dimensions of the bounding rectangle of the text and the second draws the text inside the rectangle. The text is left aligned inside the rectangle.

To use the CFont class we use the following code to create a CFont pointer:

m_pFont = new CFont(m_pD3DDevice, "Verdana", 12, false, false, false);

Once we have a pointer to the CFont object we can start drawing text. So, we have a new CGame method called RenderText which is called from inside the main Render method.

void CGame::RenderText() {
 //Draw some text at the top of the screen showing stats
 char buffer[255];
 DWORD dwDuration = (timeGetTime() – m_dwStartTime) / 1000;
 if (dwDuration > 0) {
  sprintf(buffer, "Duration: %d seconds. Frames: %d. FPS: %d.", dwDuration, m_dwFrames, (m_dwFrames / dwDuration));
 } else {
  sprintf(buffer, "Calculating…");
 }
 m_pFont->DrawText(buffer, 0, 0, D3DCOLOR_XRGB(255, 255, 255));
}

So for every frame, the RenderText method is called. RenderText simply displays the length of time that the application has been running (in seconds), the number of frames so far and the average FPS count.

Оглавление книги


Генерация: 0.723. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз