home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
main.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-11-27
|
20KB
|
815 lines
#include "main.h"
//--------------------------
//Globals
//--------------------------
DXFONT DFont;
MODEL SkyBoxLeft;
MODEL SkyBoxRight;
MODEL SkyBoxFront;
MODEL SkyBoxBack;
MODEL SkyBoxTop;
SOUND *MenuMusic;
SOUND *GameMusic;
float MenuMusicTime = -1.0f;
float GameMusicTime = -1.0f;
//--------------------------
//Funkcie
//--------------------------
void InitializeScene();
void SetProjectionScene();
void SetProjectionSkyBox();
void RenderLensFlares();
PANEL2D Flare;
//--------------------------
//HiScore tab - MD5 hash
//--------------------------
int TopScore = 0;
void WriteScore();
char* MD5String(char* szString);
int writeHighScoreFile(char* randomNumber, char* gameCode, char* score);
bool compareHash(char* hash, char* randomNumber, char* gameCode, char* score);
void hashData(char* randomNumber, char* gameCode, char* score, char* finalHash);
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
//begin
Engine.BeginScene();
//ovladanie
Input.RefreshKeyboard();
Input.RefreshMouse();
Input.RefreshJoystick();
if (Input.Mouse.X > Engine.Width)
SetCursorPos((int) Engine.Width,(int)Input.Mouse.Y);
else if (Input.Mouse.Y > Engine.Height)
SetCursorPos((int)Input.Mouse.X,(int) Engine.Height);
else if (Input.Mouse.X < 0)
SetCursorPos( 0,(int)Input.Mouse.Y);
else if (Input.Mouse.Y < 0)
SetCursorPos((int)Input.Mouse.X,0);
//obnov svetla
LightManager.RefreshLights();
////////
//Menu//
////////
if (Menu.EnabledMenu == true)
{
//hudba
if (GameMusicTime != -1.0f && MusicEnabled)
{
GameMusic->Stop();
GameMusicTime = -1.0f;
}
if (MenuMusicTime == -1.0f && MusicEnabled)
{
MenuMusicTime = 0.0f;
}
if (MenuMusicTime == 0.0f && MusicEnabled)
{
MenuMusic->Play(true);
}
MenuMusicTime += PowerTime(0.01f);
if (MenuMusicTime > 295.0f && MusicEnabled)
{
MenuMusicTime = 0.0f;
}
//skry kurzor
ShowCursor(false);
//obnovi menu
Menu.Refresh();
}
/////////////
//Briefring//
/////////////
float zFPS = FPS; //pre casovanie hudby
if (Menu.EnabledBriefring == true)
{
//zvysi FPS - zastavi hru
FPS = 160000.0f;
}
////////
//Svet//
////////
if (Menu.EnabledMenu == false)
{
//hudba
if (MenuMusicTime != -1.0f && MusicEnabled )
{
MenuMusic->Stop();
MenuMusicTime = -1.0f;
}
if (GameMusicTime == -1.0f && MusicEnabled)
{
GameMusicTime = 0.0f;
}
if (GameMusicTime == 0.0f && MusicEnabled)
{
GameMusic->Play(true);
}
GameMusicTime += (1000.0f/zFPS) * 0.01f;
if (GameMusicTime > 480.0f && MusicEnabled)
{
GameMusicTime = 0.0f;
}
//Pridavanie objektov
Level.ProccesLevel();
//spitfire
SpitFire.Refresh();
//nastavi projekcnu maticu pre skybox
SetProjectionSkyBox();
//skybox
SkyBoxLeft.Pos = Camera.Pos;
SkyBoxLeft.Pos.Y = Camera.Pos.Y-200.0f;
SkyBoxLeft.Render();
SkyBoxRight.Pos = Camera.Pos;
SkyBoxRight.Pos.Y = Camera.Pos.Y-200.0f;
SkyBoxRight.Render();
SkyBoxFront.Pos = Camera.Pos;
SkyBoxFront.Pos.Y = Camera.Pos.Y-200.0f;
SkyBoxFront.Render();
SkyBoxBack.Pos = Camera.Pos;
SkyBoxBack.Pos.Y = Camera.Pos.Y-200.0f;
SkyBoxBack.Render();
SkyBoxTop.Pos = Camera.Pos;
SkyBoxTop.Pos.Y = Camera.Pos.Y-200.0f;
SkyBoxTop.Render();
//nastavi projektcnu maticiu pre scenu
SetProjectionScene();
//krajina
Level.Refresh();
//renderspitfire
SpitFire.Render1();
//explozie
Explo.Render();
//renderspitfire - particles
SpitFire.Render2();
//vyrenderuje flare efekty
RenderLensFlares();
//vyrenderuj skore napisy
Score.Refresh();
//vyrenderuje gui
Menu.DrawGui();
//vyrenderuje mapu
Menu.DrawMap();
//oznamy
SpitFire.DrawScreens();
//skry kurzor
ShowCursor(false);
//debug font
if (SpitFire.ShowFPS)
{
char cBuf[80];
sprintf(cBuf,"FPS: %d",iFPS);
DFont.Print(20,20,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
}
}
/////////////
//Briefring//
/////////////
if (Menu.EnabledBriefring == true)
{
//zobrazi briefring
Menu.RefreshBriefring();
}
//!!Tento problem vystava iba pri nastaveni grafickej karty
//!!na slaby vykon, alebo je to tiez dobre kedze na zaciatku
//!!sa nacitava briefing
//!!Hack, zoberie silu
float Power;
Power = PowerTime(0.001f);
//end
Engine.EndScene();
//!!Hack ak stagetime == 0.0f+power FPS zvysit!!
if (Level.StageTime == 0.0f+Power && Menu.EnabledMenu == false)
FPS = 120.0f;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WinClassName, NULL };
RegisterClassEx( &wc );
//zaciatok logu
LogStart();
//info o programe
LogPrint(ProgramInfo);
///////////////////////
//PRECITAJ NASTAVENIA//
///////////////////////
ReadConfigData();
////////////////////////
//MENITELNE NASTAVENIA//
////////////////////////
//SetUp Resotution
if (Resolution == 0)
{
Engine.Width = 640;
Engine.Height = 480;
}
if (Resolution == 1)
{
Engine.Width = 800;
Engine.Height = 600;
}
if (Resolution == 2)
{
Engine.Width = 1024;
Engine.Height = 768;
}
//SetUp Engine
Engine.Windowed = false;
Engine.Format = D3DFMT_R5G6B5;
Engine.TextureFormat = D3DFMT_A8R8G8B8;
Engine.TextureCreatingFlag = D3DPOOL_DEFAULT;
Engine.Min = 10.0f;
Engine.Max = (float)DrawDistance;
Engine.BackColor = GetColor(1.0f,1.0f,1.0f,1.0f);
//nastav frustrum culling camery
Camera.MaxDis = (float)DrawDistance;
//stencil tiene
if (Shadows == 0)
Engine.StencilShadows = false;
else if (Shadows == 1)
Engine.StencilShadows = true;
////////////////////////////
//nastav okno pre windowed//
////////////////////////////
if(Engine.Windowed == true)
{
hWnd = CreateWindow( WinClassName, WinClassTittle,
WS_OVERLAPPEDWINDOW,
100, 100,Engine.Width, Engine.Height,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
}
//////////////////////////////
//nastav okno pre fullscreen//
//////////////////////////////
if(Engine.Windowed == false)
{
hWnd = CreateWindowEx( WS_EX_APPWINDOW, // Extended Style For The Window
WinClassName, // Class Name
WinClassTittle, // Window Title
WS_POPUP | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
Engine.Width, // Calculate Window Width
Engine.Height, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
wc.hInstance, // Instance
NULL); // Dont Pass Anything To WM_CREATE
}
hIns = hInst;
////////////////////////////////////
//Nastavenia ktore sa nedaju menit//
////////////////////////////////////
//Initialize engine
Engine.Initialize();
Engine.SetZBuffer(true);
Engine.SetFilter(0,D3DTEXF_LINEAR);
Engine.SetFilter(1,D3DTEXF_LINEAR);
Engine.SetCullMode(D3DCULL_NONE);
Engine.SetShadeMode(D3DSHADE_GOURAUD);
Engine.SetMipMapping(0,true);
Engine.SetMipMapping(1,true);
Engine.MipMapLevels = 0;
//directInput
Input.Initialize();
//zvuk
InitializeSound();
//zobrazi okno
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//vstup do Message slucky
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
//inicializacia svetiel pre lightmanager
LightManager.Initialize(1);
//debug font
DFont.Create(15,FW_BOLD,"Arial");
//editor
Editor.Initialize();
///////////////////////
//Inicializacia Sceny//
///////////////////////
InitializeScene();
////////////////
//message loop//
////////////////
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();
//Editor.RefreshEditor();
}
}
/////////////////
//Destroy sceny//
/////////////////
//znic level
Level.Destroy();
//vymaze svetla
LightManager.CleanUp();
//ukonci direct Input
Input.CleanUp();
//Hudba
if (MusicEnabled)
{
delete [] MenuMusic;
delete [] GameMusic;
}
//ukonci zvuk
CleanUpSound();
//Ukonci Engine
Engine.CleanUp();
//zapise score
WriteScore();
//znici okno
UnregisterClass( WinClassName, wc.hInstance );
return 0;
}
//-----------------------------------------------------------------------------
// Name: InitializeScene()
// Desc: nastavy krajinu, svetla, ..
//-----------------------------------------------------------------------------
void InitializeScene()
{
//nastav menu
//---------------
Menu.Initialize();
//skore
//-----
Score.Initialize();
//Svetla
//------
LightManager.AddLightDir(Get3D(-1.5f,-1.0f,-1.5f),GetColor(1.6f,1.6f,1.6f,1.6f),
GetColor(0.3f,0.3f,0.3f,0.3f),
GetColor(1.0f,1.0f,1.0f,1.0f));
//Hmla
//----
g_pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, 0xffffffff );
g_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
g_pd3dDevice->SetRenderState(D3DRS_FOGSTART, FtoDW((float)DrawDistance*0.50f));
g_pd3dDevice->SetRenderState(D3DRS_FOGEND, FtoDW((float)DrawDistance*0.75f));
//skyBox
//--------
Menu.DrawLoading(1,"Loading SkyBox");
SkyBoxLeft.FrustrumTest = false;
SkyBoxLeft.Lighting = false;
SkyBoxLeft.InitializeAse(1,"scene/skyboxleft.ase");
SkyBoxLeft.LoadAse(0,"scene/skyboxleft.ase");
SkyBoxLeft.LoadTexture("scene/sleft.jpg",GetColor(0.0f,0.0f,0.0f));
SkyBoxLeft.Sca = Get3D(250.0f,200.0f,250.0f);
SkyBoxRight.FrustrumTest = false;
SkyBoxRight.Lighting = false;
SkyBoxRight.InitializeAse(1,"scene/skyboxRight.ase");
SkyBoxRight.LoadAse(0,"scene/skyboxRight.ase");
SkyBoxRight.LoadTexture("scene/sRight.jpg",GetColor(0.0f,0.0f,0.0f));
SkyBoxRight.Sca = Get3D(250.0f,200.0f,250.0f);
SkyBoxFront.FrustrumTest = false;
SkyBoxFront.Lighting = false;
SkyBoxFront.InitializeAse(1,"scene/skyboxFront.ase");
SkyBoxFront.LoadAse(0,"scene/skyboxFront.ase");
SkyBoxFront.LoadTexture("scene/sFront.jpg",GetColor(0.0f,0.0f,0.0f));
SkyBoxFront.Sca = Get3D(250.0f,200.0f,250.0f);
SkyBoxBack.FrustrumTest = false;
SkyBoxBack.Lighting = false;
SkyBoxBack.InitializeAse(1,"scene/skyboxBack.ase");
SkyBoxBack.LoadAse(0,"scene/skyboxBack.ase");
SkyBoxBack.LoadTexture("scene/sBack.jpg",GetColor(0.0f,0.0f,0.0f));
SkyBoxBack.Sca = Get3D(250.0f,200.0f,250.0f);
SkyBoxTop.FrustrumTest = false;
SkyBoxTop.Lighting = false;
SkyBoxTop.InitializeAse(1,"scene/skyboxTop.ase");
SkyBoxTop.LoadAse(0,"scene/skyboxTop.ase");
SkyBoxTop.LoadTexture("scene/sTop.jpg",GetColor(0.0f,0.0f,0.0f));
SkyBoxTop.Sca = Get3D(250.0f,200.0f,250.0f);
Flare.Create(128,128,4);
Flare.RelativeMode = true;
Flare.AddFrame("scene/lens1.jpg",GetColor(1,0,0,0));
Flare.AddFrame("scene/lens2.jpg",GetColor(1,0,0,0));
Flare.AddFrame("scene/lens3.jpg",GetColor(1,0,0,0));
Flare.AddFrame("scene/lens4.jpg",GetColor(1,0,0,0));
//stencil
//------------
Menu.DrawLoading(1,"Initalizing StencilShadow");
StencilShadow.Initialize();
//modely
//------
Menu.DrawLoading(2,"Loading Models");
ModelLib.Initialize();
//zvuky
//-----
Menu.DrawLoading(4,"Loading Sounds");
SoundLib.Initialize();
//SpitFire
//--------------
Menu.DrawLoading(5,"Loading SpitFire");
SpitFire.Initialize();
//svet
//-----
Menu.DrawLoading(6,"Initializing World");
Level.Initialize();
//Particles
//------------
Menu.DrawLoading(8,"Initializing Particle Systems");
Explo.Initialize();
//Hudba
//---------
if (MusicEnabled == 1)
{
MenuMusic = new SOUND;
GameMusic = new SOUND;
MenuMusic->InitialiseForMP3();
MenuMusic->LoadSound("sounds/menumusic.mp3");
GameMusic->InitialiseForMP3();
GameMusic->LoadSound("sounds/gamemusic.mp3");
}
}
//-----------------------------------------------------------------------------
// Name: SetProjectionScene()
// Desc: nastavi projekcnu maticu pre renderovanie sceny
//-----------------------------------------------------------------------------
void SetProjectionScene()
{
D3DXMatrixPerspectiveFovLH( &matProj, 4.0f/3.0f,1.0f,Engine.Min, Engine.Max);
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: SetProjectionSkyBox()
// Desc: nastavi projekcnu maticu pre renderovanie skyboxu
//-----------------------------------------------------------------------------
void SetProjectionSkyBox()
{
D3DXMatrixPerspectiveFovLH( &matProj, 4.0f/3.0f, 1.0f,Engine.Min, 25000);
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: RenderLensFlares()
// Desc: renderuje lens flare efekty
//-----------------------------------------------------------------------------
void RenderLensFlares()
{
VECTOR3D SunPosition;
SunPosition.X = Camera.Pos.X + 1000.0f;
SunPosition.Y = Camera.Pos.Y + 575.0f;
SunPosition.Z = Camera.Pos.Z + 1000.0f;
VECTOR3D Sun = Project(SunPosition);
if (Sun.X == -1000.0f || Sun.Y == -1000.0f)
return;
Sun.X = (800.0f/Engine.Width)*Sun.X;
Sun.Y = (600.0f/Engine.Height)*Sun.Y;
VECTOR3D Center = Get3D(400.0f,300.0f,0.0f);
VECTOR3D Vector;
Vector.X = Sun.X-Center.X;
Vector.Y = Sun.Y-Center.Y;
Vector.Z = Sun.Z-Center.Z;
float Length = Absolute(Vector);
Normalize(&Vector);
Engine.SetBlendCustom(D3DBLEND_SRCCOLOR,D3DBLEND_ONE);
Flare.Frame = 0.0f;
Flare.Pos.X = Center.X + Vector.X*(Length/1.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(Length/1.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 2.0f;
Flare.Render();
Flare.Render();
Flare.Frame = 1.0f;
Flare.Pos.X = Center.X + Vector.X*(Length/2.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(Length/2.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.5f;
Flare.Render();
Flare.Render();
Flare.Frame = 0.0f;
Flare.Pos.X = Center.X + Vector.X*(Length/3.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(Length/3.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.25f;
Flare.Render();
Flare.Render();
Flare.Frame = 2.0f;
Flare.Pos.X = Center.X + Vector.X*(Length/8.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(Length/8.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.7f;
Flare.Render();
Flare.Render();
Flare.Frame = 3.0f;
Flare.Pos.X = Center.X + Vector.X*(-Length/2.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(-Length/2.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.5f;
Flare.Render();
Flare.Render();
Flare.Frame = 2.0f;
Flare.Pos.X = Center.X + Vector.X*(-Length/4.0f);
Flare.Pos.Y = Center.Y + Vector.Y*(-Length/4.0f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.45f;
Flare.Render();
Flare.Render();
Flare.Frame = 1.0f;
Flare.Pos.X = Center.X + Vector.X*(-Length/5.5f);
Flare.Pos.Y = Center.Y + Vector.Y*(-Length/5.5f);
Flare.Pos.Z = 0.0f;
Flare.Scale = 0.35f;
Flare.Render();
Flare.Render();
Engine.SetBlendNone();
}
//=========================================================
//
//
// MD5 HASH
//=========================================================
void WriteScore()
{
//------------------
// Nacitaj score
//------------------
int iInScore;
char InScore[80];
char InRandom[80];
char InHash[80];
FILE *FileIn = NULL;
FileIn = fopen("models/structures/tank3destroy.bmp","r");
if (FileIn == NULL)
LogPrint("Nemozem otvorit score subor");
fscanf(FileIn,"%s %s %s",InScore,InRandom,InHash);
sscanf(InScore,"%d",&iInScore);
//zisti pravdivost hashu
if (compareHash(InHash,InRandom,"PlaneArcade",InScore) == false
&& iInScore != 0)
{
LogPrint("Chybn² hash");
return;
}
fclose(FileIn);
//porovnaj nahrate score s nacitanym score
if (TopScore <= iInScore)
return;
//vygeneruj hash
char OutScore[80];
char OutRandom[80];
char OutHash[80];
sprintf(OutScore,"%d",TopScore);
sprintf(OutRandom,"%d",rand());
//zapise do suboru nove hodnoty
hashData(OutRandom,"PlaneArcade",OutScore,OutHash);
FILE *OutFile;
OutFile = fopen("models/structures/tank3destroy.bmp","w");
fprintf(OutFile,"%s %s %s",OutScore,OutRandom,OutHash);
fclose(OutFile);
//vygeneruje htm subor
writeHighScoreFile(OutRandom,"PlaneArcade",OutScore);
}
// funkce vracφ °et∞zec finalHash kter² obsahuje v²skledn² hash, ten se dß pak porovnßvat spoleΦn∞ s
// hodnotami hry (skore, kod hry) a funkce compareHash jednoznaΦn∞ °ekne jestli jsou hodnoty pravdivnΘ
void hashData(char* randomNumber, char* gameCode, char* score, char* finalHash)
{
char randomHash[33]; // Nßhodn² "mezi-hash" vygenerovan² z nßhodnΘho Φφsla a n∞jak²ch znak∙...
char outputTemp[255]; // V²sledn² MD5 hash vÜech dat
// Vygeneruje nahodil² "mezi-hash" kter² nßm zv∞tÜφ nßhodu a taky ud∞lß proces prolomenφ slo₧it∞jÜφm
sprintf(randomHash, "%s", MD5String(randomNumber));
// Do doΦasnΘ prom∞nnΘ za sebe nasklßdßme nßhodn² hash, k≤d hry(tabulky), skore a jmeno hrace
sprintf(outputTemp, "3df32%s21fds1%sF5A4f%sAd_5:", randomHash, gameCode, score);
// Hashneme tyto udaje a dostaneme tak jedinecny kod, kter² podobne
// vygenerujeme na serveru a porovname s poslanymi udaji a hashem
sprintf(finalHash, "%s", MD5String(outputTemp));
}
bool compareHash(char* hash, char* randomNumber, char* gameCode, char* score)
{
char compareHash[33]; // V²sledn² MD5 hash vÜech dat
hashData(randomNumber, gameCode, score, compareHash);
if(strcmp(hash, compareHash) == 0)
return true;
return false;
}
int writeHighScoreFile(char* randomNumber, char* gameCode, char* score)
{
char outputHash[33]; // V²sledn² MD5 hash vÜech dat
char outputTemp[255]; // V²sledn² MD5 hash vÜech dat
// Hashneme tyto udaje a dostaneme tak jedinecny kod, kter² podobne
// vygenerujeme na serveru a porovname s poslanymi udaji a hashem
hashData(randomNumber, gameCode, score, outputHash);
// V²pis ladφcφch informacφ do konzole
//printf("RAND#: %s\nRANDhash: %s\nScore: %s\nFinalHash: %s\n", randomNumber, randomHash, score, outputHash);
sprintf(outputTemp, "http://3dgamesdev.com/uploadscore/write.php?action=%s&game=%s&id=%s&score=%s&version=%s", outputHash, gameCode, randomNumber, score, ProgramVersion);
// ZapφÜeme vÜe do URL dotazu do HTML souboru
FILE* HTMLout = fopen("upload score.htm", "w");
fprintf(HTMLout, "<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"><title>Upload score</title></head><body><h2>VyΦkejte prosφm</h2><h3>Nahrßvßm score...</h3></body></html>", outputTemp);
fclose(HTMLout);
return 0;
}