00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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);
00053 }
00054
00056 void K3dFontBmp::LoadBmpFonts()
00057 {
00058 cout << "void K3dFontBmp::LoadBmpFonts()" << endl;
00059 K3dString strFc = "void K3dFontBmp::LoadBmpFonts() -- ";
00060 K3dString strOut;
00061
00062 K3dLua *pLua = m_pGameData->GetLua();
00063
00064 pLua->LuaGetGlobal(K_STR_NUM_BMP_FONTS);
00065 if (!pLua->LuaIsNumber(-1))
00066 {
00067
00068
00069
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
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
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);
00131 Reset();
00132 }
00133
00135 void K3dFontBmp::Reset()
00136 {
00137 cout << "void K3dFontBmp::Reset()" << endl;
00138
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
00167 K3dString strPath = m_pGameData->GetPath(K_PATH_APP);
00168
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
00183 float fX = (float)0;
00184 float fY = (float)0;
00185
00186 const float fR = (float)0.0625;
00187
00188
00189 m_uiBase = glGenLists(K_NUM_DISPLAY_LISTS);
00190
00191 for(int i=0; i<K_NUM_DISPLAY_LISTS; i++)
00192 {
00193
00194 fX= (float)(i%16) / 16;
00195 fY= (float)(i>>4) / 16;
00196
00197
00198 glNewList(m_uiBase+i,GL_COMPILE);
00199 glBegin(GL_TRIANGLE_FAN);
00200 glTexCoord2f(fX,(float)1-fY); glVertex2i(0,16);
00201 glTexCoord2f(fX,(float)1-fY-fR); glVertex2i(0,0);
00202 glTexCoord2f(fX+fR,(float)1-fY-fR); glVertex2i(16,0);
00203 glTexCoord2f(fX+fR,(float)1-fY); glVertex2i(16,16);
00204 glEnd();
00205
00206 glTranslatef((float)10,(float)0,(float)0);
00207 glEndList();
00208 }
00209
00210
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
00224 if(m_iLine >= 0)
00225 {
00226 m_iLine--;
00227 }
00228
00229 glEnable(GL_TEXTURE_2D);
00230
00231
00232 glBindTexture(GL_TEXTURE_2D, m_uiTextureId);
00233
00234 glColor3fv(_kColor.GetVectorArray());
00235
00236 glEnable(GL_BLEND);
00237 glBlendFunc(GL_ONE, GL_ONE);
00238
00239 glDisable(GL_DEPTH_TEST);
00240
00241
00242 glMatrixMode(GL_PROJECTION);
00243 glPushMatrix();
00244
00245
00246 glLoadIdentity();
00247
00248 glOrtho(0,pkOption->iWidth,0,pkOption->iHeight,1 ,0);
00249
00250 glMatrixMode(GL_MODELVIEW);
00251 glPushMatrix();
00252
00253 glLoadIdentity();
00254
00255 glTranslatef((float) 0,m_afLine[m_iLine],(float) 0);
00256
00257 glCallLists((GLsizei)strlen(_str),GL_BYTE, _str);
00258 glPopMatrix();
00259
00260
00261 glMatrixMode(GL_PROJECTION);
00262 glPopMatrix();
00263
00264 glMatrixMode(GL_MODELVIEW);
00265
00266 glEnable(GL_DEPTH_TEST);
00267
00268 glColor3f(1, 1, 1);
00269 glDisable(GL_BLEND);
00270 }
00271 else
00272 {
00273
00274 if(m_bNewFrame)
00275 {
00276 m_iNumDraw = 0;
00277
00278 m_iNumDraw++;
00279 m_bNewFrame = false;
00280 }
00281 else
00282 {
00283
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)
00304 {
00305
00306
00307
00308
00309
00310
00311
00312
00313 }
00314 else
00315 {
00316 if(glIsEnabled(GL_TEXTURE_2D))
00317 {
00318 glDisable(GL_TEXTURE_2D);
00319 }
00320 kColor.Set((float)0,(float)1,(float)1);
00321 Print(_strComment, kColor);
00322 }
00323 }
00324
00326 void K3dFontBmp::DrawFps()
00327 {
00328
00329
00330 K3dVector3 kColor;
00331 char acString[200];
00332
00333
00334 TTextureOption *ptOption = m_pGameData->GetTextureOption();
00335
00336 if(ptOption->bMultitexture)
00337 {
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 }
00348 else
00349 {
00350
00351 }
00352
00353 m_iFrame++;
00354
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
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
00382 if(m_bDrawFps)
00383 {
00384 DrawFps();
00385 }
00386
00387 if(m_bDrawCamPos)
00388 {
00389 DrawCameraPosition();
00390 }
00391
00392 if(m_bDrawNumFaces)
00393 {
00394 DrawNumFaces();
00395 }
00396
00397
00398 for(int j=0; j<m_iNumDraw; j++)
00399 {
00400 Print(m_atDraw[j].str, m_atDraw[j].kColor);
00401 }
00402 }
00403
00404 m_bDraw = false;
00405
00406 }