home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
engine.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-11-24
|
18KB
|
622 lines
#include "main.h"
//---------------------------------------------------------------------
//Globalne PremennΘ
//---------------------------------------------------------------------
//DIRETCX
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
//WINDOWS
HWND hWnd = NULL;
HINSTANCE hIns = NULL;
//MATICA
D3DXMATRIXA16 matProj; //projekcia
//ENGINE
ENGINE Engine;
/*
//--------------------------------------------------------------------
// Name: Initialze()
// Desc: Pred Inicializaciou musia byt zadane Height, Width a Windowed
//--------------------------------------------------------------------
*/
void ENGINE::Initialize()
{
//log start
char cBuf[80];
LogPrint(">>>Inicializijem ENGINE");
LogPrint("Nastavenia:");
sprintf(cBuf," Sirka: %d Vyska: %d",Width,Height);
LogPrint(cBuf);
sprintf(cBuf," Windowed: %d ",Windowed);
LogPrint(cBuf);
sprintf(cBuf," Max: %.2f Min: %.2f",Max,Min);
LogPrint(cBuf);
sprintf(cBuf," BackColor: %f %f %f",BackColor.R,BackColor.G,BackColor.B);
LogPrint(cBuf);
sprintf(cBuf," Format Obrazovky: %d ",Format);
LogPrint(cBuf);
sprintf(cBuf," Format Textur: %d ",TextureFormat);
LogPrint(cBuf);
sprintf(cBuf," Flag Textur: %d ",TextureCreatingFlag);
LogPrint(cBuf);
sprintf(cBuf," Stencil Shadows: %d ",StencilShadows);
LogPrint(cBuf);
/////////////////////
//Vytvorenie g_pD3D//
/////////////////////
if (FAILED(g_pD3D = Direct3DCreate9( D3D_SDK_VERSION )))
{
LogPrint("Nemozno vytvorit g_pD3D");
MessageBox(hWnd,"Chyba poΦas vytvßrania Direct3D9",
"ERROR",MB_OK|MB_ICONERROR);
}
else
{
LogPrint("g_pD3D vytvorene");
}
//zisti mod obrazovky
D3DDISPLAYMODE d3ddm;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
////////////////////////
//nastavanie Viewportu//
////////////////////////
Viewport.Height = Height;
Viewport.Width = Width;
Viewport.X = 0;
Viewport.Y = 0;
Viewport.MaxZ = Max;
Viewport.MinZ = Min;
////////////////////
//paremetre obrazu//
////////////////////
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = Windowed;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferWidth = Width;
d3dpp.BackBufferHeight = Height ;
// !! nastavi 60 hz pre moj pitomi monitor !!
//if (Resolution == 2) d3dpp.FullScreen_RefreshRateInHz = 60;
if (Windowed == true)
d3dpp.BackBufferFormat = d3ddm.Format ;
else
d3dpp.BackBufferFormat = Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
////////////////////////////////
//Zisti podporu Stencil Shadow//
////////////////////////////////
if (StencilShadows == true)
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
if(FAILED(g_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
d3dpp.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
D3DRTYPE_SURFACE, D3DFMT_D24S8)))
{
StencilShadows = false;
LogPrint("Grafickß karta nepodporuje Stencil Buffer");
MessageBox(hWnd,"VaÜa grafickß karta nepodporuje stencil tiene",
"ERROR",MB_OK|MB_ICONWARNING);
}
else
{
if(FAILED(g_pD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
d3dpp.BackBufferFormat, d3dpp.BackBufferFormat, D3DFMT_D24S8)))
{
StencilShadows = false;
LogPrint("Depht Stencil nieje kompatibliny s modom obrazovky");
MessageBox(hWnd,"Depht Stencil nieje kompatiblin² s m≤dom obrazovky",
"ERROR",MB_OK|MB_ICONWARNING);
}
}
//preni mod ak je stencil vypnuty
if (StencilShadows == false)
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
}
}
//////////////////////////
//inicializacia Direct3D//
//////////////////////////
int Mode=1;
while(1==1)
{
// Mode 1
if (Mode == 1)
{
if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
Mode = 2;
else
Mode = 0;
continue;
}
// Mode 2
if (Mode == 2)
{
if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
StencilShadows = false;
Mode = 3;
}
else
Mode = 0;
continue;
}
// Mode 3
if (Mode == 3)
{
if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )))
{
Mode = -1;
}
else
Mode = 0;
continue;
}
//ERROR
if (Mode == -1)
{
LogPrint("chyba pri inicializacii g_pd3dDevice ") ;
MessageBox(hWnd,"VaÜa grafickß karta nieje kompatibilnß s Direct3D9",
"ERROR",MB_OK|MB_ICONERROR);
break;
}
//OK
if (Mode == 0)
{
LogPrint("inicializacia g_pd3dDevice uspesna") ;
break;
}
}
///////////
//D3DCAPS//
///////////
D3DCAPS9 Caps;
g_pd3dDevice->GetDeviceCaps(&Caps);
//stencil
if (StencilShadows == true)
{
if (!(Caps.StencilCaps & D3DSTENCILCAPS_DECR) ||
!(Caps.StencilCaps & D3DSTENCILCAPS_INCR) ||
!(Caps.StencilCaps & D3DSTENCILCAPS_KEEP))
{
LogPrint("D3D Device nemoze pouzit Stencil Buffer");
StencilShadows = false;
}
}
//////////////////////
//OSTATNE NASTAVENIA//
//////////////////////
//normalizuj normaly
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE);
//deepht bias slope scale
g_pd3dDevice->SetRenderState( D3DRS_SLOPESCALEDEPTHBIAS, FtoDW(-0.3f) );
g_pd3dDevice->SetRenderState( D3DRS_DEPTHBIAS, FtoDW(0.0f) );
//ambient
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT,0xff555555);
}
/*
//--------------------------------------------------------------------
// Name: CleanUp()
// Desc: Vycistenie pamati
//--------------------------------------------------------------------
*/
void ENGINE::CleanUp()
{
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
/*
//--------------------------------------------------------------------
// Name: SetBlendNone()
// Desc: Ziadna priesvitnost - vypne priesvitnos¥
//--------------------------------------------------------------------
*/
void ENGINE::SetBlendNone()
{
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ZERO );
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
}
/*
//--------------------------------------------------------------------
// Name: SetBlendOne()
// Desc: Jednoducha priesvitnost
//--------------------------------------------------------------------
*/
void ENGINE::SetBlendOne()
{
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE );
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
}
/*
//--------------------------------------------------------------------
// Name: SetBlendTrans()
// Desc: Jedna farba bude transparentna, zalezi na texture aka
//--------------------------------------------------------------------
*/
void ENGINE::SetBlendTrans()
{
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA );
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
}
/*
//--------------------------------------------------------------------
// Name: SetBlendCustom()
// Desc: Umo₧nuje si nastavi¥ vlastnu transparentnos¥ podla D3D
//--------------------------------------------------------------------
D3D TYPY ktore mozno pouzit
D3DBLEND_ZERO = 1,
D3DBLEND_ONE = 2,
D3DBLEND_SRCCOLOR = 3,
D3DBLEND_INVSRCCOLOR = 4,
D3DBLEND_SRCALPHA = 5,
D3DBLEND_INVSRCALPHA = 6,
D3DBLEND_DESTALPHA = 7,
D3DBLEND_INVDESTALPHA = 8,
D3DBLEND_DESTCOLOR = 9,
D3DBLEND_INVDESTCOLOR = 10,
D3DBLEND_SRCALPHASAT = 11,
D3DBLEND_BOTHSRCALPHA = 12,
D3DBLEND_BOTHINVSRCALPHA = 13,
*/
void ENGINE::SetBlendCustom(_D3DBLEND Scr,_D3DBLEND Dest)
{
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,Scr );
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,Dest);
}
/*
//--------------------------------------------------------------------
// Name: SetCullMode()
// Desc: Nastavi orezavanie (prednΘ,zadne, ziadne)
//--------------------------------------------------------------------
D3DCULL_NONE = 1,
D3DCULL_CW = 2,
D3DCULL_CCW = 3,
*/
void ENGINE::SetCullMode(_D3DCULL Cull)
{
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, Cull );
}
/*
//--------------------------------------------------------------------
// Name: SetFillMode()
// Desc: Nastavi mod naplnovania
//--------------------------------------------------------------------
D3DFILL_POINT = 1,
D3DFILL_WIREFRAME = 2,
D3DFILL_SOLID = 3,
*/
void ENGINE::SetFillMode(_D3DFILLMODE Fill)
{
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE ,Fill);
}
//--------------------------------------------------------------------
// Name: SetFilter()
// Desc: Nastavi filtrovanie textur
//--------------------------------------------------------------------
/* D3DTEXF_NONE = 0, // filtering disabled (valid for mip filter only)
D3DTEXF_POINT = 1, // nearest
D3DTEXF_LINEAR = 2, // linear interpolation
D3DTEXF_ANISOTROPIC = 3, // anisotropic
D3DTEXF_PYRAMIDALQUAD = 6, // 4-sample tent
D3DTEXF_GAUSSIANQUAD = 7, // 4-sample gaussian
*/
void ENGINE::SetFilter(int Stage, _D3DTEXTUREFILTERTYPE Filter)
{
g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MINFILTER, Filter);
g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MAGFILTER, Filter);
}
/*
//--------------------------------------------------------------------
// Name: SetShadeMode()
// Desc: Nastavi shading polygonov
//--------------------------------------------------------------------
D3DSHADE_FLAT = 1,
D3DSHADE_GOURAUD = 2,
D3DSHADE_PHONG = 3,
*/
void ENGINE::SetShadeMode(_D3DSHADEMODE Shade)
{
g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE ,Shade);
}
/*
//--------------------------------------------------------------------
// Name: SetZBuffer()
// Desc: zapne - vypne ZBuffer
//--------------------------------------------------------------------
*/
void ENGINE::SetZBuffer(bool Activate)
{
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, Activate);
}
/*
//--------------------------------------------------------------------
// Name: SetMipMapping()
// Desc: zapne - vypne MipMapping, Levels - kolko mipmaplevelov vytvara¥
//--------------------------------------------------------------------
*/
void ENGINE::SetMipMapping(int Stage,bool Activate)
{
if (Activate == true)
{
g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
else
{
g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
}
}
/*
//--------------------------------------------------------------------
// Name: SetProjection
// Desc: nastavi projekcnu maticu
//--------------------------------------------------------------------
*/
void ENGINE::SetProjection()
{
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/3.6f, 1.0f,Min, Max);
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
/*
//--------------------------------------------------------------------
// Name: SetLighting()
// Desc: Vypne zapne osvetlenie
//--------------------------------------------------------------------
*/
void ENGINE::SetLighting(bool Activate)
{
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING,Activate);
}
/*
//--------------------------------------------------------------------
// Name: SetFog()nastavi hmlu
// Desc: Vypne zapne osvetlenie
//--------------------------------------------------------------------
*/
void ENGINE::SetupFog(COLOR Color, float Destiny)
{
g_pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, D3DXCOLOR(Color.R,Color.G,Color.B,Color.A ));
g_pd3dDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_EXP2);
g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, FtoDW(Destiny));
}
/*
//--------------------------------------------------------------------
// Name: SetFogActive(()
// Desc: Vypne zapne hmli
//--------------------------------------------------------------------
*/
void ENGINE::SetFog(bool Activate)
{
g_pd3dDevice->SetRenderState(D3DRS_FOGENABLE, Activate);
}
/*
//--------------------------------------------------------------------
// Name: SetSpecular()
// Desc: Vypne zapne specular svetla
//--------------------------------------------------------------------
*/
void ENGINE::SetSpecular(bool Activate)
{
g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE,Activate);
}
/*
//--------------------------------------------------------------------
// Name: SetAlphaTest()
// Desc: Vypne alpha test, umo₧ni kresli¥ transparentne objekty nakoniec
//--------------------------------------------------------------------
*/
void ENGINE::SetAlphaTest(bool Activate)
{
g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE,Activate);
g_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, Activate);
g_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
}
/*
//--------------------------------------------------------------------
// Name: SetZwrite()
// Desc: Vypne alebo zapne zapisovanie do hlbkoveho bufferu
//--------------------------------------------------------------------
*/
void ENGINE::SetZwrite(bool Activate)
{
g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,Activate);
}
/*
//--------------------------------------------------------------------
// Name: SetStencil()
// Desc: Vypne alebo zapne stencil
//--------------------------------------------------------------------
*/
void ENGINE::SetStencil(bool Activate)
{
g_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,Activate );
}
/*
//--------------------------------------------------------------------
// Name: SetSpecular()
// Desc: zresetuje nastavenia svetiel a priesvitnosti
//--------------------------------------------------------------------
*/
void ENGINE::ResetToDefault()
{
Engine.SetLighting(false);
Engine.SetSpecular(false);
Engine.SetZBuffer(true);
Engine.SetZwrite(true);
Engine.SetStencil(false);
Engine.SetAlphaTest(false);
Engine.SetCullMode(D3DCULL_NONE);
Engine.SetShadeMode(D3DSHADE_GOURAUD);
g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAARG2, D3DTA_CURRENT);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX,0);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAOP, D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAARG2, D3DTA_CURRENT);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX,1);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
}
/*
//--------------------------------------------------------------------
// Name: BeginScene()
// Desc: zacni vykreslovanie
//--------------------------------------------------------------------
*/
void ENGINE::BeginScene()
{
//zacniFPS
TimeStart();
//vytvori flagy pre vycistenie
DWORD flags = 0;
flags |= D3DCLEAR_TARGET;
flags |= D3DCLEAR_ZBUFFER;
if (StencilShadows)
flags |= D3DCLEAR_STENCIL;
//Vicistenie backbufferu
g_pd3dDevice->Clear( 0, NULL, flags, D3DXCOLOR(Engine.BackColor.R,Engine.BackColor.G,Engine.BackColor.B,1.0f), 1.0f, 0 );
//zacni scenu
g_pd3dDevice->BeginScene();
}
/*
//--------------------------------------------------------------------
// Name: EndScene()
// Desc: prezentuj scenu
//--------------------------------------------------------------------
*/
void ENGINE::EndScene()
{
//ukonci scenu
g_pd3dDevice->EndScene();
//Zbraz na Obrazovku
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
//skonciFPS
TimeEnd();
}