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 >
C/C++ Source or Header  |  2004-07-03  |  8KB  |  349 lines

  1.  
  2. #include "main.h"
  3.  
  4. //----------------
  5. //Global
  6. //----------------
  7. EDITOR Editor;
  8.  
  9.  
  10. //konstruktor
  11. //-------------------------------------------------------------
  12. void EDITOR::Initialize()
  13. {
  14.  
  15.     //budovy
  16.     for (int i=0;i<Editor_MaxStructures;i++)
  17.     {
  18.         Buffer[i].Active = false;
  19.         Buffer[i].Pos = Get3D(0.0f,0.0f,0.0f);
  20.         Buffer[i].Rot = Get3D(0.0f,0.0f,0.0f);
  21.         Buffer[i].ModelIndex = 0;
  22.         Buffer[i].Mode = 0;
  23.     }
  24.  
  25.     //kurzor
  26.     ActStructure = 0;
  27.     Cursor.Active = true;
  28.     Cursor.Pos = Get3D(0.0f,0.0f,0.0f);
  29.     Cursor.Rot = Get3D(0.0f,0.0f,0.0f);
  30.     Cursor.ModelIndex = 0;
  31.     Cursor.Mode = 0;
  32.     
  33.     //font
  34.     Font.Create(15,FW_BOLD,"Arial");
  35.  
  36.     //camera
  37.     CameraDistance = 1000.0f;
  38.     CameraRotation = 0.0f;
  39. }
  40.  
  41. //Refresh celeho editoru
  42. //--------------------------------------------------------------
  43. void EDITOR::RefreshEditor()
  44. {
  45.  
  46.     //begin
  47.     Engine.BeginScene();
  48.  
  49.     //ovladanie 
  50.     Input.RefreshKeyboard();
  51.     Input.RefreshMouse();
  52.  
  53.     //obnov svetla
  54.     LightManager.RefreshLights();
  55.  
  56.     //-----------
  57.  
  58.     Ovladanie();
  59.  
  60.     SetCamera();
  61.  
  62.     Render();
  63.  
  64.     //-----------
  65.  
  66.     //end
  67.     Engine.EndScene();
  68.  
  69.  
  70. }
  71.  
  72. //Set camera
  73. //--------------------------------------------------------------
  74. void EDITOR::SetCamera()
  75. {
  76.  
  77.     //projekcia
  78.     D3DXMatrixPerspectiveFovLH( &matProj, 4.0f/3.0f , 1.0f,Engine.Min, Engine.Max);
  79.     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  80.  
  81.     //camera
  82.     Camera.Pos.X = Cursor.Pos.X + sinf(CameraRotation)*CameraDistance;
  83.     Camera.Pos.Y = Cursor.Pos.Y + CameraDistance;
  84.     Camera.Pos.Z = Cursor.Pos.Z - cosf(CameraRotation)*CameraDistance;
  85.     Camera.Rot.X = -0.85f;
  86.     Camera.Rot.Y = -CameraRotation;
  87.     Camera.RefreshYawPitchRoll();
  88.  
  89. }
  90.  
  91.  
  92. //Ovladanie
  93. //--------------------------------------------------------------
  94. void EDITOR::Ovladanie()
  95. {
  96.  
  97.     //load 
  98.     if (Input.KeyPRESS[DIK_NUMPAD1]) LoadScene("saves/1.scf");
  99.     if (Input.KeyPRESS[DIK_NUMPAD2]) LoadScene("saves/2.scf");
  100.     if (Input.KeyPRESS[DIK_NUMPAD3]) LoadScene("saves/3.scf");
  101.  
  102.     //Save
  103.     if (Input.KeyPRESS[DIK_NUMPAD4]) SaveScene("saves/1.scf");
  104.     if (Input.KeyPRESS[DIK_NUMPAD5]) SaveScene("saves/2.scf");
  105.     if (Input.KeyPRESS[DIK_NUMPAD6]) SaveScene("saves/3.scf");
  106.  
  107.     //export
  108.     if (Input.KeyPRESS[DIK_NUMPAD7]) ExportScene("saves/export1.txt");
  109.     if (Input.KeyPRESS[DIK_NUMPAD8]) ExportScene("saves/export2.txt");
  110.     if (Input.KeyPRESS[DIK_NUMPAD9]) ExportScene("saves/export3.txt");
  111.  
  112.     //priblizovanie
  113.     if (Input.KeyDOWN[DIK_RSHIFT]) 
  114.         CameraDistance -= Power(20.0f);
  115.  
  116.     if (Input.KeyDOWN[DIK_RCONTROL]) 
  117.         CameraDistance += Power(20.0f);
  118.  
  119.     //camera rotation
  120.     if (Input.KeyDOWN[DIK_N]) 
  121.         CameraRotation -= Power(0.05f);
  122.     if (Input.KeyDOWN[DIK_M]) 
  123.         CameraRotation += Power(0.05f);
  124.  
  125.  
  126.     //urcenie bodu
  127.     VECTOR3D P1,P2;
  128.     P1 = Get3D(Cursor.Pos.X, 1000.0f,Cursor.Pos.Z);
  129.     P2 = Get3D(Cursor.Pos.X,-1000.0f,Cursor.Pos.Z);
  130.     Level.Krajina.ColliseDistance(P1,P2);
  131.     Cursor.Pos = Level.Krajina.IntPos;
  132.     
  133.     //pohyb
  134.     if (Input.KeyDOWN[DIK_LEFT]) 
  135.     {
  136.         Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation-1.57f);
  137.         Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation-1.57f);
  138.     }
  139.     if (Input.KeyDOWN[DIK_RIGHT]) 
  140.     {
  141.         Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation+1.57f);
  142.         Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation+1.57f);
  143.     }
  144.     if (Input.KeyDOWN[DIK_UP]) 
  145.     {
  146.         Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation);
  147.         Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation);
  148.     }
  149.     if (Input.KeyDOWN[DIK_DOWN]) 
  150.     {
  151.         Cursor.Pos.X += Power(15.0f)*sinf(-CameraRotation-3.14f);
  152.         Cursor.Pos.Z += Power(15.0f)*cosf(-CameraRotation-3.14f);
  153.     }
  154.  
  155.     //rotacia
  156.     if (Input.KeyDOWN[DIK_K]) 
  157.         Cursor.Rot.Y += Power(0.05f);
  158.     if (Input.KeyDOWN[DIK_L]) 
  159.         Cursor.Rot.Y -= Power(0.05f);
  160.  
  161.     //modelindex
  162.     if (Input.KeyPRESS[DIK_O]) 
  163.         Cursor.ModelIndex--;
  164.     if (Input.KeyPRESS[DIK_P]) 
  165.         Cursor.ModelIndex++;
  166.     if (Cursor.ModelIndex == -1) Cursor.ModelIndex = Editor_MaxTypes-1;
  167.     if (Cursor.ModelIndex == Editor_MaxTypes) Cursor.ModelIndex = 0;
  168.  
  169.     //structure index
  170.     if (Input.KeyPRESS[DIK_U]) 
  171.     {
  172.         ActStructure--;
  173.         if (Buffer[ActStructure].Active)
  174.             Cursor = Buffer[ActStructure];
  175.     }
  176.     if (Input.KeyPRESS[DIK_I]) 
  177.     {
  178.         ActStructure++;
  179.         if (Buffer[ActStructure].Active)
  180.             Cursor = Buffer[ActStructure];
  181.     }
  182.  
  183.     //delete structure
  184.     if (ActStructure > 0 && Input.KeyPRESS[DIK_DELETE])
  185.     {
  186.         Buffer[ActStructure].Active = false;
  187.         ActStructure--;
  188.         Cursor = Buffer[ActStructure];
  189.     }
  190.  
  191.     //mode
  192.     if (Input.KeyDOWN[DIK_Q]) Cursor.Mode = 0;
  193.     if (Input.KeyDOWN[DIK_W]) Cursor.Mode = 1;
  194.     if (Input.KeyDOWN[DIK_E]) Cursor.Mode = 2;
  195.  
  196.     //mod krajiny
  197.     if (Input.KeyDOWN[DIK_X]) Level.Krajina.Mode = 0;
  198.     if (Input.KeyDOWN[DIK_C]) Level.Krajina.Mode = 1;
  199.  
  200.     //budova
  201.     Buffer[ActStructure] = Cursor;
  202.  
  203. }
  204.  
  205. //Render
  206. //--------------------------------------------------------------
  207. void EDITOR::Render()
  208. {
  209.  
  210.     //render krajiny
  211.     //----------------
  212.     Level.Krajina.Render();
  213.  
  214.     //render ciari
  215.     //-----------------
  216.     DebugDrawLine(Cursor.Pos,Get3D(Cursor.Pos.X,
  217.                                    Cursor.Pos.Y + 350.0f,
  218.                                    Cursor.Pos.Z));
  219.  
  220.     //render aktivnych budov
  221.     //-----------------------
  222.     MODEL *Model;
  223.     for (int i=0;i<Editor_MaxStructures;i++)
  224.     {
  225.         if (Buffer[i].Active == false)
  226.             continue;
  227.  
  228.         //ak je strom zapni blending
  229.         if (Buffer[i].ModelIndex >= Index_Structure_Strom1)
  230.         {
  231.             Engine.SetBlendTrans();
  232.             Engine.SetAlphaTest(true);
  233.             Engine.SetFilter(0,D3DTEXF_NONE);
  234.         }
  235.  
  236.         //ziskanie modelu
  237.         Model = ModelLib.GetStructure(Buffer[i].ModelIndex);
  238.  
  239.         //render
  240.         Model->Pos = Buffer[i].Pos;
  241.         Model->Rot = Buffer[i].Rot;
  242.         Model->Render();
  243.  
  244.         //ak je strom zapni blending
  245.         if (Buffer[i].ModelIndex >= Index_Structure_Strom1)
  246.         {
  247.             Engine.SetBlendTrans();
  248.             Engine.SetAlphaTest(false);
  249.             Engine.SetFilter(0,D3DTEXF_LINEAR);
  250.         }
  251.     }
  252.  
  253.     char cBuf[80];
  254.  
  255.     //
  256.     //-----------------------
  257.     if (Input.KeyDOWN[DIK_B])
  258.     {
  259.         VECTOR3D FPos;
  260.         for (int i=0;i<Editor_MaxStructures;i++)
  261.         {
  262.             if (Buffer[i].Active == false)
  263.             continue;
  264.  
  265.             FPos = Project(Buffer[i].Pos);
  266.             sprintf(cBuf,"%d",i);
  267.             Font.Print((int)FPos.X,(int)FPos.Y,GetColor(1.0f,1.0f,1.0f,0.0f),cBuf);
  268.         }
  269.     }
  270.  
  271.     //debug
  272.     //----------
  273.     
  274.     sprintf(cBuf,"POS: %f %f %f",Cursor.Pos.X,Cursor.Pos.Y,Cursor.Pos.Z);
  275.     Font.Print(20,20,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  276.     sprintf(cBuf,"ROT: %f %f %f",Cursor.Rot.X,Cursor.Rot.Y,Cursor.Rot.Z);
  277.     Font.Print(20,40,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  278.     sprintf(cBuf,"MODE: %d",Cursor.Mode);
  279.     Font.Print(20,60,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  280.     sprintf(cBuf,"MODEL INDEX: %d",Cursor.ModelIndex );
  281.     Font.Print(20,80,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  282.     sprintf(cBuf,"STRUCTURE INDEX: %d",ActStructure );
  283.     Font.Print(20,100,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  284.     sprintf(cBuf,"CAMERA ROTATION: %f",CameraRotation);
  285.     Font.Print(20,120,GetColor(1.0f,1.0f,0.0f,0.0f),cBuf);
  286.  
  287. }
  288.  
  289. //uloz
  290. //----------------------------------------------------------
  291. void EDITOR::SaveScene(char *FileName)
  292. {
  293.  
  294.  
  295.     FILE *File = NULL;
  296.     File = fopen(FileName,"wb");
  297.  
  298.     fwrite(Buffer,sizeof(E_STRUCTURE),Editor_MaxStructures,File);
  299.  
  300.     fclose(File);
  301.  
  302. }
  303.  
  304. //nahraj
  305. //-----------------------------------------------------------
  306. void EDITOR::LoadScene(char *FileName)
  307. {
  308.     FILE *File;
  309.     File = fopen(FileName,"rb");
  310.  
  311.     fread(Buffer,sizeof(E_STRUCTURE),Editor_MaxStructures,File);
  312.  
  313.     fclose(File);
  314.  
  315.     ActStructure = 0;
  316.     Cursor = Buffer[0];
  317.  
  318. }
  319.  
  320. //export
  321. //--------------------------------------------------------------
  322. void EDITOR::ExportScene(char *FileName)
  323. {
  324.     FILE *File;
  325.     File = fopen(FileName,"w");
  326.  
  327.  
  328.     for (int i=0;i<Editor_MaxStructures;i++)
  329.     {
  330.         if (Buffer[i].Active == false)
  331.             continue;
  332.     
  333.         fprintf(File,"AddStructure( %d,%d,%d,",i,Buffer[i].ModelIndex,
  334.                                           Buffer[i].Mode);
  335.  
  336.         fprintf(File,"Get3D(%.1ff,%.1ff,%.1ff),",Buffer[i].Pos.X,
  337.                                            0.0f,
  338.                                            Buffer[i].Pos.Z);
  339.  
  340.         fprintf(File,"Get3D(%.1ff,%.1ff,%.1ff));",Buffer[i].Rot.X,
  341.                                              Buffer[i].Rot.Y,
  342.                                              Buffer[i].Rot.Z);
  343.  
  344.         fprintf(File,"\n");
  345.  
  346.     }
  347.  
  348.     fclose(File);
  349. }