home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
2dpanel.cpp
next >
Wrap
C/C++ Source or Header
|
2004-10-03
|
8KB
|
356 lines
#include "main.h"
//------------------------------------------------------------------
// Name: konstructor
// Desc:
//------------------------------------------------------------------
PANEL2D::PANEL2D()
{
g_pTexture = NULL; //textura
pVertices = NULL; //vertexi
//default vlastnosti
Rot = 0.0f;
Pos = Get3D(0.0f,0.0f,0.0f);
Color = GetColor(1.0f,1.0f,1.0f,1.0f);
NumFrames = 0;
Frame = 0.0f;
RelativeMode = false;
Scale = 1.0f;
}
//------------------------------------------------------------------
// Name: destructor
// Desc:
//------------------------------------------------------------------
PANEL2D::~PANEL2D()
{
//znic textury
if (g_pTexture != NULL)
{
for (int i=0;i<NumFrames;i=i+1)
{
if (g_pTexture[i] !=NULL)
g_pTexture[i]->Release();
}
delete [] g_pTexture;
}
g_pTexture = NULL;
//znic vertexi
if (pVertices !=NULL)
delete [] pVertices;
pVertices = NULL;
}
//------------------------------------------------------------------
// Name: Create()
// Desc: Vytvori 2D Panel , PanelWidth - sirka v pixeloch,
// PanelHeight - vyska v pixeloch
// NumAnimFrames - pocet animacnych snimkov
//------------------------------------------------------------------
void PANEL2D::Create(float PanelWidth, float PanelHeight,int NumAnimFrames)
{
char cBuf[80];
sprintf(cBuf,"Vytvaram 2D Panel NumFrames: %d",NumAnimFrames);
LogPrint(cBuf);
//vytvori textury
g_pTexture = new LPDIRECT3DTEXTURE9[NumAnimFrames];
for (int i=0;i<NumAnimFrames;i++)
{
g_pTexture [i] = NULL;
}
//vertexi
pVertices = new CUSTOMVERTEX2DPANEL[6];
//sirka vyska
Width = PanelWidth;
Height = PanelHeight;
//zresetuje vertexi
ResetPoints();
}
//------------------------------------------------------------------
// Name: AddFrame()
// Desc: Prida snimok, je mozne pridat kolko snimkov
//------------------------------------------------------------------
void PANEL2D::AddFrame(char *FileName,COLOR ColorKey)
{
char cBuf[80];
sprintf(cBuf," AddFrame: %d %s",NumFrames,FileName);
LogPrint(cBuf);
//vytvor texturu
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[NumFrames])))
{
sprintf(cBuf," chyba pri vytvarani textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Textura vytvorena");
}
NumFrames = NumFrames + 1;
}
//------------------------------------------------------------------
// Name: ClearAllFrames()
// Desc: znici vsetky snφmky
//------------------------------------------------------------------
void PANEL2D::ClearAllFrames()
{
if (NumFrames == 0)
return;
if (g_pTexture != NULL)
{
for (int i=0;i<NumFrames;i=i+1)
{
if (g_pTexture[i] !=NULL)
g_pTexture[i]->Release();
}
delete [] g_pTexture;
}
g_pTexture = NULL;
g_pTexture = new LPDIRECT3DTEXTURE9[NumFrames];
for (int i=0;i<NumFrames;i++)
{
g_pTexture [i] = NULL;
}
NumFrames = 0;
}
//------------------------------------------------------------------
// Name: ResetPoints()
// Desc: reset vertexov (P1-P4) podla vysky a sirky
//------------------------------------------------------------------
void PANEL2D::ResetPoints()
{
float PW = Width/2.0f - 0.5f;
float PH = Height/2.0f - 0.5f;
PW *= Scale;
PH *= Scale;
//reset vertexov
P1.X = Pos.X - PW ;
P1.Y = Pos.Y - PH ;
P1.Z = 0.0f ;
P2.X = Pos.X + PW ;
P2.Y = Pos.Y - PH ;
P2.Z = 0.0f ;
P3.X = Pos.X + PW ;
P3.Y = Pos.Y + PH ;
P3.Z = 0.0f ;
P4.X = Pos.X - PW ;
P4.Y = Pos.Y + PH ;
P4.Z = 0.0f ;
}
//------------------------------------------------------------------
// Name: GetNewPos()
// Desc: Vrati novu poziciu podla uhla
//------------------------------------------------------------------
VECTOR3D PANEL2D::GetNewPos(VECTOR3D Centre,VECTOR3D Point)
{
//vysledok
VECTOR3D Vys;
float Distance = CalcDistance(Centre,Point);
float SubY = (Point.Y - Centre.Y) / Distance;
float SubX = (Point.X - Centre.X) / Distance;
float Angle = asinf(SubX);
if (SubY < 0.0f)
{
if (SubX > 0.0f)
Angle = 3.141f - Angle;
if (SubX < 0.0f)
Angle = 3.141f - Angle;
}
Angle = Angle + Rot;
Vys.X = Centre.X + (sinf(Angle) * Distance);
Vys.Y = Centre.Y + (cosf(Angle) * Distance);
Vys.Z = 0.0f;
return Vys;
}
//------------------------------------------------------------------
// Name: UpDateVertices()
// Desc: Updatuje pole vertexov
//------------------------------------------------------------------
void PANEL2D::UpDateVertices()
{
//reset
ResetPoints();
//vypocitaj pozicie vertexov podla uhla
P1 = GetNewPos(Pos,P1);
P2 = GetNewPos(Pos,P2);
P3 = GetNewPos(Pos,P3);
P4 = GetNewPos(Pos,P4);
//relativne
if (RelativeMode == true)
{
P1.X = FTRX(P1.X);
P1.Y = FTRY(P1.Y);
P2.X = FTRX(P2.X);
P2.Y = FTRY(P2.Y);
P3.X = FTRX(P3.X);
P3.Y = FTRY(P3.Y);
P4.X = FTRX(P4.X);
P4.Y = FTRY(P4.Y);
}
//spolocne vlastnosti
for (int i = 0;i<4;i=i+1)
{
pVertices[i].color = D3DXCOLOR(Color.R,Color.G,Color.B,Color.A);
pVertices[i].rhw = 1.0f;
pVertices[i].z = 0.0f;
}
//
//1
//
pVertices[0].x = P1.X;
pVertices[0].y = P1.Y;
pVertices[0].tu = 0.0f ;
pVertices[0].tv = 0.0f;
pVertices[1].x = P2.X;
pVertices[1].y = P2.Y;
pVertices[1].tu = 1.0f ;
pVertices[1].tv = 0.0f;
pVertices[2].x = P3.X;
pVertices[2].y = P3.Y;
pVertices[2].tu = 1.0f ;
pVertices[2].tv = 1.0f;
pVertices[3].x = P4.X;
pVertices[3].y = P4.Y;
pVertices[3].tu = 0.0f ;
pVertices[3].tv = 1.0f;
}
//------------------------------------------------------------------
// Name: Render()
// Desc: Verenderuje
//------------------------------------------------------------------
void PANEL2D::Render()
{
//automaticky pretocit
if (Frame > NumFrames)
Frame = 0.0f;
//obnov vertexi
UpDateVertices();
//oznac prislusnu texturu
g_pd3dDevice->SetTexture( 0, g_pTexture[(int)Frame]);
//vykresli
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX2DPANEL );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN,2,(BYTE**)&pVertices[0], sizeof(CUSTOMVERTEX2DPANEL));
}
//------------------------------------------------------------------
// Name: MousePick()
// Desc: kolizia s bodom na obrazovke
//------------------------------------------------------------------
bool PANEL2D::MousePick(VECTOR3D Point)
{
float U1 = CalcAngleCentre(Point,P1,P2);
float U2 = CalcAngleCentre(Point,P2,P3);
float U3 = CalcAngleCentre(Point,P3,P4);
float U4 = CalcAngleCentre(Point,P4,P1);
float U = U1 + U2 + U3 + U4;
if ((U > 6.24f) && (U < 6.30f))
return true;
else
return false;
}
//------------------------------------------------------------------
// Name: Center()
// Desc: zarovanie podla sirky a vysky enginu
//------------------------------------------------------------------
void PANEL2D::Center(VECTOR3D CPos)
{
Pos.X = Engine.Width * CPos.X;
Pos.Y = Engine.Height * CPos.Y;
Pos.Z = 0.0f;
}
//------------------------------------------------------------------
// Name: SetTexture
// Desc: nastavi externu texturu
//------------------------------------------------------------------
void PANEL2D::SetTexture(int ToFrame,LPDIRECT3DTEXTURE9 Tex)
{
g_pTexture[ToFrame] = Tex;
}