home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
font2D.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-10-03
|
5KB
|
200 lines
#include "main.h"
//------------------------------------------------------------------
// Name: FONT2D()
// Desc: Konstruktor
//------------------------------------------------------------------
FONT2D::FONT2D()
{
g_pTexture = NULL;
Vertex = NULL;
//nastavenie vlastnosti
Color = GetColor(1.0f,1.0f,1.0f,1.0f);
}
//------------------------------------------------------------------
// Name: ~FONT2D()
// Desc: Destructor
//------------------------------------------------------------------
FONT2D::~FONT2D()
{
if (g_pTexture != NULL)
g_pTexture->Release();
if (Vertex != NULL)
delete [] Vertex;
}
//------------------------------------------------------------------
// Name: LoadFont()
// Desc: inicializacia fontu
//------------------------------------------------------------------
void FONT2D::LoadFont(char *FileName,COLOR ColorKey,int WidthChar,int HeightChar)
{
Width = WidthChar;
Height = HeightChar;
ActVer = 0;
ActChar = 0;
//vertex pole
Vertex = new CUSTOMVERTEXFONT[1200];
//log
char cBuf[80];
LogPrint("Vytvaram 2D Font");
sprintf(cBuf," Font: %s",FileName);
LogPrint(cBuf);
if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,
FileName,
D3DX_DEFAULT,
D3DX_DEFAULT,
0, //MipLevels
0,
Engine.TextureFormat,
Engine.TextureCreatingFlag,
D3DX_DEFAULT, //Filter
D3DX_DEFAULT, //MipFilter
D3DXCOLOR (ColorKey.R,
ColorKey.G,
ColorKey.B,
ColorKey.A), //ColourKey
NULL,
NULL,
&g_pTexture)))
{
sprintf(cBuf," chyba pri vytvarani textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Textura fontu vytvorena");
}
}
//------------------------------------------------------------------
// Name: Print()
// Desc: Vytlaci text
//------------------------------------------------------------------
void FONT2D::Print(float X,float Y,char *Text)
{
for (int i=0;i < (strlen(Text));i=i+1)
{
PrintCharacter(X+(i*(Width*0.75f)),Y,Text[i]);
}
}
//------------------------------------------------------------------
// Name: PrintCharacter()
// Desc: Vytlaci znak
//------------------------------------------------------------------
void FONT2D::PrintCharacter(float X,float Y,char Pis)
{
float StartX,StartY; //suradnice zaciatku pismena na BMP
float EndX,EndY; //suradnice na konci
float Asci; //acikod
float Riadok;
float Stlpec;
Asci =(float) Pis;
Asci = Asci / 16.0f;
Riadok =((float) ((int)(Asci))) * 16.0f;
Stlpec = (Asci - Riadok) * 16.0f * 16.0f;
StartX = Stlpec / 256.0f;
EndX = (Stlpec + 16.0f) / 256.0f;
StartY = (Riadok + 1.0f) / 256.0f;
EndY = ((Riadok + 16.0f) / 256.0f) ;
//spolocne vlastnosti
for (int i=ActVer;i<ActVer+6;i=i+1)
{
Vertex[i].z = 0.0f;
Vertex[i].rhw = 1.0f;
Vertex[i].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
}
//
//1
//
Vertex[ActVer].x = X;
Vertex[ActVer].y = Y;
Vertex[ActVer].tu = StartX;
Vertex[ActVer].tv = StartY;
ActVer = ActVer + 1;
Vertex[ActVer].x = X+Width;
Vertex[ActVer].y = Y;
Vertex[ActVer].tu = EndX;
Vertex[ActVer].tv = StartY;
ActVer = ActVer + 1;
Vertex[ActVer].x = X;
Vertex[ActVer].y = Y+Height;
Vertex[ActVer].tu = StartX;
Vertex[ActVer].tv = EndY;
ActVer = ActVer + 1;
ActChar = ActChar + 1;
//
//2
//
Vertex[ActVer].x = X+Width;
Vertex[ActVer].y = Y;
Vertex[ActVer].tu = EndX;
Vertex[ActVer].tv = StartY;
ActVer = ActVer + 1;
Vertex[ActVer].x = X+Width;
Vertex[ActVer].y = Y+Height;
Vertex[ActVer].tu = EndX;
Vertex[ActVer].tv = EndY;
ActVer = ActVer + 1;
Vertex[ActVer].x = X;
Vertex[ActVer].y = Y+Height;
Vertex[ActVer].tu = StartX;
Vertex[ActVer].tv = EndY;
ActVer = ActVer + 1;
ActChar = ActChar + 1;
}
//------------------------------------------------------------------
// Name: Render()
// Desc: Vyrenderuje vytlaceny text
//------------------------------------------------------------------
void FONT2D::Render()
{
//ak nieje napisany ziadny text koniec funckie
if(ActChar==0) return;
g_pd3dDevice->SetTexture( 0, g_pTexture);
// Render the vertex buffer contents
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEXFONT );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLELIST,ActChar,(BYTE**)&Vertex[0], sizeof(CUSTOMVERTEXFONT));
ActVer = 0;
ActChar = 0;
}