XFactorDev.net Article   XFactorDev.net Article

Prt-7- Lights camera action...CCamera.

by Smuggl0r

 

In this tutorial, we're going to write a class which includes a CreateFont() function, a DisplayText(WCHAR string[200], float xpos, float ypos, int r, int g, int b) function, and a ReleaseBuffers() function.
So first, here is the code for the Text.h file:

class CText
{
public:

//Function to create the font. Called once at start of game.
void Create( IDirect3DDevice8** pd3dDevice );

//Function that displays the actual text, as you can see you can set the text, x axis //position, y axis position, and   //red,green,blue values for the colour
void DisplayText(WCHAR string[200], float xpos, float ypos, int r, int g, int b);

//Function to clear up and release the font buffers. Called once at end of game
void Release();
};

Then in your main cpp file, we just use it like this:

void main()
{
 

/*****

other code

****/


// Creates a class of CText called My_Text which you will use to call the functions in the // class
CText My_Text;

//Create the fonts. The g_pd3dDevice bit should already be declared in your game in your globals as: //LPDIRECT3DDEVICE8 g_pd3dDevice = NULL;  Its just a class for the Direct3D device
My_Text.CreateFont( &g_pd3dDevice );

while( TRUE )
{
//Function thats called all the time, you could put the DisplayText function in here
Render();
}

// Release the buffers set up by the fonts
My_Text.Release();

}

I usually declare the class in globals of the program, as you can then use it in any function. To declare it in globals is just the same: CText My_Text;
Just put it in the globals section

Now, for the function where you will call the DisplayText function, this is what it should look like. This is in Render(), but you should be able to put it anywhere

void Render()
{
// Set up the variables to hold the red, green, and blue values
int red = 0;
int green = 0;
int blue = 0;

// Set up the variable to hold the string
WCHAR my_string[200];

// Clear the backbuffer to a black color
g_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0L );

// Begin the scene
g_pd3dDevice->BeginScene();

// This is the bit where we set up all the parameters for the function and call it
red = 255;
green = 255;
blue = 255;
// This is needed to set the string with your own text
wsprintfW(my_string, L"Hello World");
// string xpos ypos r g b
My_Text.DisplayText(my_string,225.0f,200.0f,red,green,blue);
}

One more thing before i get onto the .cpp of the class, is to use the function: wsprintfW
You need to #include <winbase.h>. Just add it at the top of your code with all of the others, in the main.cpp

Ok, now on to the .cpp of the class
This is what should be in the Text.cpp:

#include "stdafx.h"
#include <winbase.h>
#include <xfont.h>
#include "Text.h"

//Create some DirectX text buffers
XFONT* m_pArial18BitmapFont; // Pointer to the Arial18Normal Bitmap font
LPDIRECT3DSURFACE8 g_pFrontBuffer;

void CText::Create( IDirect3DDevice8**  pD )
{

//Sets up the device using the one parsed in the function from the main part
m_pd3dDevice = *pD;
//InitialiseFonts
m_pd3dDevice->GetBackBuffer(-1,D3DBACKBUFFER_TYPE_MONO,&g_pFrontBuffer);
DWORD dwFontCacheSize = 16 * 1024;

//Load our font in - have to sepcify its loacation
XFONT_OpenBitmapFont( L"D:\\Arial18Normal.bmf",
dwFontCacheSize, &m_pArial18BitmapFont );

}

void CText::DisplayText(WCHAR string[200], float xpos, float ypos, int r, int g, int b)
{

WCHAR szbuff[200] = {0};

wsprintfW(szbuff, L"%s", string);

//Display our text.

m_pArial18BitmapFont->SetTextColor(D3DCOLOR_XRGB(r,g,b));
m_pArial18BitmapFont->TextOut( g_pFrontBuffer, szbuff, -1, (long)xpos, (long)ypos );

}

void CText::Release()
{
//Release our TextBuffers
m_pArial18BitmapFont->Release();
g_pFrontBuffer->Release();
}


Also, the last thing you have to do is put this line at the top of your main.cpp:
#include "Text.h"
 

We are using a class in this example, to keep things all organised and also so its much easier to just plug into other projects, just by #include'ing the header file in the main.cpp, and declaring a new class, like CText My_Text;

Its much like a Lego Block system where you can put together pieces you want, and use them all together to build one big block ( or project if you get fed up of Lego Bricks ).

  XFactorDev.net Article



      
webmaster@xfactordev.net