K3dSphereBuild.cpp

Go to the documentation of this file.
00001 
00012 /***************************************************************************
00013  *   Copyright (C) 2007 by Jan Koci   *
00014  *   honza.koci@email.cz   *
00015  *   http://kengine.sourceforge.net/tutorial/
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  *   This program is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00025  *   GNU General Public License for more details.                          *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU General Public License     *
00028  *   along with this program; if not, write to the                         *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00031  ***************************************************************************/
00032 
00033 #include "K3dSphereBuild.h"
00034 
00038 K3dSphereBuild::K3dSphereBuild(K3dGameData *_pGameData)
00039 {
00040         m_pGameData = _pGameData;
00041         // Set this pointer to the game data
00042         m_pGameData->SetSphereBuild(this);
00043         m_pVector3Build = m_pGameData->GetVector3Build();
00044         m_pColor3Build =  m_pGameData->GetColor3Build();
00045         m_pMatrixBuild = m_pGameData->GetMatrixBuild();
00046         m_pTextureBuild = m_pGameData->GetTextureBuild();
00047 }
00048 
00049 K3dSphereBuild::~K3dSphereBuild(void)
00050 {
00051         // Delete all spheres
00052         DeleteSpheres();
00053 }
00054 
00057 K3dSphereObj *K3dSphereBuild::CreateNewSphereObj()
00058 {
00059         K3dSphereObj *pSphereObj = new K3dSphereObj(m_pGameData);
00060         // Add sphere pointer to safe pointer array
00061         m_pGameData->GetSphereObjSP().Add(pSphereObj);
00062         pSphereObj->SetPosition(m_pVector3Build->CreateNewVector3());
00063         pSphereObj->SetColor(m_pColor3Build->CreateNewColor3());
00064         pSphereObj->SetRelPosition(m_pVector3Build->CreateNewVector3());
00065         pSphereObj->SetRotation(m_pVector3Build->CreateNewVector3());
00066         pSphereObj->SetMatrix(m_pMatrixBuild->CreateNewMatrix());
00067         pSphereObj->SetRotMatrix(m_pMatrixBuild->CreateNewMatrix());
00068         pSphereObj->GetId() = m_pGameData->GetSphereObjSP().GetNum()-1;
00069         return pSphereObj;
00070 }
00071 
00073 void K3dSphereBuild::LoadSpheres()
00074 {
00075         cout << "void K3dSphereBuild::LoadSpheres()" << endl;
00076         K3dString strFc = "void K3dSphereBuild::LoadSpheres() -- ";
00077         K3dString strOut;
00078 
00079         // Get virtual machine from global data
00080         K3dLua *pLua = m_pGameData->GetLua();
00081         
00082         // Get number of spheres
00083         pLua->LuaGetGlobal(K_STR_NUM_SPHERES);
00084     if (!pLua->LuaIsNumber(-1))
00085         {
00086         //strOut = strFc + K_STR_NUM_SPHERES;
00087                 //strOut += " is not a valid number";
00088                 //pLua->LuaError(strOut.GetString().c_str());
00089                 cerr << "Warning :: void K3dSphereBuild::LoadSpheres() -- Doesn`t load " << K_STR_NUM_SPHERES << " from map file" << endl;
00090     }
00091         
00092         int iNumSpheres = (int) pLua->LuaToNumber(-1);
00093         for(int i=0; i<iNumSpheres; i++)
00094         {
00095                 K3dSphereObj *pSphere = CreateNewSphereObj();
00096                 K3dString kString(K_STR_SPHERE);
00097                 // Add number behind string
00098                 kString.AddNumber(i);
00099                 
00100                 pLua->LuaGetGlobal(kString.GetString().c_str());
00101                 if (!pLua->LuaIsTable(-1))
00102                 {
00103                         strOut = strFc + kString;
00104                         strOut += " is not a valid sphere table";
00105                         pLua->LuaError(strOut.GetString().c_str());
00106                 }
00107                 
00108                 pSphere->GetName() = pLua->LuaGetFieldString(K_STR_NAME);
00109                 // Get texture
00110                 K3dString strTexture = pLua->LuaGetFieldString(K_STR_TEXTURE_TABLE);
00111                 int iTextureId = m_pTextureBuild->FindTextureId(strTexture.GetString().c_str());
00112                 if(iTextureId >=0)
00113                 {
00114                         K3dTextureObj *pTexture = m_pGameData->GetTextureObjSP().Get( iTextureId);
00115                         pSphere->GetTextureId() =  pTexture->GetId();
00116                         pSphere->GetRelPosition()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_POS_X);
00117                         pSphere->GetRelPosition()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_POS_Y);
00118                         pSphere->GetRelPosition()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_POS_Z);
00119                         pSphere->GetRotation()->GetX() =  (float)pLua->LuaGetFieldNumber(K_STR_ROT_X);
00120                         pSphere->GetRotation()->GetY() =  (float)pLua->LuaGetFieldNumber(K_STR_ROT_Y);
00121                         pSphere->GetRotation()->GetZ() =  (float)pLua->LuaGetFieldNumber(K_STR_ROT_Z);
00122                         pSphere->GetRadius() =  (float)pLua->LuaGetFieldNumber(K_STR_RADIUS);
00123                         pSphere->GetColor()->GetR() =  (GLubyte)pLua->LuaGetFieldNumber(K_STR_R);
00124                         pSphere->GetColor()->GetG() =  (GLubyte)pLua->LuaGetFieldNumber(K_STR_G);
00125                         pSphere->GetColor()->GetB() =  (GLubyte)pLua->LuaGetFieldNumber(K_STR_B);
00126                         pSphere->GetNumSlices() = (int)pLua->LuaGetFieldNumber(K_STR_NUM_SLICES);
00127                         pSphere->GetNumStacks() = (int)pLua->LuaGetFieldNumber(K_STR_NUM_STACKS);
00128                         pSphere->Move( *pSphere->GetRelPosition(), *pSphere->GetRotation());
00129                 }
00130                 else
00131                 {
00132                         cerr << "Error: void K3dSphereBuild::LoadSpheres(): Texture id " << iTextureId << " is negative" << endl;
00133                 }
00134         }
00135 }
00136 
00138 void K3dSphereBuild::BuildSpheres()
00139 {
00140 }
00141 
00143 void K3dSphereBuild::DeleteSpheres()
00144 {
00145         std::cout << "void K3dSphereBuild::DeleteSpheres()" << endl;
00146         while(m_pGameData->GetSphereObjSP().GetNum())
00147         {
00148                 K3dSphereObj *p = m_pGameData->GetSphereObjSP().Get(0);
00149                 p = m_pGameData->GetSphereObjSP().Delete(p);
00150         }
00151 }
00152 
00153 

Generated on Thu Aug 16 23:53:28 2007 for K3dEngine by  doxygen 1.5.0