home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
editor.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-07-03
|
8KB
|
349 lines
#include "main.h"
//----------------
//Global
//----------------
EDITOR Editor;
//konstruktor
//-------------------------------------------------------------
void EDITOR::Initialize()
{
//budovy
for (int i=0;i<Editor_MaxStructures;i++)
{
Buffer[i].Active = false;
Buffer[i].Pos = Get3D(0.0f,0.0f,0.0f);
Buffer[i].Rot = Get3D(0.0f,0.0f,0.0f);
Buffer[i].ModelIndex = 0;
Buffer[i].Mode = 0;
}
//kurzor
ActStructure = 0;
Cursor.Active = true;
Cursor.Pos = Get3D(0.0f,0.0f,0.0f);
Cursor.Rot = Get3D(0.0f,0.0f,0.0f);
Cursor.ModelIndex = 0;
Cursor.Mode = 0;
//font
Font.Create(15,FW_BOLD,"Arial");
//camera
CameraDistance = 1000.0f;
CameraRotation = 0.0f;
}
//Refresh celeho editoru
//--------------------------------------------------------------
void EDITOR::RefreshEditor()
{
//begin
Engine.BeginScene();
//ovladanie
Input.RefreshKeyboard();
Input.RefreshMouse();
//obnov svetla
LightManager.RefreshLights();
//-----------
Ovladanie();
SetCamera();
Render();
//-----------
//end
Engine.EndScene();
}
//Set camera
//--------------------------------------------------------------
void EDITOR::SetCamera()
{
//projekcia
D3DXMatrixPerspectiveFovLH( &matProj, 4.0f/3.0f , 1.0f,Engine.Min, Engine.Max);
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
//camera
Camera.Pos.X = Cursor.Pos.X + sinf(CameraRotation)*CameraDistance;
Camera.Pos.Y = Cursor.Pos.Y + CameraDistance;
Camera.Pos.Z = Cursor.Pos.Z - cosf(CameraRotation)*CameraDistance;
Camera.Rot.X = -0.85f;
Camera.Rot.Y = -CameraRotation;
Camera.RefreshYawPitchRoll();
}
//Ovladanie
//--------------------------------------------------------------
void EDITOR::Ovladanie()
{
//load
if (Input.KeyPRESS[DIK_NUMPAD1]) LoadScene("saves/1.scf");
if (Input.KeyPRESS[DIK_NUMPAD2]) LoadScene("saves/2.scf");
if (Input.KeyPRESS[DIK_NUMPAD3]) LoadScene("saves/3.scf");
//Save
if (Input.KeyPRESS[DIK_NUMPAD4]) SaveScene("saves/1.scf");
if (Input.KeyPRESS[DIK_NUMPAD5]) SaveScene("saves/2.scf");
if (Input.KeyPRESS[DIK_NUMPAD6]) SaveScene("saves/3.scf");
//export
if (Input.KeyPRESS[DIK_NUMPAD7]) ExportScene("saves/export1.txt");
if (Input.KeyPRESS[DIK_NUMPAD8]) ExportScene("saves/export2.txt");
if (Input.KeyPRESS[DIK_NUMPAD9]) ExportScene("saves/export3.txt");
//priblizovanie
if (Input.KeyDOWN[DIK_RSHIFT])
CameraDistance -= Power(20.0f);
if (Input.KeyDOWN[DIK_RCONTROL])
CameraDistance += Power(20.0f);
//camera rotation
if (Input.KeyDOWN[DIK_N])
CameraRotation -= Power(0.05f);
if (Input.KeyDOWN[DIK_M])
CameraRotation += Power(0.05f);
//urcenie bodu
VECTOR3D P1,P2;
P1 = Get3D(Cursor.Pos.X, 1000.0f,Cursor.Pos.Z);
P2 = Get3D(Cursor.Pos.X,-1000.0f,Cursor.Pos.Z);
Level.Krajina.ColliseDistance(P1,P2);
Cursor.Pos = Level.Krajina.IntPos;
//pohyb
if (Input.KeyDOWN[DIK_LEFT])
{
Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation-1.57f);
Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation-1.57f);
}
if (Input.KeyDOWN[DIK_RIGHT])
{
Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation+1.57f);
Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation+1.57f);
}
if (Input.KeyDOWN[DIK_UP])
{
Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation);
Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation);
}
if (Input.KeyDOWN[DIK_DOWN])
{
Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation-3.14f);
Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation-3.14f);
}
//rotacia
if (Input.KeyDOWN[DIK_K])
Cursor.Rot.Y += Power(0.05f);
if (Input.KeyDOWN[DIK_L])
Cursor.Rot.Y -= Power(0.05f);
//modelindex
if (Input.KeyPRESS[DIK_O])
Cursor.ModelIndex--;
if (Input.KeyPRESS[DIK_P])
Cursor.ModelIndex++;
if (Cursor.ModelIndex == -1) Cursor.ModelIndex = Editor_MaxTypes-1;
if (Cursor.ModelIndex == Editor_MaxTypes) Cursor.ModelIndex = 0;
//structure index
if (Input.KeyPRESS[DIK_U])
{
ActStructure--;
if (Buffer[ActStructure].Active)
Cursor = Buffer[ActStructure];
}
if (Input.KeyPRESS[DIK_I])
{
ActStructure++;
if (Buffer[ActStructure].Active)
Cursor = Buffer[ActStructure];
}
//delete structure
if (ActStructure > 0 && Input.KeyPRESS[DIK_DELETE])
{
Buffer[ActStructure].Active = false;
ActStructure--;
Cursor = Buffer[ActStructure];
}
//mode
if (Input.KeyDOWN[DIK_Q]) Cursor.Mode = 0;
if (Input.KeyDOWN[DIK_W]) Cursor.Mode = 1;
if (Input.KeyDOWN[DIK_E]) Cursor.Mode = 2;
//mod krajiny
if (Input.KeyDOWN[DIK_X]) Level.Krajina.Mode = 0;
if (Input.KeyDOWN[DIK_C]) Level.Krajina.Mode = 1;
//budova
Buffer[ActStructure] = Cursor;
}
//Render
//--------------------------------------------------------------
void EDITOR::Render()
{
//render krajiny
//----------------
Level.Krajina.Render();
//render ciari
//-----------------
DebugDrawLine(Cursor.Pos,Get3D(Cursor.Pos.X,
Cursor.Pos.Y + 350.0f,
Cursor.Pos.Z));
//render aktivnych budov
//-----------------------
MODEL *Model;
for (int i=0;i<Editor_MaxStructures;i++)
{
if (Buffer[i].Active == false)
continue;
//ak je strom zapni blending
if (Buffer[i].ModelIndex >= Index_Structure_Strom1)
{
Engine.SetBlendTrans();
Engine.SetAlphaTest(true);
Engine.SetFilter(0,D3DTEXF_NONE);
}
//ziskanie modelu
Model = ModelLib.GetStructure(Buffer[i].ModelIndex);
//render
Model->Pos = Buffer[i].Pos;
Model->Rot = Buffer[i].Rot;
Model->Render();
//ak je strom zapni blending
if (Buffer[i].ModelIndex >= Index_Structure_Strom1)
{
Engine.SetBlendTrans();
Engine.SetAlphaTest(false);
Engine.SetFilter(0,D3DTEXF_LINEAR);
}
}
char cBuf[80];
//
//-----------------------
if (Input.KeyDOWN[DIK_B])
{
VECTOR3D FPos;
for (int i=0;i<Editor_MaxStructures;i++)
{
if (Buffer[i].Active == false)
continue;
FPos = Project(Buffer[i].Pos);
sprintf(cBuf,"%d",i);
Font.Print((int)FPos.X,(int)FPos.Y,GetColor(1.0f,1.0f,1.0f,0.0f),cBuf);
}
}
//debug
//----------
sprintf(cBuf,"POS: %f %f %f",Cursor.Pos.X,Cursor.Pos.Y,Cursor.Pos.Z);
Font.Print(20,20,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
sprintf(cBuf,"ROT: %f %f %f",Cursor.Rot.X,Cursor.Rot.Y,Cursor.Rot.Z);
Font.Print(20,40,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
sprintf(cBuf,"MODE: %d",Cursor.Mode);
Font.Print(20,60,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
sprintf(cBuf,"MODEL INDEX: %d",Cursor.ModelIndex );
Font.Print(20,80,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
sprintf(cBuf,"STRUCTURE INDEX: %d",ActStructure );
Font.Print(20,100,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
sprintf(cBuf,"CAMERA ROTATION: %f",CameraRotation);
Font.Print(20,120,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
}
//uloz
//----------------------------------------------------------
void EDITOR::SaveScene(char *FileName)
{
FILE *File = NULL;
File = fopen(FileName,"wb");
fwrite(Buffer,sizeof(E_STRUCTURE),Editor_MaxStructures,File);
fclose(File);
}
//nahraj
//-----------------------------------------------------------
void EDITOR::LoadScene(char *FileName)
{
FILE *File;
File = fopen(FileName,"rb");
fread(Buffer,sizeof(E_STRUCTURE),Editor_MaxStructures,File);
fclose(File);
ActStructure = 0;
Cursor = Buffer[0];
}
//export
//--------------------------------------------------------------
void EDITOR::ExportScene(char *FileName)
{
FILE *File;
File = fopen(FileName,"w");
for (int i=0;i<Editor_MaxStructures;i++)
{
if (Buffer[i].Active == false)
continue;
fprintf(File,"AddStructure( %d,%d,%d,",i,Buffer[i].ModelIndex,
Buffer[i].Mode);
fprintf(File,"Get3D(%.1ff,%.1ff,%.1ff),",Buffer[i].Pos.X,
0.0f,
Buffer[i].Pos.Z);
fprintf(File,"Get3D(%.1ff,%.1ff,%.1ff));",Buffer[i].Rot.X,
Buffer[i].Rot.Y,
Buffer[i].Rot.Z);
fprintf(File,"\n");
}
fclose(File);
}