K3dFontBmp.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 "K3dFontBmp.h"
00034 
00035 #define K_FONT_SPACE 480
00036 #define K_NUM_DISPLAY_LISTS 256
00037 
00041 K3dFontBmp::K3dFontBmp(K3dGameData *_pGameData)
00042 {
00043         m_pGameData = _pGameData;
00044         m_iFps = 0;
00045         m_iFrame = 0;
00046         m_iFpsTmp = 0;
00047 }
00048 
00049 K3dFontBmp::~K3dFontBmp(void)
00050 {
00051         cout << "K3dFontBmp::~K3dFontBmp(void)" << endl;
00052         glDeleteLists(0,K_NUM_DISPLAY_LISTS);   // Delete 256 display list
00053 }
00054 
00056 void K3dFontBmp::LoadBmpFonts()
00057 {
00058         cout << "void K3dFontBmp::LoadBmpFonts()" << endl;
00059         K3dString strFc = "void K3dFontBmp::LoadBmpFonts() -- ";
00060         K3dString strOut;
00061         // Get virtual machine from global data
00062         K3dLua *pLua = m_pGameData->GetLua();
00063         // Get number of planes
00064         pLua->LuaGetGlobal(K_STR_NUM_BMP_FONTS);
00065     if (!pLua->LuaIsNumber(-1))
00066         {
00067         //strOut = strFc + K_STR_NUM_BMP_FONTS;
00068                 //strOut += " is not a valid number";
00069                 //pLua->LuaError(strOut.GetString().c_str());
00070                 cerr << "Warning: void K3dFontBmp::LoadBmpFonts(): Doesn`t load " << K_STR_NUM_BMP_FONTS << " from map file" << endl;
00071                 return;
00072     }
00073         int iNumBmpFonts = (int) pLua->LuaToNumber(-1);
00074         for(int i=0; i<iNumBmpFonts; i++)
00075         {
00076                 K3dBmpFontObj *pBmpFont = m_pGameData->GetBmpFontObjSP().New();;
00077                 K3dString kString(K_STR_BMPFONT);
00078                 // Add number behind string
00079                 kString.AddNumber(i);
00080                 pLua->LuaGetGlobal(kString.GetString().c_str());
00081                 if (!pLua->LuaIsTable(-1))
00082                 {
00083                         strOut = strFc + kString;
00084                         strOut += " is not a valid bmp font table";
00085                         pLua->LuaError(strOut.GetString().c_str());
00086                 }
00087                 pBmpFont->GetName() = pLua->LuaGetFieldString(K_STR_NAME);
00088                 K3dString strTexture = pLua->LuaGetFieldString(K_STR_TEXTURE_TABLE);
00089                 int iTextureId = m_pGameData->GetTextureBuild()->FindTextureId(strTexture.GetString().c_str());
00090                 if(iTextureId >= 0)
00091                 {
00092                         K3dTextureObj *pTexture = m_pGameData->GetTextureObjSP().Get( iTextureId);
00093                         // Create texture and get texture index
00094                         pBmpFont->GetTextureId() =  pTexture->GetId();
00095                 }
00096                 else
00097                 {
00098                         cerr << "Error: void K3dFontBmp::LoadBmpFonts(): Texture id " << iTextureId << " is negative" << endl;
00099                 }
00100         }
00101         if(iNumBmpFonts>0)
00102         {
00103                 InitFontBmp();
00104         }
00105 }
00106 
00108 void K3dFontBmp::InitFontBmp()
00109 {
00110         cout << "void K3dFontBmp::InitFontBmp()" << endl;
00111         Reset();
00112 }
00113 
00115 void  K3dFontBmp::DeleteFontBmp()
00116 {
00117         cout << "K3dFontBmp::DeleteFontBmp()" << endl;
00118         while(m_pGameData->GetBmpFontObjSP().GetNum())
00119         {
00120                 K3dBmpFontObj *p = m_pGameData->GetBmpFontObjSP().Get(0);
00121                 p = m_pGameData->GetBmpFontObjSP().Delete(p);
00122         }
00123         glDeleteLists(0,K_NUM_DISPLAY_LISTS);
00124 }
00125 
00127 void K3dFontBmp::Destroy()
00128 {
00129         cout << "void K3dFontBmp::Destroy()" << endl;
00130         glDeleteLists(m_uiBase,K_NUM_DISPLAY_LISTS);    // Delete 256 display list
00131         Reset();
00132 }
00133 
00135 void K3dFontBmp::Reset()
00136 {
00137         cout << "void K3dFontBmp::Reset()" << endl;
00138         // Get graphics settings
00139         TGraphicOption *ptGOption = m_pGameData->GetGraphicOption();
00140 
00141         m_uiBase = 0;
00142         m_uiTextureId = 0;
00143         m_bNewFrame = true;
00144         m_iNumLines = ptGOption->iHeight >> 4;
00145         m_iLine = m_iNumLines;
00146         float fRow;
00147         if(ptGOption->iHeight)
00148         {
00149                 m_fSpace = K_FONT_SPACE / (float) ptGOption->iHeight;
00150         }
00151         fRow = (float)16 - ((float)1 - m_fSpace);
00152         for(int i=0; i<m_iNumLines; i++)
00153         {
00154                 m_afLine[i] = (float)i * fRow; 
00155         }
00156         m_fFirstLine = (float) 0;
00157         m_fSpace = (float) 0;
00158         m_fRow = (float) 0; 
00159         m_bDraw = false;
00160         m_iNumDraw = 0;
00161         m_bDrawFps = false;
00162         m_bDrawCamPos = false;
00163         m_bDrawNumFaces = false;
00164 
00165         TFontBmpOption *ptFOption = m_pGameData->GetFontBmpOption();
00166         // Get application path
00167         K3dString strPath = m_pGameData->GetPath(K_PATH_APP);
00168         // Add font filename
00169         K3dString strFileName = strPath + ptFOption->strFont;
00170 
00171         LoadTexture(strFileName);
00172         CreateDisplayLists(ptFOption->bItalic);
00173         SetDrawFps(ptFOption->bDrawFps);
00174         SetDrawCamPos(ptFOption->bDrawCamPos);
00175         SetDrawNumFaces(ptFOption->bDrawNumFaces);
00176 }
00177 
00179 void K3dFontBmp::CreateDisplayLists(bool _bItalic)
00180 {
00181         cout << "void K3dFontBmp::CreateDisplayLists(bool _bItalic)" << endl;
00182         // Texture coordinates
00183         float  fX = (float)0;
00184         float   fY = (float)0;
00185         // Result of 1/16
00186         const float fR =  (float)0.0625;
00187 
00188         // Create 256 display lists
00189         m_uiBase = glGenLists(K_NUM_DISPLAY_LISTS);
00190         // Go through all display lists
00191         for(int i=0; i<K_NUM_DISPLAY_LISTS; i++)
00192         {
00193                 // Calc actual char texture coordinates 
00194                 fX= (float)(i%16) / 16;
00195                 fY= (float)(i>>4) / 16;
00196 
00197                 // Create new list 
00198                 glNewList(m_uiBase+i,GL_COMPILE);
00199                 glBegin(GL_TRIANGLE_FAN);
00200                 glTexCoord2f(fX,(float)1-fY);                   glVertex2i(0,16);       // Left up vertex
00201                 glTexCoord2f(fX,(float)1-fY-fR);                glVertex2i(0,0);        // Left down vertex
00202                 glTexCoord2f(fX+fR,(float)1-fY-fR);             glVertex2i(16,0);       // Right down vertex
00203                 glTexCoord2f(fX+fR,(float)1-fY);                glVertex2i(16,16);      // Right up vertex
00204                 glEnd();// Triangle strip end
00205                 // Translate to the next char
00206                 glTranslatef((float)10,(float)0,(float)0);
00207                 glEndList();    // End new display list
00208         }
00209 
00210         // Set the display list base for glCallLists, set normal or italic font
00211         glListBase(m_uiBase-32+(128*_bItalic));
00212 }
00213 
00217 void K3dFontBmp::Print(const char *_str, K3dVector3 _kColor)
00218 {
00219         TGraphicOption *pkOption = m_pGameData->GetGraphicOption();
00220 
00221         if(m_bDraw)
00222         {
00223                 // Subtract line index
00224                 if(m_iLine >= 0)
00225                 {
00226                         m_iLine--;
00227                 }
00228 
00229                 glEnable(GL_TEXTURE_2D);
00230 
00231                 // Set font texture
00232                 glBindTexture(GL_TEXTURE_2D, m_uiTextureId);
00233                 // Set font color
00234                 glColor3fv(_kColor.GetVectorArray());
00235 
00236                 glEnable(GL_BLEND);
00237                 glBlendFunc(GL_ONE, GL_ONE);
00238                 // Disable depth test
00239                 glDisable(GL_DEPTH_TEST);
00240 
00241                 // Choose projection matrix
00242                 glMatrixMode(GL_PROJECTION);
00243                 glPushMatrix();
00244 
00245                 // Reset projection matrix
00246                 glLoadIdentity();
00247                 // Set cross projection by screen resolution
00248                 glOrtho(0,pkOption->iWidth,0,pkOption->iHeight,1 ,0);
00249                 // Set modelview matrix
00250                 glMatrixMode(GL_MODELVIEW);
00251                 glPushMatrix();
00252                 // Reset modelview matrix
00253                 glLoadIdentity();
00254                 // Translate text
00255                 glTranslatef((float) 0,m_afLine[m_iLine],(float) 0);
00256                 // Draw text to the screen
00257                 glCallLists((GLsizei)strlen(_str),GL_BYTE, _str);       
00258                 glPopMatrix();  // Pop modelview matrix
00259 
00260                 // Choose projection matrix
00261                 glMatrixMode(GL_PROJECTION);
00262                 glPopMatrix(); // Pop projection matrix
00263 
00264                 glMatrixMode(GL_MODELVIEW);
00265                 // Enable detph test
00266                 glEnable(GL_DEPTH_TEST);
00267                 // Reset color to white
00268                 glColor3f(1, 1, 1);
00269                 glDisable(GL_BLEND);
00270         }
00271         else
00272         {
00273                 // If new frame than reset number of draw
00274                 if(m_bNewFrame)
00275                 {
00276                         m_iNumDraw = 0;
00277                         //m_bNewFrame = false;
00278                         m_iNumDraw++;
00279                         m_bNewFrame = false;
00280                 }
00281                 else
00282                 {
00283                         // Add number of draw object
00284                         m_iNumDraw++;
00285                 }
00286                 if(m_iNumDraw<m_iNumLines)
00287                 {
00288                         strcpy(m_atDraw[m_iNumDraw-1].str, _str);
00289                         m_atDraw[m_iNumDraw-1].kColor = _kColor;
00290                 }
00291 
00292         }
00293 }
00294 
00297 void K3dFontBmp::DrawValue(const char *_strComment)
00298 {
00299         K3dVector3 kColor;
00300 
00301         TTextureOption *ptOption = m_pGameData->GetTextureOption();
00302 
00303         if(ptOption->bMultitexture)                             // Jestlie je zapnut mutlitexturing
00304         {
00305                 /*
00306                 // Active first texture of multitexture
00307                 glActiveTextureARB(GL_TEXTURE1_ARB);
00308                 if(!glIsEnabled(GL_TEXTURE_2D))         // Jestlie je vypnuto texturov��                    glEnable(GL_TEXTURE_2D);                                                        // Zapni texturov��                 glBindTexture(GL_TEXTURE_2D, K3dTexture::m_uiWhiteTexture);             // Nastav b�ou texturu
00309                 // Active second texture of multitexture
00310                 glActiveTextureARB(GL_TEXTURE0_ARB);
00311                 if(!glIsEnabled(GL_TEXTURE_2D))         // Jestlie je vypnuto texturov��                    glEnable(GL_TEXTURE_2D);                                                        // Zapni texturov��                 glBindTexture(GL_TEXTURE_2D, K3dTexture::m_uiWhiteTexture);             // Nastav b�ou texturu
00312                 */
00313         }
00314         else
00315         {
00316                 if(glIsEnabled(GL_TEXTURE_2D))          // Jestlie je zapnuto texturov
00317                 {
00318                         glDisable(GL_TEXTURE_2D);       // Vypni texturov
00319                 }
00320                 kColor.Set((float)0,(float)1,(float)1);
00321                 Print(_strComment,  kColor);
00322         }
00323 }
00324 
00326 void K3dFontBmp::DrawFps()
00327 {       
00328         //float fLine;
00329         //K3dVector2 kRasterPos;
00330         K3dVector3 kColor;
00331         char acString[200];
00332         //int iFps = 0;
00333 
00334         TTextureOption *ptOption = m_pGameData->GetTextureOption();
00335 
00336         if(ptOption->bMultitexture)                                     // Jestlie je zapnut mutlitexturing
00337         {
00338                 /*
00339                 // Active first texture of multitexture
00340                 glActiveTextureARB(GL_TEXTURE1_ARB);
00341                 if(!glIsEnabled(GL_TEXTURE_2D))         // Jestlie je vypnuto texturov��                    glEnable(GL_TEXTURE_2D);                                                        // Zapni texturov��                 glBindTexture(GL_TEXTURE_2D, K3dTexture::m_uiWhiteTexture);             // Nastav b�ou texturu
00342                 // Active second texture of multitexture
00343                 glActiveTextureARB(GL_TEXTURE0_ARB);
00344                 //g_K3dEngine.m_pGLExt->glActiveTextureARB(GL_TEXTURE0_ARB);                            // Zaktivuj druhou texturu
00345                 if(!glIsEnabled(GL_TEXTURE_2D))         // Jestlie je vypnuto texturov��                    glEnable(GL_TEXTURE_2D);                                                        // Zapni texturov��                 glBindTexture(GL_TEXTURE_2D, K3dTexture::m_uiWhiteTexture);             // Nastav b�ou texturu
00346                 */
00347         }
00348         else                                                                                                    // Jinak
00349         {
00350                 //if(glIsEnabled(GL_TEXTURE_2D))                // Jestlie je zapnuto texturov��                            glDisable(GL_TEXTURE_2D);                                                       // Vypni texturov��         }
00351         }
00352 
00353         m_iFrame++;
00354         // Get global timer
00355         K3dTimer *pTimer = m_pGameData->GetTimer();
00356         m_iFpsTmp = m_iFpsTmp + (int)pTimer->GetFPS();
00357         if(m_iFrame>(int)pTimer->GetFPS())
00358         {
00359                 m_iFps = m_iFpsTmp / m_iFrame;
00360                 m_iFpsTmp = 0;
00361                 m_iFrame = 0;
00362         }
00363 
00364         kColor.Set(0,1,0);
00365         snprintf(acString, 200, "%s%i", "FPS: ", m_iFps);
00366         Print(acString, kColor);
00367 
00368         
00369 }
00370 
00371 void K3dFontBmp::DrawFontBmp()
00372 {
00373         // Enable draw switch
00374         m_bDraw = true;
00375 
00376         int iNumBmpFonts = m_pGameData->GetBmpFontObjSP().GetNum();
00377         for(int i=0; i<iNumBmpFonts; i++)
00378         {
00379                 K3dBmpFontObj *pBmpFont = m_pGameData->GetBmpFontObjSP().Get( i);
00380                 m_uiTextureId = pBmpFont->GetTextureId();
00381                 // Draw fps
00382                 if(m_bDrawFps)
00383                 {
00384                         DrawFps();
00385                 }
00386                 // Draw camera position
00387                 if(m_bDrawCamPos)
00388                 {
00389                         DrawCameraPosition();
00390                 }
00391                 // Draw number of faces in view
00392                 if(m_bDrawNumFaces)
00393                 {
00394                         DrawNumFaces();
00395                 }
00396         
00397                 // Print saved strings
00398                 for(int j=0; j<m_iNumDraw; j++)
00399                 {
00400                         Print(m_atDraw[j].str,  m_atDraw[j].kColor);
00401                 }
00402         }
00403         // Disable draw switch
00404         m_bDraw = false;
00405 
00406 }

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