00001 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 #include "K3dTextureBuild.h"
00036 
00040 K3dTextureBuild::K3dTextureBuild(K3dGameData *_pGameData)
00041 {
00042         cout << "K3dTexture::K3dTexture()" << endl;
00043         m_pGameData = _pGameData;
00044         
00045         m_pGameData->SetTextureBuild(this);
00046 }
00047 
00048 K3dTextureBuild::~K3dTextureBuild()
00049 {
00050         cout << "K3dTexture::~K3dTexture()" << endl;
00051         DeleteTextures();
00052 }
00053 
00055 void K3dTextureBuild::DeleteTextures()
00056 {
00057         while(m_pGameData->GetTextureObjSP().GetNum())
00058         {
00059                 K3dTextureObj *p = m_pGameData->GetTextureObjSP().Get(0);
00060                 p = m_pGameData->GetTextureObjSP().Delete(p);
00061         }
00062 }
00063 
00065 void K3dTextureBuild::LoadTextures()
00066 {
00067         cout << "void K3dTextureBuild::LoadTextures()" << endl;
00068         K3dString strFc = "void K3dTextureBuild::LoadTextures() -- ";
00069         K3dString strOut;
00070         
00071         K3dLua *pLua = m_pGameData->GetLua();
00072         
00073         pLua->LuaGetGlobal(K_STR_NUM_TEXTURES);
00074     if (!pLua->LuaIsNumber(-1))
00075         {
00076                 cerr << "Warning: void K3dTextureBuild::LoadTextures(): Doesn`t load " << K_STR_NUM_TEXTURES << " from map file" << endl;
00077                 return;
00078     }
00079         int iNumTextures = (int) pLua->LuaToNumber(-1);
00080         for(int i=0; i<iNumTextures; i++)
00081         {
00082                 K3dTextureObj *pTexture = m_pGameData->GetTextureObjSP().New();
00083                 K3dString kString(K_STR_TEXTURE_TABLE);
00084                 
00085                 kString.AddNumber(i);
00086                 
00087                 pLua->LuaGetGlobal(kString.GetString().c_str());
00088                 if (!pLua->LuaIsTable(-1))
00089                 {
00090                         strOut = strFc + kString;
00091                         strOut += " is not a valid texture table";
00092                         pLua->LuaError(strOut.GetString().c_str());
00093                 }
00094                 
00095                 pTexture->GetName() = pLua->LuaGetFieldString(K_STR_NAME);
00096                 K3dString strPath = pLua->LuaGetFieldString(K_STR_PATH);
00097                 
00098                 K3dString strFileName = m_pGameData->GetPath(K_PATH_APP) + strPath;
00099                 
00100                 pTexture->GetId() = CreateTexture(strFileName.GetString().c_str());
00101                 
00102                 pTexture->GetWidth() = GetTextureWidth();
00103                 pTexture->GetHeight() = GetTextureHeight();
00104         }
00105 }
00106 
00108 int K3dTextureBuild::FindTextureId(const char* _strName)
00109 {
00110         K3dString strName(_strName);
00111         for(int i=0; i<m_pGameData->GetTextureObjSP().GetNum(); i++)
00112         {
00113                 K3dTextureObj *pTexture = m_pGameData->GetTextureObjSP().Get(i);
00114                 if(strName == pTexture->GetName())
00115                 {
00116                         return i;
00117                 }
00118         }
00119         return -1;
00120 }
00121 
00125 bool K3dTextureBuild::SwapSurfaceRows(SDL_Surface *_ptSurface)
00126 {
00127         cout << "bool K3dTexture::SwapSurfaceRows(SDL_Surface *_ptSurface)" << endl;
00129         Uint8 *puiHiRow, *puiLoRow, *puiTmpRow;
00130 
00131         
00132         puiTmpRow = (Uint8 *)malloc(_ptSurface->pitch);
00133 
00134         if(puiTmpRow == NULL)
00135         {
00136                 fprintf(stderr, "Unable to allocate memory.\n");
00137                 cerr << "bool K3dTexture::SwapSurfaceRows() -- Error - Unable to allocate memory" << endl;
00138                 return false;
00139         }
00140 
00141         
00142         puiHiRow = (Uint8 *)_ptSurface->pixels;
00143         puiLoRow = puiHiRow + (_ptSurface->h * _ptSurface->pitch) - _ptSurface->pitch;
00144 
00145         if(SDL_MUSTLOCK(_ptSurface))
00146         {
00147                 if(SDL_LockSurface(_ptSurface) == -1)
00148                 {
00149                         fprintf(stderr, "Unable to lock _ptSurface.\n");
00150                         cerr << "bool K3dTexture::SwapSurfaceRows() -- Error - Unable to lock _ptSurface." << endl;
00151                         free(puiTmpRow);
00152                         return false;
00153                 }
00154         }
00155 
00156         for(int i = 0; i < _ptSurface->h / 2; i++)
00157         {
00158                 memcpy(puiTmpRow, puiHiRow, _ptSurface->pitch);
00159                 memcpy(puiHiRow, puiLoRow, _ptSurface->pitch);
00160                 memcpy(puiLoRow, puiTmpRow, _ptSurface->pitch);
00161 
00162                 puiHiRow += _ptSurface->pitch;
00163                 puiLoRow -= _ptSurface->pitch;
00164         }
00165 
00166         if (SDL_MUSTLOCK(_ptSurface))
00167         {
00168                 SDL_UnlockSurface(_ptSurface);
00169         }
00170 
00171         free(puiTmpRow);
00172 
00173         return true;
00174 }
00175 
00179 GLuint K3dTextureBuild::CreateTexture(SDL_Surface *_ptSurface)
00180 {
00181         cout << "GLuint K3dTexture::CreateTexture(SDL_Surface *_ptSurface)" << endl;
00182         GLuint iTexture;                
00183         Uint32 uiSavedFlags;    
00184         Uint8  uiSavedAlpha;
00185         SDL_Surface *ptTmpImg;  
00186 
00187         
00188         ptTmpImg = SDL_CreateRGBSurface(
00189                 SDL_SWSURFACE,          
00190                 _ptSurface->w, _ptSurface->h,   
00191                 m_pGameData->GetGraphicOption()->iColorBits,                    
00192         #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00193                                 0x000000FF,             
00194                                 0x0000FF00,
00195                                 0x00FF0000,
00196                                 0xFF000000
00197         #else
00198                                 0xFF000000,
00199                                 0x00FF0000,
00200                                 0x0000FF00,
00201                                 0x000000FF
00202         #endif
00203                 );
00204 
00205         if(ptTmpImg == NULL)
00206                 return 0;
00207 
00208         
00209         m_iWidth = _ptSurface->w;
00210         m_iHeight = _ptSurface->h;
00211 
00212         
00213         uiSavedFlags = _ptSurface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK);
00214         uiSavedAlpha = _ptSurface->format->alpha;
00215 
00216         if((uiSavedFlags & SDL_SRCALPHA) == SDL_SRCALPHA)
00217                 SDL_SetAlpha(_ptSurface, 0, 0);
00218 
00219         
00220         SDL_Rect tArea;
00221         tArea.x = 0;
00222         tArea.y = 0;
00223         tArea.w = _ptSurface->w;
00224         tArea.h = _ptSurface->h;
00225         SDL_BlitSurface(_ptSurface, &tArea, ptTmpImg, &tArea);
00226 
00227         
00228         if((uiSavedFlags & SDL_SRCALPHA) == SDL_SRCALPHA)
00229                 SDL_SetAlpha(_ptSurface, uiSavedFlags, uiSavedAlpha);
00230 
00231         
00232         if(!SwapSurfaceRows(ptTmpImg))
00233         {
00234                 cout << "GLuint K3dTexture::CreateTexture(SDL_Surface *_ptSurface) -- Error - SwapSurfaceRows returns some error..." << endl;
00235                 SDL_FreeSurface(ptTmpImg);
00236                 return 0;
00237         }
00238         
00239         cout << "GLuint K3dTexture::CreateTexture(SDL_Surface *_ptSurface) -- glGenTextures -- iTexture = " << iTexture << endl;
00240         
00241         glGenTextures(1, &iTexture);
00242         glBindTexture(GL_TEXTURE_2D, iTexture);
00243         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00244         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00245         glTexImage2D(GL_TEXTURE_2D,
00246                 0,
00247                 GL_RGBA,
00248                 _ptSurface->w, _ptSurface->h,
00249                 0,
00250                 GL_RGBA,
00251                 GL_UNSIGNED_BYTE,
00252                 ptTmpImg->pixels);
00253 
00254         cout << "GLuint K3dTexture::CreateTexture(SDL_Surface *_ptSurface) -- SDL_FreeSurface " << ptTmpImg << endl;
00255         SDL_FreeSurface(ptTmpImg);
00256         return iTexture;
00257 }
00258 
00261 GLuint K3dTextureBuild::CreateTexture(const char* _strFileName)
00262 {
00263         cout << "GLuint K3dTexture::CreateTexture()" << endl;
00264         cout << "Input filename = " << _strFileName << endl;
00265         GLuint iTexture;
00266 
00267         
00268         
00269         
00270         
00271         SDL_Surface *ptImg = IMG_Load(_strFileName);
00272 
00273         if(ptImg == NULL)
00274         {
00275                 
00276                 
00277                 cerr << "GLuint K3dTexture::CreateTexture() -- Error - Doesn`t load texture " << _strFileName << SDL_GetError() << endl;
00278                 return 0;
00279         }
00280 
00281         iTexture = CreateTexture(ptImg);
00282         cout << "GLuint K3dTexture::CreateTexture(const char* _strFileName) -- SDL_FreeSurface " << ptImg << endl;
00283         SDL_FreeSurface(ptImg);
00284         cout << "GLuint K3dTexture::CreateTexture(const char* _strFileName) -- SDL_FreeSurface - OK " << ptImg << endl;
00285         return iTexture;
00286 }
00287