1 //******************************************************************* 2 //glfont2.h -- Header for glfont2.cpp 3 //Copyright (c) 1998-2002 Brad Fish 4 //See glfont.html for terms of use 5 //May 14, 2002 6 //******************************************************************* 7 8 #ifndef GLFONT2_H 9 #define GLFONT2_H 10 11 #if defined(__GNUC__) && (__GNUC__ < 3) 12 #include <pair.h> 13 #endif 14 #include <string> 15 #include <GL/gl.h> 16 #include <GL/glu.h> 17 18 using namespace std; 19 20 //******************************************************************* 21 //GLFont Interface 22 //******************************************************************* 23 24 //glFont class 25 class GLFont 26 { 27 private: 28 29 //glFont character structure 30 typedef struct 31 { 32 float dx, dy; 33 float tx1, ty1; 34 float tx2, ty2; 35 } GLFontChar; 36 37 //glFont header structure 38 struct 39 { 40 int tex; 41 int tex_width, tex_height; 42 int start_char, end_char; 43 GLFontChar *chars; 44 } header; 45 46 public: 47 48 //Constructor 49 GLFont (); 50 51 //Destructor 52 ~GLFont (); 53 54 public: 55 56 //Creates the glFont 57 bool Create (const char *file_name, int tex); 58 bool Create (const std::string &file_name, int tex); 59 60 //Destroys the glFont 61 void Destroy (void); 62 63 //Texture size retrieval methods 64 void GetTexSize (std::pair<int, int> *size); 65 int GetTexWidth (void); 66 int GetTexHeight (void); 67 68 //Character interval retrieval methods 69 void GetCharInterval (std::pair<int, int> *interval); 70 int GetStartChar (void); 71 int GetEndChar (void); 72 73 //Character size retrieval methods 74 void GetCharSize (int c, std::pair<int, int> *size); 75 int GetCharWidth (int c); 76 int GetCharHeight (int c); 77 78 //Calculates the size in pixels of a character array GetStringSize(const T * text,std::pair<int,int> * size)79 template<class T> void GetStringSize (const T *text, 80 std::pair<int, int> *size) 81 { 82 const T *i; 83 GLFontChar *glfont_char; 84 float width; 85 86 //Height is the same for now...might change in future 87 size->second = (int)(header.chars[header.start_char].dy * 88 header.tex_height); 89 90 //Calculate width of string 91 width = 0.0F; 92 for (i = text; *i != (T)'\0'; i++) 93 { 94 //Make sure character is in range 95 if (*i < header.start_char || *i > header.end_char) 96 continue; 97 98 //Get pointer to glFont character 99 glfont_char = &header.chars[*i - header.start_char]; 100 101 //Get width and height 102 width += glfont_char->dx * header.tex_width; 103 } 104 105 //Save width 106 size->first = (int)width; 107 } 108 109 //Template function to calculate size of a std::basic_string GetStringSize(const std::basic_string<T> & text,std::pair<int,int> * size)110 template<class T> void GetStringSize ( 111 const std::basic_string<T> &text, std::pair<int, int> *size) 112 { 113 unsigned int i; 114 T *c; 115 GLFontChar *glfont_char; 116 float width; 117 118 //Height is the same for now...might change in future 119 size->second = (int)(header.chars[header.start_char].dy * 120 header.tex_height); 121 122 //Calculate width of string 123 width = 0.0F; 124 for (i = 0; i < text.size(); i++) 125 { 126 //Make sure character is in range 127 c = text[i]; 128 if (c < header.start_char || c > header.end_char) 129 continue; 130 131 //Get pointer to glFont character 132 glfont_char = &header.chars[c - header.start_char]; 133 134 //Get width and height 135 width += glfont_char->dx * header.tex_width; 136 } 137 138 //Save width 139 size->first = (int)width; 140 } 141 142 //Begins text output with this font 143 void Begin (void); 144 145 //Template function to output a character array DrawString(const T * text,float x,float y)146 template<class T> void DrawString (const T *text, float x, 147 float y) 148 { 149 const T *i; 150 GLFontChar *glfont_char; 151 float width, height; 152 153 //Begin rendering quads 154 glBegin(GL_QUADS); 155 156 //Loop through characters 157 for (i = text; *i != (T)'\0'; i++) 158 { 159 //Make sure character is in range 160 if (*i < header.start_char || *i > header.end_char) 161 continue; 162 163 //Get pointer to glFont character 164 glfont_char = &header.chars[*i - header.start_char]; 165 166 //Get width and height 167 width = glfont_char->dx * header.tex_width; 168 height = glfont_char->dy * header.tex_height; 169 170 //Specify vertices and texture coordinates 171 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 172 glVertex3f(x, y, 0.0F); 173 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 174 glVertex3f(x, y - height, 0.0F); 175 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 176 glVertex3f(x + width, y - height, 0.0F); 177 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 178 glVertex3f(x + width, y, 0.0F); 179 180 //Move to next character 181 x += width; 182 } 183 184 //Stop rendering quads 185 glEnd(); 186 } 187 188 //Template function to draw a std::basic_string DrawString(const std::basic_string<T> & text,float x,float y)189 template<class T> void DrawString ( 190 const std::basic_string<T> &text, float x, float y) 191 { 192 unsigned int i; 193 T c; 194 GLFontChar *glfont_char; 195 float width, height; 196 197 //Begin rendering quads 198 glBegin(GL_QUADS); 199 200 //Loop through characters 201 for (i = 0; i < text.size(); i++) 202 { 203 //Make sure character is in range 204 c = text[i]; 205 if (c < header.start_char || c > header.end_char) 206 continue; 207 208 //Get pointer to glFont character 209 glfont_char = &header.chars[c - header.start_char]; 210 211 //Get width and height 212 width = glfont_char->dx * header.tex_width; 213 height = glfont_char->dy * header.tex_height; 214 215 //Specify vertices and texture coordinates 216 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 217 glVertex3f(x, y, 0.0F); 218 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 219 glVertex3f(x, y - height, 0.0F); 220 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 221 glVertex3f(x + width, y - height, 0.0F); 222 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 223 glVertex3f(x + width, y, 0.0F); 224 225 //Move to next character 226 x += width; 227 } 228 229 //Stop rendering quads 230 glEnd(); 231 } 232 233 //Template function to output a scaled character array DrawString(const T * text,float scalar,float x,float y)234 template<class T> void DrawString (const T *text, float scalar, 235 float x, float y) 236 { 237 const T *i; 238 GLFontChar *glfont_char; 239 float width, height; 240 241 //Begin rendering quads 242 glBegin(GL_QUADS); 243 244 //Loop through characters 245 for (i = text; *i != (T)'\0'; i++) 246 { 247 //Make sure character is in range 248 if (*i < header.start_char || *i > header.end_char) 249 continue; 250 251 //Get pointer to glFont character 252 glfont_char = &header.chars[*i - header.start_char]; 253 254 //Get width and height 255 width = (glfont_char->dx * header.tex_width) * scalar; 256 height = (glfont_char->dy * header.tex_height) * scalar; 257 258 //Specify vertices and texture coordinates 259 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 260 glVertex3f(x, y, 0.0F); 261 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 262 glVertex3f(x, y - height, 0.0F); 263 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 264 glVertex3f(x + width, y - height, 0.0F); 265 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 266 glVertex3f(x + width, y, 0.0F); 267 268 //Move to next character 269 x += width; 270 } 271 272 //Stop rendering quads 273 glEnd(); 274 } 275 276 //Template function to output a scaled std::basic_string DrawString(const std::basic_string<T> & text,float scalar,float x,float y)277 template<class T> void DrawString ( 278 const std::basic_string<T> &text, float scalar, float x, 279 float y) 280 { 281 unsigned int i; 282 T c; 283 GLFontChar *glfont_char; 284 float width, height; 285 286 //Begin rendering quads 287 glBegin(GL_QUADS); 288 289 //Loop through characters 290 for (i = 0; i < text.size(); i++) 291 { 292 //Make sure character is in range 293 c = text[i]; 294 if (c < header.start_char || c > header.end_char) 295 continue; 296 297 //Get pointer to glFont character 298 glfont_char = &header.chars[c - header.start_char]; 299 300 //Get width and height 301 width = (glfont_char->dx * header.tex_width) * scalar; 302 height = (glfont_char->dy * header.tex_height) * scalar; 303 304 //Specify vertices and texture coordinates 305 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 306 glVertex3f(x, y, 0.0F); 307 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 308 glVertex3f(x, y - height, 0.0F); 309 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 310 glVertex3f(x + width, y - height, 0.0F); 311 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 312 glVertex3f(x + width, y, 0.0F); 313 314 //Move to next character 315 x += width; 316 } 317 318 //Stop rendering quads 319 glEnd(); 320 } 321 322 //Template function to output a colored character array DrawString(const T * text,float x,float y,const float * top_color,const float * bottom_color)323 template<class T> void DrawString (const T *text, float x, 324 float y, const float *top_color, 325 const float *bottom_color) 326 { 327 const T *i; 328 GLFontChar *glfont_char; 329 float width, height; 330 331 //Begin rendering quads 332 glBegin(GL_QUADS); 333 334 //Loop through characters 335 for (i = text; *i != '\0'; i++) 336 { 337 //Make sure character is in range 338 if (*i < header.start_char || *i > header.end_char) 339 continue; 340 341 //Get pointer to glFont character 342 glfont_char = &header.chars[*i - header.start_char]; 343 344 //Get width and height 345 width = glfont_char->dx * header.tex_width; 346 height = glfont_char->dy * header.tex_height; 347 348 //Specify colors, vertices, and texture coordinates 349 glColor3fv(top_color); 350 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 351 glVertex3f(x, y, 0.0F); 352 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 353 glVertex3f(x + width, y, 0.0F); 354 glColor3fv(bottom_color); 355 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 356 glVertex3f(x + width, y - height, 0.0F); 357 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 358 glVertex3f(x, y - height, 0.0F); 359 360 //Move to next character 361 x += width; 362 } 363 364 //Stop rendering quads 365 glEnd(); 366 } 367 368 //Template function to output a colored std::basic_string DrawString(const std::basic_string<T> & text,float x,float y,const float * top_color,const float * bottom_color)369 template<class T> void DrawString ( 370 const std::basic_string<T> &text, float x, float y, 371 const float *top_color, const float *bottom_color) 372 { 373 unsigned int i; 374 T c; 375 GLFontChar *glfont_char; 376 float width, height; 377 378 //Begin rendering quads 379 glBegin(GL_QUADS); 380 381 //Loop through characters 382 for (i = 0; i < text.size(); i++) 383 { 384 //Make sure character is in range 385 c = text[i]; 386 if (c < header.start_char || c > header.end_char) 387 continue; 388 389 //Get pointer to glFont character 390 glfont_char = &header.chars[c - header.start_char]; 391 392 //Get width and height 393 width = glfont_char->dx * header.tex_width; 394 height = glfont_char->dy * header.tex_height; 395 396 //Specify colors, vertices, and texture coordinates 397 glColor3fv(top_color); 398 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 399 glVertex3f(x, y, 0.0F); 400 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 401 glVertex3f(x + width, y, 0.0F); 402 glColor3fv(bottom_color); 403 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 404 glVertex3f(x + width, y - height, 0.0F); 405 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 406 glVertex3f(x, y - height, 0.0F); 407 408 //Move to next character 409 x += width; 410 } 411 412 //Stop rendering quads 413 glEnd(); 414 } 415 416 //Template function to output a scaled, colored character array DrawString(const T * text,float scalar,float x,float y,const float * top_color,const float * bottom_color)417 template<class T> void DrawString (const T *text, float scalar, 418 float x, float y, const float *top_color, 419 const float *bottom_color) 420 { 421 const T *i; 422 GLFontChar *glfont_char; 423 float width, height; 424 425 //Begin rendering quads 426 glBegin(GL_QUADS); 427 428 //Loop through characters 429 for (i = text; *i != '\0'; i++) 430 { 431 //Make sure character is in range 432 if (*i < header.start_char || *i > header.end_char) 433 continue; 434 435 //Get pointer to glFont character 436 glfont_char = &header.chars[*i - header.start_char]; 437 438 //Get width and height 439 width = (glfont_char->dx * header.tex_width) * scalar; 440 height = (glfont_char->dy * header.tex_height) * scalar; 441 442 //Specify colors, vertices, and texture coordinates 443 glColor3fv(top_color); 444 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 445 glVertex3f(x, y, 0.0F); 446 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 447 glVertex3f(x + width, y, 0.0F); 448 glColor3fv(bottom_color); 449 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 450 glVertex3f(x + width, y - height, 0.0F); 451 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 452 glVertex3f(x, y - height, 0.0F); 453 454 //Move to next character 455 x += width; 456 } 457 458 //Stop rendering quads 459 glEnd(); 460 } 461 462 //Template function to output a scaled, colored std::basic_string DrawString(const std::basic_string<T> & text,float scalar,float x,float y,const float * top_color,const float * bottom_color)463 template<class T> void DrawString ( 464 const std::basic_string<T> &text, float scalar, float x, 465 float y, const float *top_color, const float *bottom_color) 466 { 467 unsigned int i; 468 T c; 469 GLFontChar *glfont_char; 470 float width, height; 471 472 //Begin rendering quads 473 glBegin(GL_QUADS); 474 475 //Loop through characters 476 for (i = 0; i < text.size(); i++) 477 { 478 //Make sure character is in range 479 c = text[i]; 480 if (c < header.start_char || c > header.end_char) 481 continue; 482 483 //Get pointer to glFont character 484 glfont_char = &header.chars[c - header.start_char]; 485 486 //Get width and height 487 width = (glfont_char->dx * header.tex_width) * scalar; 488 height = (glfont_char->dy * header.tex_height) * scalar; 489 490 //Specify colors, vertices, and texture coordinates 491 glColor3fv(top_color); 492 glTexCoord2f(glfont_char->tx1, glfont_char->ty1); 493 glVertex3f(x, y, 0.0F); 494 glTexCoord2f(glfont_char->tx2, glfont_char->ty1); 495 glVertex3f(x + width, y, 0.0F); 496 glColor3fv(bottom_color); 497 glTexCoord2f(glfont_char->tx2, glfont_char->ty2); 498 glVertex3f(x + width, y - height, 0.0F); 499 glTexCoord2f(glfont_char->tx1, glfont_char->ty2); 500 glVertex3f(x, y - height, 0.0F); 501 502 //Move to next character 503 x += width; 504 } 505 506 //Stop rendering quads 507 glEnd(); 508 } 509 }; 510 511 //******************************************************************* 512 513 #endif 514 515 //End of file 516 517