K2dImageBuild.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 "K2dImageBuild.h"
00034 
00038 K2dImageBuild::K2dImageBuild(K3dGameData *_pGameData)
00039 {
00040         cout << "K2dImageBuild::K2dImageBuild()" << endl;
00041         m_pGameData = _pGameData;
00042         // Set this pointer to the game data
00043         m_pGameData->SetImageBuild(this);
00044         m_pTextureBuild = m_pGameData->GetTextureBuild();
00045 }
00046 
00047 K2dImageBuild::~K2dImageBuild(void)
00048 {
00049         DeleteImages();
00050 }
00051 
00053 void K2dImageBuild::LoadImages()
00054 {
00055         cout << "void K2dImageBuild::LoadImages()" << endl;
00056         K3dString strFc = "void K2dImageBuild::LoadImages() -- ";
00057         K3dString strOut;
00058 
00059         // Get Lua from global data
00060         K3dLua *pLua = m_pGameData->GetLua();   
00061 
00062         // Get number of 2d images
00063         pLua->LuaGetGlobal(K_STR_NUM_2DIMAGES);
00064     if (!pLua->LuaIsNumber(-1))
00065         {
00066         //strOut = strFc + K_STR_NUM_2DIMAGES;
00067                 //strOut += " is not a valid number";
00068                 //pLua->LuaError(strOut.GetString().c_str());
00069                 cerr << "Warning: void K2dImageBuild::LoadImages(): Doesn`t load " << K_STR_NUM_2DIMAGES << " from map file" << endl;
00070                 return;
00071     }
00072         
00073         int iNumImages = (int) pLua->LuaToNumber(-1);
00074         for(int i=0; i<iNumImages; i++)
00075         {
00076                 
00077                 K2dImageObj *p2DImage = new K2dImageObj();
00078                 
00079                 K3dString kString(K_STR_IMAGE2D);
00080                 // Add number behind string
00081                 kString.AddNumber(i);
00082                 
00083                 pLua->LuaGetGlobal(kString.GetString().c_str());
00084                 if (!pLua->LuaIsTable(-1))
00085                 {
00086                         strOut = strFc + kString;
00087                         strOut += " is not a valid bmp font table";
00088                         pLua->LuaError(strOut.GetString().c_str());
00089                 }
00090 
00091                 p2DImage->GetName() = pLua->LuaGetFieldString(K_STR_NAME);
00092                 p2DImage->GetId() = m_pGameData->Get2dImageObjSP().GetNum();
00093                 // Get texture
00094                 K3dString strTexture = pLua->LuaGetFieldString(K_STR_TEXTURE_TABLE);
00095                 int iTextureId = m_pTextureBuild->FindTextureId(strTexture.GetString().c_str());
00096                 if(iTextureId >= 0)
00097                 {
00098                         K3dTextureObj *pTexture = m_pGameData->GetTextureObjSP().Get( iTextureId);
00099                         // Get texture index
00100                         p2DImage->GetTextureId() =  pTexture->GetId();
00101                         // Load blend
00102                         bool bBlend = pLua->LuaGetFieldBool(K_STR_BLEND);
00103                         p2DImage->SetBlend(bBlend);
00104                         // Load texture size
00105                         SDL_Rect tRect;
00106                         tRect.x = (int)pLua->LuaGetFieldNumber(K_STR_RECTANGLE_X);
00107                         tRect.y = (int)pLua->LuaGetFieldNumber(K_STR_RECTANGLE_Y);
00108                         // Set rectange height and width
00109                         tRect.w = pTexture->GetWidth() + tRect.x;
00110                         tRect.h = pTexture->GetHeight() + tRect.y;
00111                         p2DImage->SetRectangle(tRect);
00112                 }
00113                 else
00114                 {
00115                         cerr << "Error: void K2dImageBuild::LoadImages(): Texture id " << iTextureId << " is negative" << endl;
00116                 }
00117         }
00118 }
00119 
00121 void K2dImageBuild::DeleteImages()
00122 {
00123         cout << "void K2dImageBuild::DeleteImages()" << endl;
00124 
00125         while(m_pGameData->Get2dImageObjSP().GetNum())
00126         {
00127                 // Get image from game data
00128                 K2dImageObj *pkImage = m_pGameData->Get2dImageObjSP().Get(0);
00129                 // Get texture index
00130                 GLuint iTextureId = pkImage->GetTextureId();
00131                 glDeleteTextures(1, &iTextureId);
00132                 pkImage = m_pGameData->Get2dImageObjSP().Delete(pkImage);
00133         }
00134 }

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