Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/BitmapFont.cpp

00001 #include "StdAfx.h" 00002 #include "BitmapFont.h" 00003 00004 CBitmapFont::CBitmapFont() 00005 { 00006 SetEntityType(Entity_BitmapFont); 00007 00008 m_uiNI = 00009 m_uiCharLength = 00010 m_uiCharLength = 00011 m_uiQuadLength = 00012 m_uiQuadLength = 00013 m_uiSpacing = 00014 m_dwFlags = 0; 00015 00016 m_pTexture = NULL; 00017 m_pMaterial = NULL; 00018 00019 m_eFontType = Bitmap_Font; 00020 } 00021 00022 CBitmapFont::~CBitmapFont() 00023 { 00024 00025 } 00026 00027 int CBitmapFont::Create(UINT uiNormalOrItalic, UINT uiCharLength, UINT uiCharHeight, UINT uiQuadLength, UINT uiQuadHeight, UINT uiSpacing) 00028 { 00029 if(m_pTexture == NULL) 00030 return 0; 00031 00032 m_uiQuadLength = uiQuadLength; 00033 m_uiQuadLength = uiQuadHeight; 00034 m_uiCharLength = uiCharLength; 00035 m_uiCharLength = uiCharHeight; 00036 m_uiSpacing = uiSpacing; 00037 m_uiNI = uiNormalOrItalic; 00038 00039 m_uiNumLists = 256; 00040 00041 glPushMatrix(); 00042 float cx; // Holds Our X Character Coord 00043 float cy; // Holds Our Y Character Coord 00044 float quad_x = (float)m_uiQuadLength, 00045 quad_y = (float)m_uiQuadLength; 00046 int lx = m_uiCharLength, ly = m_uiCharLength; 00047 00048 m_uiFontBase = glGenLists(m_uiNumLists); // Creating 256 Display Lists 00049 // glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Font Texture 00050 for (int loop=0; loop<m_uiNumLists; loop++) // Loop Through All 256 Lists 00051 { 00052 cx = float(loop%lx)*0.0625f; // X Position Of Current Character 00053 cy = float(loop/ly)*0.0625f; // Y Position Of Current Character 00054 00055 glNewList(m_uiFontBase+loop, GL_COMPILE); // Start Building A List 00056 m_pTexture->Bind(); 00057 glBegin(GL_QUADS); // Use A Quad For Each Character 00058 glTexCoord2f(cx, 1-cy-0.0625f); // Texture Coord (Bottom Left) 00059 glVertex2i(0, 0); // Vertex Coord (Bottom Left) 00060 glTexCoord2f(cx+0.0625f, 1-cy-0.0625f); // Texture Coord (Bottom Right) 00061 glVertex2i(quad_x, 0); // Vertex Coord (Bottom Right) 00062 glTexCoord2f(cx+0.0625f, 1-cy); // Texture Coord (Top Right) 00063 glVertex2i(quad_x, quad_y); // Vertex Coord (Top Right) 00064 glTexCoord2f(cx, 1-cy); // Texture Coord (Top Left) 00065 glVertex2i(0, quad_y); // Vertex Coord (Top Left) 00066 glEnd(); // Done Building Our Quad (Character) 00067 glTranslated(m_uiQuadLength + m_uiSpacing, 0, 0); // Move To The Right Of The Character 00068 glEndList(); // Done Building The Display List 00069 } // Loop Until All 256 Are Built 00070 glPopMatrix(); 00071 00072 return 1; 00073 } 00074 00075 int CBitmapFont::LoadXML(TiXmlNode *pDataNode, CString strFilename) 00076 { 00077 TiXmlElement* pXMLElement = NULL; 00078 const char *pcValue = NULL; 00079 00080 int iRetValue = CCustomFont::LoadXML(pDataNode, strFilename); // LoadXML the data of derivative 00081 if(iRetValue !=1) 00082 return iRetValue; 00083 00084 pXMLElement = m_pXMLElement; 00085 00086 if(stricmp(pXMLElement->pcValue(), "Font") !=0) 00087 return 0; 00088 00089 int Color[3] = {0, 0, 0}; 00090 pcValue = pXMLElement->Attribute("Type"); 00091 if(pcValue !=NULL) 00092 { 00093 if(stricmp(pcValue, "bitmap") !=0) 00094 return -1; // Not bitmap font! 00095 00096 pcValue = NULL; 00097 } 00098 00099 pcValue = pXMLElement->Attribute("QuadLength"); 00100 if(pcValue !=NULL) 00101 { 00102 m_uiQuadLength = atoi(pcValue); 00103 pcValue = NULL; 00104 } 00105 00106 pcValue = pXMLElement->Attribute("QuadHeight"); 00107 if(pcValue !=NULL) 00108 { 00109 m_uiQuadHeight = atoi(pcValue); 00110 pcValue = NULL; 00111 } 00112 00113 pcValue = pXMLElement->Attribute("CharLength"); 00114 if(pcValue !=NULL) 00115 { 00116 m_uiCharLength = atoi(pcValue); 00117 pcValue = NULL; 00118 } 00119 00120 pcValue = pXMLElement->Attribute("CharHeight"); 00121 if(pcValue !=NULL) 00122 { 00123 m_uiCharHeight = atoi(pcValue); 00124 pcValue = NULL; 00125 } 00126 00127 pcValue = pXMLElement->Attribute("NI"); 00128 if(pcValue !=NULL) 00129 { 00130 m_uiNI = atoi(pcValue); 00131 pcValue = NULL; 00132 } 00133 00134 pcValue = pXMLElement->Attribute("Spacing"); 00135 if(pcValue !=NULL) 00136 { 00137 m_uiSpacing = atoi(pcValue); 00138 pcValue = NULL; 00139 } 00140 00141 m_pTexture = (CTexture *)CTextureManager::GetSingleton().LoadResource(pXMLElement, ""); 00142 m_pMaterial = (CMaterial *)CMaterialManager::GetSingleton().LoadResource(pXMLElement, ""); 00143 00144 return Create(m_uiNI, m_uiCharLength, m_uiCharHeight, m_uiQuadLength, m_uiQuadHeight, m_uiSpacing); 00145 } 00146 00147 int CBitmapFont::SaveXML(TiXmlNode *pDataNode, CString strFilename) 00148 { 00149 TiXmlElement *pSaveElement = NULL; 00150 TiXmlDocument Doc; 00151 00152 if(pDataNode !=NULL) 00153 pSaveElement = pDataNode->ToElement(); 00154 else if(m_pXMLElement !=NULL) 00155 pSaveElement = m_pXMLElement; 00156 else 00157 pSaveElement = new TiXmlElement("Font"); 00158 00159 char pcValue[256] = ""; 00160 00161 00162 if(m_eFontType == Bitmap_Font) 00163 pSaveElement->SetAttribute("Type", "Bitmap"); 00164 00165 sprintf(pcValue, "%d", m_uiQuadLength); 00166 pSaveElement->SetAttribute("uiQuadLength", pcValue); 00167 memset(pcValue, 0, sizeof(pcValue)); 00168 00169 sprintf(pcValue, "%d", m_uiQuadHeight); 00170 pSaveElement->SetAttribute("uiQuadHeight", pcValue); 00171 memset(pcValue, 0, sizeof(pcValue)); 00172 00173 sprintf(pcValue, "%d", m_uiCharLength); 00174 pSaveElement->SetAttribute("uiCharLength", pcValue); 00175 memset(pcValue, 0, sizeof(pcValue)); 00176 00177 sprintf(pcValue, "%d", m_uiCharHeight); 00178 pSaveElement->SetAttribute("uiCharHeight", pcValue); 00179 memset(pcValue, 0, sizeof(pcValue)); 00180 00181 sprintf(pcValue, "%d", m_uiNI); 00182 pSaveElement->SetAttribute("NI", pcValue); 00183 memset(pcValue, 0, sizeof(pcValue)); 00184 00185 sprintf(pcValue, "%d", m_uiSpacing); 00186 pSaveElement->SetAttribute("uiSpacing", pcValue); 00187 memset(pcValue, 0, sizeof(pcValue)); 00188 00189 if(m_pTexture !=NULL) 00190 pSaveElement->SetAttribute("TexByName", m_pTexture->GetName()); 00191 00192 if(m_pMaterial !=NULL) 00193 pSaveElement->SetAttribute("MatByName", m_pMaterial->GetName()); 00194 00195 if(GetName() !="") 00196 pSaveElement->SetAttribute("strName", GetName().GetBuffer()); 00197 00198 if(pDataNode == NULL) 00199 { 00200 if(strFilename == "") 00201 return 0; 00202 00203 Doc.InsertEndChild(*pSaveElement); 00204 SAFEDEL(pSaveElement) 00205 return Doc.SaveFile(strFilename.GetBuffer()); 00206 } 00207 else 00208 { 00209 if(strFilename == "") 00210 return 1; 00211 00212 Doc.InsertEndChild(*pSaveElement); 00213 return Doc.SaveFile(strFilename.GetBuffer()); 00214 } 00215 00216 return 0; 00217 } 00218 00219 void CBitmapFont::Destroy() 00220 { 00221 00222 } 00223 00224 void CBitmapFont::Draw(CString Format, ...) 00225 { 00226 if(m_pTexture == NULL) 00227 return; 00228 00229 if (Format == "") // If There's No Text 00230 return; // Do Nothing 00231 00232 char Text[256]; // Holds Our CString 00233 va_list ap; // Pointer To List Of Arguments 00234 00235 va_start(ap, Format); // Parses The CString For Variables 00236 vsprintf(Text, Format.GetBuffer(), ap); // And Converts Symbols To Actual Numbers 00237 va_end(ap); // Results Are Stored In Text 00238 00239 glPushMatrix(); 00240 00241 m_pTexture->Bind(); 00242 if(m_pMaterial !=NULL) 00243 { 00244 glColor4fv(m_pMaterial->GetAmbient().RGBA); 00245 // m_pMaterial->Bind(); 00246 } 00247 else 00248 { 00249 glDisable(GL_LIGHTING); 00250 glColor3d(255, 255, 255); 00251 } 00252 00253 BeginBlend(); 00254 BeginOrtho(); 00255 00256 glLoadIdentity(); // Reset The Modelview Matrix 00257 00258 glTranslated(m_TextPos.x, m_TextPos.y, 0); 00259 00260 glListBase(m_uiFontBase - 32 + (128*m_uiNI) ); // Choose The Font Set (0 or 1) 00261 glCallLists(strlen(Text), GL_UNSIGNED_BYTE, Text); // Write The Text To The Screen 00262 00263 EndOrtho(); 00264 EndBlend(); 00265 00266 glPopMatrix(); 00267 } 00268 00269 void CBitmapFont::DrawChar(float x, float y, char c) 00270 { 00271 00272 } 00273 00274 void CBitmapFont::SetTextPos(float X, float Y) 00275 { 00276 m_TextPos.x = X; m_TextPos.y = Y; 00277 } 00278 00279 tVERTEX2f& CBitmapFont::GetTextPos() 00280 { 00281 return m_TextPos; 00282 } 00283 00284 UINT CBitmapFont::GetQuadLength() 00285 { 00286 return m_uiQuadLength; 00287 } 00288 00289 UINT CBitmapFont::GetQuadHeight() 00290 { 00291 return m_uiQuadHeight; 00292 } 00293 00294 UINT CBitmapFont::GetCharLength() 00295 { 00296 return m_uiCharLength; 00297 } 00298 00299 UINT CBitmapFont::GetCharHeight() 00300 { 00301 return m_uiCharHeight; 00302 } 00303 00304 UINT CBitmapFont::GetSpacing() 00305 { 00306 return m_uiSpacing; 00307 } 00308 00309 bool CBitmapFont::IsOfType(eEntityType eType) 00310 { 00311 if(eType == Entity_BitmapFont) 00312 return true; 00313 00314 return CCustomFont::IsOfType(eType); 00315 } 00316 00320 void BeginBlend() 00321 { 00322 glDisable(GL_DEPTH_TEST); // Disable Depth Testing 00323 glEnable(GL_BLEND); // Enable Blending 00324 glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Type Of Blending To Perform 00325 glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // Really Nice Perspective Calculations 00326 glHint(GL_POINT_SMOOTH_HINT,GL_NICEST); // Really Nice Point Smoothing 00327 } 00328 00332 void EndBlend() 00333 { 00334 glBlendFunc(GL_ONE,GL_ZERO); // Reset to previous settings 00335 glEnable(GL_DEPTH_TEST); // (restore) 00336 } 00337 00341 void BeginOrtho() 00342 { 00343 int Viewport[4]; 00344 00345 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 00346 glPushMatrix(); // Store The Projection Matrix 00347 glLoadIdentity(); // Reset The Projection Matrix 00348 glGetIntegerv(GL_VIEWPORT, Viewport); 00349 00350 glOrtho(0, Viewport[2], 0, Viewport[3], 0, 100); // Set Up An Ortho Screen 00351 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 00352 glPushMatrix(); // Store The Modelview Matrix 00353 } 00354 00358 void EndOrtho() 00359 { 00360 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 00361 glPopMatrix(); // Restore The Old Projection Matrix 00362 00363 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 00364 glPopMatrix(); // Restore The Old Projection Matrix 00365 }

Generated on Sun Jul 17 21:34:26 2005 for OpenGL GUI by doxygen 1.3.8