1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | for some fonts and licenses visit: =http://www.dafont.com/en/font.php= |
---|
16 | |
---|
17 | !! IMPORTANT !! When using ttf fonts clear the license issues prior to |
---|
18 | adding them to orxonox. This is really important, because we do not |
---|
19 | want to offend anyone. |
---|
20 | */ |
---|
21 | |
---|
22 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_FONT |
---|
23 | |
---|
24 | #include "text_engine.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | #include <stdlib.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include "graphics_engine.h" |
---|
33 | #include "resource_manager.h" |
---|
34 | #include "class_list.h" |
---|
35 | |
---|
36 | #include "p_node.h" |
---|
37 | #include "vector.h" |
---|
38 | #include "debug.h" |
---|
39 | #include "list.h" |
---|
40 | |
---|
41 | //////////// |
---|
42 | /// TEXT /// |
---|
43 | //////////// |
---|
44 | /** |
---|
45 | * creates a new Text Element |
---|
46 | * @param font the Font to render this text in |
---|
47 | * @param type The renderType to display this font in |
---|
48 | |
---|
49 | this constructor is private, because the user should initialize |
---|
50 | a text with the TextEngine. |
---|
51 | */ |
---|
52 | Text::Text(Font* font, int type) |
---|
53 | { |
---|
54 | this->setClassID(CL_TEXT, "Text"); |
---|
55 | |
---|
56 | // initialize this Text |
---|
57 | this->font = font; |
---|
58 | this->text = NULL; |
---|
59 | this->setAlignment(TEXT_DEFAULT_ALIGNMENT); |
---|
60 | this->texture = 0; |
---|
61 | this->blending = 1.0f; |
---|
62 | this->setType(type); |
---|
63 | |
---|
64 | this->setText(FONT_DEFAULT_TEXT); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * deletes a Text out of memory |
---|
69 | |
---|
70 | This also ereases the text from the textList of the TextEngine |
---|
71 | */ |
---|
72 | Text::~Text() |
---|
73 | { |
---|
74 | ResourceManager::getInstance()->unload(this->font); |
---|
75 | |
---|
76 | if (this->text) |
---|
77 | delete []this->text; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * sets the Type of this Text |
---|
82 | * @param type the type to set. |
---|
83 | */ |
---|
84 | void Text::setType(int type) |
---|
85 | { |
---|
86 | if (this->font->font) |
---|
87 | this->type = type; |
---|
88 | else |
---|
89 | this->type = TEXT_DYNAMIC; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Sets a new Text to the font |
---|
94 | * @param text the new text to set |
---|
95 | */ |
---|
96 | void Text::setText(const char* text) |
---|
97 | { |
---|
98 | if (this->text) |
---|
99 | delete []this->text; |
---|
100 | if (text != NULL) |
---|
101 | { |
---|
102 | this->text = new char[strlen(text)+1]; |
---|
103 | strcpy(this->text, text); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | this->text = new char[1]; |
---|
108 | *this->text = '\0'; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | // setting up the Text-Width if DYNAMIC |
---|
113 | if (this->type == TEXT_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT) |
---|
114 | { |
---|
115 | Glyph** glyphArray = this->font->getGlyphArray(); |
---|
116 | |
---|
117 | int width = 0; |
---|
118 | char* tmpText = this->text; |
---|
119 | while (*tmpText != '\0') |
---|
120 | { |
---|
121 | if(glyphArray[*tmpText]) |
---|
122 | { |
---|
123 | width += glyphArray[*tmpText]->width; |
---|
124 | } |
---|
125 | tmpText++; |
---|
126 | } |
---|
127 | this->posSize.w = width; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * sets a new color to the font |
---|
133 | * @param r Red |
---|
134 | * @param g Green |
---|
135 | * @param b Blue |
---|
136 | */ |
---|
137 | void Text::setColor(Uint8 r, Uint8 g, Uint8 b) |
---|
138 | { |
---|
139 | this->color.r = r; |
---|
140 | this->color.g = g; |
---|
141 | this->color.b = b; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * creates a texture out of the given parameters |
---|
146 | |
---|
147 | this has to be called every time by the user, to if changes were made. |
---|
148 | this is only for TEXT_STATIC-mode |
---|
149 | */ |
---|
150 | void Text::createTexture() |
---|
151 | { |
---|
152 | SDL_Surface* tmpSurf; |
---|
153 | if (this->texture) |
---|
154 | glDeleteTextures(1, &this->texture); |
---|
155 | if (likely(this->font != NULL)) |
---|
156 | tmpSurf = TTF_RenderText_Blended(this->font->font, |
---|
157 | this->text, |
---|
158 | this->color); |
---|
159 | if (tmpSurf) |
---|
160 | this->texture = loadTexture(tmpSurf, &this->texCoord); |
---|
161 | |
---|
162 | this->posSize.w = tmpSurf->w; |
---|
163 | this->posSize.h = tmpSurf->h; |
---|
164 | SDL_FreeSurface(tmpSurf); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * draws the Text |
---|
169 | */ |
---|
170 | void Text::draw() const |
---|
171 | { |
---|
172 | glPushMatrix(); |
---|
173 | // transform for alignment. |
---|
174 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
175 | glTranslatef(-this->posSize.w, 0, 0); |
---|
176 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
177 | glTranslatef(-this->posSize.w/2, 0, 0); |
---|
178 | |
---|
179 | // drawing this Text. |
---|
180 | // setting the Blending effects |
---|
181 | glColor4f(1.0f,1.0f,1.0f, this->blending); |
---|
182 | glEnable(GL_BLEND); |
---|
183 | glEnable(GL_TEXTURE_2D); |
---|
184 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
185 | |
---|
186 | if(type == TEXT_STATIC) |
---|
187 | { |
---|
188 | glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
189 | glBegin(GL_QUADS); |
---|
190 | |
---|
191 | glTexCoord2f(this->texCoord.minU, this->texCoord.minV); |
---|
192 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
193 | |
---|
194 | glTexCoord2f(this->texCoord.maxU, this->texCoord.minV); |
---|
195 | glVertex2f(this->getAbsCoor2D().x + this->posSize.w, this->getAbsCoor2D().y ); |
---|
196 | |
---|
197 | glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV); |
---|
198 | glVertex2f(this->getAbsCoor2D().x + this->posSize.w, getAbsCoor2D().y + this->posSize.h); |
---|
199 | |
---|
200 | glTexCoord2f(this->texCoord.minU, this->texCoord.maxV); |
---|
201 | glVertex2f(getAbsCoor2D().x, getAbsCoor2D().y + this->posSize.h); |
---|
202 | |
---|
203 | glEnd(); |
---|
204 | } |
---|
205 | else //(if type == TEXT_DYNAMIC) |
---|
206 | { |
---|
207 | Glyph** glyphArray = this->font->getGlyphArray(); |
---|
208 | glBindTexture(GL_TEXTURE_2D, this->font->getFastTextureID()); |
---|
209 | // glEnable(GL_TEXTURE_2D); |
---|
210 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
211 | // glRotatef(this->getAbsDir2D(), 0,0,1); |
---|
212 | |
---|
213 | const char* tmpText = this->text; |
---|
214 | while (*tmpText != '\0') |
---|
215 | { |
---|
216 | if(glyphArray[*tmpText]) |
---|
217 | { |
---|
218 | glCallList(glyphArray[*tmpText]->displayList); |
---|
219 | glTranslatef(glyphArray[*tmpText]->width, 0, 0); |
---|
220 | } |
---|
221 | tmpText++; |
---|
222 | } |
---|
223 | } |
---|
224 | glPopMatrix(); |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * prints out some nice debug information about this text |
---|
229 | */ |
---|
230 | void Text::debug() const |
---|
231 | { |
---|
232 | PRINT(0)("=== TEXT: %s ===\n", this->text); |
---|
233 | if (this->getBindNode()) |
---|
234 | PRINT(0)("is bind to %s; ref=%p\n", this->getBindNode()->getName(), this->getBindNode()); |
---|
235 | PRINT(0)("Relative Position: (%d::%d)\n", this->posSize.x, this->posSize.y); |
---|
236 | PRINT(0)("Color: %d %d %d\n", this->color.r, this->color.g, this->color.b); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | //////////// |
---|
241 | /// UTIL /// |
---|
242 | //////////// |
---|
243 | /** |
---|
244 | * Loads a Font from an SDL_surface into a texture. |
---|
245 | * @param surface The surface to make the texture of |
---|
246 | * @param texCoord The texture coordinates of the 4 corners of the texture |
---|
247 | * @returns the ID of the texture |
---|
248 | */ |
---|
249 | GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord) |
---|
250 | { |
---|
251 | GLuint texture; |
---|
252 | int w, h; |
---|
253 | SDL_Surface *image; |
---|
254 | SDL_Rect area; |
---|
255 | Uint32 saved_flags; |
---|
256 | Uint8 saved_alpha; |
---|
257 | |
---|
258 | /* Use the surface width and height expanded to powers of 2 */ |
---|
259 | w = powerOfTwo(surface->w); |
---|
260 | h = powerOfTwo(surface->h); |
---|
261 | if (texCoord) |
---|
262 | { |
---|
263 | texCoord->minU = 0.0f; |
---|
264 | texCoord->minV = 0.0f; |
---|
265 | texCoord->maxU = (GLfloat)surface->w / w; |
---|
266 | texCoord->maxV = (GLfloat)surface->h / h; |
---|
267 | } |
---|
268 | image = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
269 | w, h, |
---|
270 | 32, |
---|
271 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
272 | 0x000000FF, |
---|
273 | 0x0000FF00, |
---|
274 | 0x00FF0000, |
---|
275 | 0xFF000000 |
---|
276 | #else |
---|
277 | 0xFF000000, |
---|
278 | 0x00FF0000, |
---|
279 | 0x0000FF00, |
---|
280 | 0x000000FF |
---|
281 | #endif |
---|
282 | ); |
---|
283 | if ( image == NULL ) { |
---|
284 | return 0; |
---|
285 | } |
---|
286 | |
---|
287 | /* Save the alpha blending attributes */ |
---|
288 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
289 | saved_alpha = surface->format->alpha; |
---|
290 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
291 | SDL_SetAlpha(surface, 0, 0); |
---|
292 | } |
---|
293 | |
---|
294 | /* Copy the surface into the GL texture image */ |
---|
295 | area.x = 0; |
---|
296 | area.y = 0; |
---|
297 | area.w = surface->w; |
---|
298 | area.h = surface->h; |
---|
299 | SDL_BlitSurface(surface, &area, image, &area); |
---|
300 | |
---|
301 | /* Restore the alpha blending attributes */ |
---|
302 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
303 | SDL_SetAlpha(surface, saved_flags, saved_alpha); |
---|
304 | } |
---|
305 | |
---|
306 | /* Create an OpenGL texture for the image */ |
---|
307 | glGenTextures(1, &texture); |
---|
308 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
309 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
310 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
311 | glTexImage2D(GL_TEXTURE_2D, |
---|
312 | 0, |
---|
313 | GL_RGBA, |
---|
314 | w, h, |
---|
315 | 0, |
---|
316 | GL_RGBA, |
---|
317 | GL_UNSIGNED_BYTE, |
---|
318 | image->pixels); |
---|
319 | SDL_FreeSurface(image); /* No longer needed */ |
---|
320 | |
---|
321 | return texture; |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * Quick utility function for texture creation |
---|
326 | * @param input an integer |
---|
327 | * @returns the next bigger 2^n-integer than input |
---|
328 | */ |
---|
329 | int Text::powerOfTwo(int input) |
---|
330 | { |
---|
331 | int value = 1; |
---|
332 | |
---|
333 | while ( value < input ) { |
---|
334 | value <<= 1; |
---|
335 | } |
---|
336 | return value; |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | //////////// |
---|
341 | /// FONT /// |
---|
342 | //////////// |
---|
343 | /** |
---|
344 | * constructs a Font |
---|
345 | * @param fontFile the File to load the font from |
---|
346 | * @param fontSize the Size of the Font in Pixels |
---|
347 | * @param r Red value of the Font. |
---|
348 | * @param g Green value of the Font. |
---|
349 | * @param b Blue value of the Font. |
---|
350 | */ |
---|
351 | Font::Font(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b) |
---|
352 | { |
---|
353 | this->setClassID(CL_FONT, "Font"); |
---|
354 | // setting default values. |
---|
355 | this->font = NULL; |
---|
356 | this->fontFile = NULL; |
---|
357 | this->glyphArray = NULL; |
---|
358 | this->fastTextureID = 0; |
---|
359 | |
---|
360 | this->setSize(fontSize); |
---|
361 | |
---|
362 | this->setFont(fontFile); |
---|
363 | |
---|
364 | this->setStyle("c");//TTF_STYLE_NORMAL); |
---|
365 | |
---|
366 | this->setFastColor(r, g, b); |
---|
367 | |
---|
368 | this->fastTextureID = this->createFastTexture(); |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * destructs a font |
---|
373 | */ |
---|
374 | Font::~Font() |
---|
375 | { |
---|
376 | // deleting the List of all Texts |
---|
377 | |
---|
378 | // deleting all Glyphs |
---|
379 | if (this->glyphArray) |
---|
380 | { |
---|
381 | for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
382 | delete this->glyphArray[i]; |
---|
383 | delete []this->glyphArray; |
---|
384 | } |
---|
385 | |
---|
386 | // erease this font out of the memory. |
---|
387 | if (likely(this->font != NULL)) |
---|
388 | TTF_CloseFont(this->font); |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * sets The Font. |
---|
393 | * @param fontFile The file containing the font. |
---|
394 | * @returns true if loaded, false if something went wrong, or if a font was loaded before. |
---|
395 | */ |
---|
396 | bool Font::setFont(const char* fontFile) |
---|
397 | { |
---|
398 | if (!this->fontFile) |
---|
399 | { |
---|
400 | this->setName(fontFile); |
---|
401 | this->fontFile = new char[strlen(fontFile)+1]; |
---|
402 | strcpy(this->fontFile, fontFile); |
---|
403 | |
---|
404 | this->font = TTF_OpenFont(this->fontFile, this->fontSize); |
---|
405 | if(!this->font) |
---|
406 | { |
---|
407 | PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError()); |
---|
408 | return false; |
---|
409 | } |
---|
410 | else |
---|
411 | return true; |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | PRINTF(2)("Font already initialized, unable to change it now.\n"); |
---|
416 | return false; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * sets a specific renderStyle |
---|
422 | * @param renderStyle the Style to render: a char-array containing: |
---|
423 | i: italic, b: bold, u, underline |
---|
424 | */ |
---|
425 | void Font::setStyle(const char* renderStyle) |
---|
426 | { |
---|
427 | this->renderStyle = TTF_STYLE_NORMAL; |
---|
428 | |
---|
429 | for (int i = 0; i < strlen(renderStyle); i++) |
---|
430 | if (strncmp(renderStyle+i, "b", 1) == 0) |
---|
431 | this->renderStyle |= TTF_STYLE_BOLD; |
---|
432 | else if (strncmp(renderStyle+i, "i", 1) == 0) |
---|
433 | this->renderStyle |= TTF_STYLE_ITALIC; |
---|
434 | else if (strncmp(renderStyle+i, "u", 1) == 0) |
---|
435 | this->renderStyle |= TTF_STYLE_UNDERLINE; |
---|
436 | |
---|
437 | if (likely(this->font != NULL)) |
---|
438 | TTF_SetFontStyle(this->font, this->renderStyle); |
---|
439 | else |
---|
440 | PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); |
---|
441 | } |
---|
442 | |
---|
443 | /** |
---|
444 | * Sets a new Size to the font |
---|
445 | * @param fontSize The new Size in pixels. |
---|
446 | */ |
---|
447 | void Font::setSize(unsigned int fontSize) |
---|
448 | { |
---|
449 | this->fontSize = fontSize; |
---|
450 | } |
---|
451 | |
---|
452 | /** |
---|
453 | * sets a new color to the font |
---|
454 | * @param r Red |
---|
455 | * @param g Green |
---|
456 | * @param b Blue |
---|
457 | */ |
---|
458 | void Font::setFastColor(Uint8 r, Uint8 g, Uint8 b) |
---|
459 | { |
---|
460 | this->fastColor.r = r; |
---|
461 | this->fastColor.g = g; |
---|
462 | this->fastColor.b = b; |
---|
463 | } |
---|
464 | |
---|
465 | /** |
---|
466 | * @returns the maximum height of the Font, if the font was initialized, 0 otherwise |
---|
467 | */ |
---|
468 | int Font::getMaxHeight() |
---|
469 | { |
---|
470 | if (likely (this->font != NULL)) |
---|
471 | return TTF_FontHeight(this->font); |
---|
472 | else |
---|
473 | return 0; |
---|
474 | } |
---|
475 | |
---|
476 | /** |
---|
477 | * @returns the maximum ascent of the Font, if the font was initialized, 0 otherwise |
---|
478 | |
---|
479 | the ascent is the pixels of the font above the baseline |
---|
480 | */ |
---|
481 | int Font::getMaxAscent() |
---|
482 | { |
---|
483 | if (likely(this->font != NULL)) |
---|
484 | return TTF_FontAscent(this->font); |
---|
485 | else |
---|
486 | return 0; |
---|
487 | } |
---|
488 | |
---|
489 | /** |
---|
490 | * @returns the maximum descent of the Font, if the font was initialized, 0 otherwise |
---|
491 | |
---|
492 | the descent is the pixels of the font below the baseline |
---|
493 | */ |
---|
494 | int Font::getMaxDescent() |
---|
495 | { |
---|
496 | if (likely(this->font != NULL)) |
---|
497 | return TTF_FontDescent(this->font); |
---|
498 | else |
---|
499 | return 0; |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | * @param character The character to get info about. |
---|
504 | * @returns a Glyph struct of a character. This Glyph is a pointer, |
---|
505 | and MUST be deleted by the user.. |
---|
506 | |
---|
507 | This only works for horizontal fonts. see |
---|
508 | http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html |
---|
509 | for more info about vertical Fonts |
---|
510 | */ |
---|
511 | Glyph* Font::getGlyphMetrics(Uint16 character) |
---|
512 | { |
---|
513 | Glyph* rg = new Glyph; |
---|
514 | rg->character = character; |
---|
515 | if (likely (this->font!= NULL)) |
---|
516 | TTF_GlyphMetrics(this->font, rg->character, |
---|
517 | &rg->minX, &rg->maxX, |
---|
518 | &rg->minY, &rg->maxY, |
---|
519 | &rg->advance); |
---|
520 | rg->height = rg->maxY - rg->minY; |
---|
521 | rg->width = rg->maxX - rg->minX; |
---|
522 | rg->bearingX = (rg->advance - rg->width) / 2; |
---|
523 | rg->bearingY = rg->maxY; |
---|
524 | return rg; |
---|
525 | } |
---|
526 | |
---|
527 | GLuint Font::createFastTexture() |
---|
528 | { |
---|
529 | /* interesting GLYPHS: |
---|
530 | * 32: space |
---|
531 | * 33-47: Special Characters. |
---|
532 | * 48-57: 0-9 |
---|
533 | * 58-63: some more special chars (minor) |
---|
534 | * 65-90: A-Z |
---|
535 | * 97-122: a-z |
---|
536 | */ |
---|
537 | int numberOfGlyphs = 91; |
---|
538 | |
---|
539 | this->initGlyphs(32, numberOfGlyphs); |
---|
540 | this->glyphArray[32]->width = fontSize/3; //!< @todo find out the real size of a Space |
---|
541 | |
---|
542 | int rectSize = this->findOptimalFastTextureSize(); |
---|
543 | |
---|
544 | // setting default values. (maybe not needed afterwards) |
---|
545 | SDL_Color tmpColor; tmpColor.r = tmpColor.g = tmpColor.b = 0; |
---|
546 | // Surface definition. |
---|
547 | SDL_Rect tmpRect; // this represents a Rectangle for blitting. |
---|
548 | SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
549 | rectSize, rectSize, |
---|
550 | 32, |
---|
551 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
552 | 0x000000FF, |
---|
553 | 0x0000FF00, |
---|
554 | 0x00FF0000, |
---|
555 | 0xFF000000 |
---|
556 | #else |
---|
557 | 0xFF000000, |
---|
558 | 0x00FF0000, |
---|
559 | 0x0000FF00, |
---|
560 | 0x000000FF |
---|
561 | #endif |
---|
562 | ); |
---|
563 | tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h; |
---|
564 | SDL_SetClipRect(tmpSurf, &tmpRect); |
---|
565 | int maxLineHeight = 0; |
---|
566 | |
---|
567 | // all the interessting Glyphs |
---|
568 | for (int i = 0; i <= 127; i++) |
---|
569 | { |
---|
570 | SDL_Surface* glyphSurf = NULL; |
---|
571 | Glyph* tmpGlyph; |
---|
572 | |
---|
573 | if (tmpGlyph = this->glyphArray[i]) |
---|
574 | { |
---|
575 | if (tmpGlyph->height > maxLineHeight) |
---|
576 | maxLineHeight = tmpGlyph->height; |
---|
577 | |
---|
578 | if (tmpRect.x+tmpGlyph->width > tmpSurf->w) |
---|
579 | { |
---|
580 | tmpRect.x = 0; |
---|
581 | tmpRect.y = tmpRect.y + maxLineHeight + 1; |
---|
582 | maxLineHeight = 0; |
---|
583 | } |
---|
584 | if (tmpRect.y + maxLineHeight > tmpSurf->h) |
---|
585 | { |
---|
586 | PRINTF(1)("Protection, so font cannot write over the boundraries error (this should not heappen\n"); |
---|
587 | break; |
---|
588 | } |
---|
589 | // reading in the new Glyph |
---|
590 | if (likely(this->font != NULL)) |
---|
591 | glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->fastColor); |
---|
592 | if( glyphSurf != NULL ) |
---|
593 | { |
---|
594 | |
---|
595 | SDL_SetAlpha(glyphSurf, 0, 0); |
---|
596 | |
---|
597 | SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect); |
---|
598 | TexCoord tmpTexCoord; |
---|
599 | tmpTexCoord.minU = (float)tmpRect.x/(float)tmpSurf->w; |
---|
600 | tmpTexCoord.maxU = (float)(tmpRect.x +1 +tmpGlyph->width)/(float)tmpSurf->w; |
---|
601 | tmpTexCoord.minV = (float)tmpRect.y/(float)tmpSurf->w; |
---|
602 | tmpTexCoord.maxV = (float)(tmpRect.y+tmpGlyph->height)/(float)tmpSurf->w; |
---|
603 | tmpGlyph->displayList = glGenLists(1); |
---|
604 | |
---|
605 | glNewList(tmpGlyph->displayList, GL_COMPILE); |
---|
606 | glBegin(GL_QUADS); |
---|
607 | glTexCoord2f(tmpTexCoord.minU, tmpTexCoord.minV); |
---|
608 | glVertex2d(0, 0); |
---|
609 | glTexCoord2f(tmpTexCoord.minU, tmpTexCoord.maxV); |
---|
610 | glVertex2d(0, tmpGlyph->height); |
---|
611 | glTexCoord2f(tmpTexCoord.maxU, tmpTexCoord.maxV); |
---|
612 | glVertex2d(tmpGlyph->width, tmpGlyph->height); |
---|
613 | glTexCoord2f(tmpTexCoord.maxU, tmpTexCoord.minV); |
---|
614 | glVertex2d(tmpGlyph->width, 0); |
---|
615 | glEnd(); |
---|
616 | glEndList(); |
---|
617 | SDL_FreeSurface(glyphSurf); |
---|
618 | |
---|
619 | tmpRect.x += tmpGlyph->width + 2; |
---|
620 | |
---|
621 | // Outputting Glyphs to BMP-files. |
---|
622 | /* |
---|
623 | char outname[64]; |
---|
624 | if (i < 10) |
---|
625 | sprintf( outname, "glyph-00%d.bmp", i ); |
---|
626 | else if (i <100) |
---|
627 | sprintf( outname, "glyph-0%d.bmp", i ); |
---|
628 | else |
---|
629 | sprintf( outname, "glyph-%d.bmp", i ); |
---|
630 | SDL_SaveBMP(tmpSurf, outname); |
---|
631 | */ |
---|
632 | } |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | GLuint texture; |
---|
637 | glGenTextures(1, &texture); |
---|
638 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
639 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
640 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
641 | glTexImage2D(GL_TEXTURE_2D, |
---|
642 | 0, |
---|
643 | GL_RGBA, |
---|
644 | tmpSurf->w, tmpSurf->h, |
---|
645 | 0, |
---|
646 | GL_RGBA, |
---|
647 | GL_UNSIGNED_BYTE, |
---|
648 | tmpSurf->pixels); |
---|
649 | SDL_FreeSurface(tmpSurf); |
---|
650 | return texture; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * stores Glyph Metrics in an Array. |
---|
655 | * @param from The Glyph to start from. |
---|
656 | * @param count The number of Glyphs to start From. |
---|
657 | */ |
---|
658 | void Font::initGlyphs(Uint16 from, Uint16 count) |
---|
659 | { |
---|
660 | /* initialize the Array, and set all its entries to NULL |
---|
661 | * only if the Glyph-array has not been initialized |
---|
662 | */ |
---|
663 | if (!this->glyphArray) |
---|
664 | { |
---|
665 | this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; |
---|
666 | for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
667 | this->glyphArray[i] = NULL; |
---|
668 | } |
---|
669 | |
---|
670 | Uint16 lastGlyph = from + count; |
---|
671 | |
---|
672 | for (int i = from; i <= lastGlyph; i++) |
---|
673 | { |
---|
674 | // setting up all the Glyphs we like. |
---|
675 | glyphArray[i] = getGlyphMetrics(i); |
---|
676 | } |
---|
677 | return; |
---|
678 | } |
---|
679 | |
---|
680 | /** |
---|
681 | * @returns the optimal size to use as the texture size |
---|
682 | |
---|
683 | @todo: this algorithm can be a lot more faster, althought it does |
---|
684 | not really matter within the init-context, and 128 glyphs. |
---|
685 | |
---|
686 | This function searches for a 2^n sizes texture-size, this is for |
---|
687 | openGL-version < 1.2 compatibility ( and because it is realy easy like this :)) |
---|
688 | */ |
---|
689 | int Font::findOptimalFastTextureSize() |
---|
690 | { |
---|
691 | int i; |
---|
692 | int x,y; // the counters |
---|
693 | int maxLineHeight; |
---|
694 | int size = 32; // starting Value, we have to start somewhere 32 seems reasonable. |
---|
695 | bool sizeOK = false; |
---|
696 | Glyph* tmpGlyph; |
---|
697 | |
---|
698 | while (!sizeOK) |
---|
699 | { |
---|
700 | x = 0; y = 0; |
---|
701 | maxLineHeight = 0; |
---|
702 | for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
703 | { |
---|
704 | if(tmpGlyph = this->glyphArray[i]) |
---|
705 | { |
---|
706 | // getting the height of the highest Glyph in the Line. |
---|
707 | if (tmpGlyph->height > maxLineHeight) |
---|
708 | maxLineHeight = tmpGlyph->height; |
---|
709 | |
---|
710 | if (x + tmpGlyph->width > size) |
---|
711 | { |
---|
712 | x = 0; |
---|
713 | y = y + maxLineHeight; |
---|
714 | maxLineHeight = 0; |
---|
715 | } |
---|
716 | if (y + maxLineHeight + 1 > size) |
---|
717 | break; |
---|
718 | x += tmpGlyph->width + 1; |
---|
719 | |
---|
720 | } |
---|
721 | } |
---|
722 | if (i == FONT_HIGHEST_KNOWN_CHAR) |
---|
723 | sizeOK = true; |
---|
724 | else |
---|
725 | size *= 2; |
---|
726 | } |
---|
727 | return size; |
---|
728 | } |
---|
729 | |
---|
730 | |
---|
731 | /** |
---|
732 | * a simple function to get some interesting information about this class |
---|
733 | */ |
---|
734 | void Font::debug() |
---|
735 | { |
---|
736 | // print the loaded font's style |
---|
737 | int style; |
---|
738 | if (likely(this->font != NULL)) |
---|
739 | style = TTF_GetFontStyle(this->font); |
---|
740 | PRINTF(0)("The font style is:"); |
---|
741 | if(style==TTF_STYLE_NORMAL) |
---|
742 | PRINTF(0)(" normal"); |
---|
743 | else { |
---|
744 | if(style&TTF_STYLE_BOLD) |
---|
745 | PRINTF(0)(" bold"); |
---|
746 | if(style&TTF_STYLE_ITALIC) |
---|
747 | PRINTF(0)(" italic"); |
---|
748 | if(style&TTF_STYLE_UNDERLINE) |
---|
749 | PRINTF(0)(" underline"); |
---|
750 | } |
---|
751 | PRINTF(0)("\n"); |
---|
752 | } |
---|
753 | |
---|
754 | |
---|
755 | /////////////////// |
---|
756 | /// TEXT-ENGINE /// |
---|
757 | /////////////////// |
---|
758 | /** |
---|
759 | * standard constructor |
---|
760 | */ |
---|
761 | TextEngine::TextEngine () |
---|
762 | { |
---|
763 | this->setClassID(CL_TEXT_ENGINE, "TextEngine"); |
---|
764 | this->setName("TextEngine"); |
---|
765 | this->enableFonts(); |
---|
766 | } |
---|
767 | |
---|
768 | /** |
---|
769 | * the singleton reference to this class |
---|
770 | */ |
---|
771 | TextEngine* TextEngine::singletonRef = NULL; |
---|
772 | |
---|
773 | /** |
---|
774 | * standard deconstructor |
---|
775 | |
---|
776 | */ |
---|
777 | TextEngine::~TextEngine () |
---|
778 | { |
---|
779 | this->disableFonts(); |
---|
780 | |
---|
781 | TextEngine::singletonRef = NULL; |
---|
782 | } |
---|
783 | |
---|
784 | /** |
---|
785 | * function to enable TTF_Fonts |
---|
786 | */ |
---|
787 | void TextEngine::enableFonts() |
---|
788 | { |
---|
789 | if (!TTF_WasInit()) |
---|
790 | { |
---|
791 | if(TTF_Init()==-1) |
---|
792 | PRINTF(1)("TTF_Init: %s\n", TTF_GetError()); |
---|
793 | |
---|
794 | TextEngine::checkVersion(); |
---|
795 | } |
---|
796 | else |
---|
797 | PRINTF(4)("Fonts already initialized\n"); |
---|
798 | } |
---|
799 | |
---|
800 | /** |
---|
801 | * function to disable TTF_fonts |
---|
802 | */ |
---|
803 | void TextEngine::disableFonts() |
---|
804 | { |
---|
805 | if (TTF_WasInit()) |
---|
806 | { |
---|
807 | TTF_Quit(); |
---|
808 | } |
---|
809 | else |
---|
810 | PRINTF(4)("Fonts were not initialized.\n"); |
---|
811 | } |
---|
812 | |
---|
813 | /** |
---|
814 | * creates a new Text with a certain font. |
---|
815 | @see Font::Font |
---|
816 | @see Text::Text |
---|
817 | */ |
---|
818 | Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, int textType, Uint8 r, Uint8 g, Uint8 b) |
---|
819 | { |
---|
820 | Font* tmpFont; |
---|
821 | Text* newText; |
---|
822 | Vector tmpVec; |
---|
823 | |
---|
824 | tmpVec = Vector(r, g, b); |
---|
825 | tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, &fontSize, &tmpVec); |
---|
826 | if (!tmpFont) |
---|
827 | { |
---|
828 | PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile); |
---|
829 | return NULL; |
---|
830 | } |
---|
831 | |
---|
832 | return new Text(tmpFont, TEXT_DYNAMIC); |
---|
833 | } |
---|
834 | |
---|
835 | /** |
---|
836 | * outputs some nice Debug information |
---|
837 | |
---|
838 | @todo there should also be something outputted about Font |
---|
839 | */ |
---|
840 | void TextEngine::debug() const |
---|
841 | { |
---|
842 | PRINT(0)("+-------------------------------+\n"); |
---|
843 | PRINT(0)("+ TEXT ENGINE DEBUG INFORMATION +\n"); |
---|
844 | PRINT(0)("+-------------------------------+\n"); |
---|
845 | PRINT(0)("Reference: %p; Text Counts: %d\n", this, ClassList::getList(CL_TEXT)->getSize()); |
---|
846 | |
---|
847 | tIterator<BaseObject>* textIterator = ClassList::getList(CL_TEXT)->getIterator(); |
---|
848 | Text* text = dynamic_cast<Text*>(textIterator->nextElement()); |
---|
849 | while( text != NULL) |
---|
850 | { |
---|
851 | text->debug(); |
---|
852 | text = dynamic_cast<Text*>(textIterator->nextElement()); |
---|
853 | } |
---|
854 | delete textIterator; |
---|
855 | PRINT(0)("+---------------------------TE--+\n"); |
---|
856 | } |
---|
857 | |
---|
858 | |
---|
859 | /** |
---|
860 | * checks if the compiled version and the local version of SDL_ttf match. |
---|
861 | * @returns true if match, false otherwise |
---|
862 | */ |
---|
863 | bool TextEngine::checkVersion() |
---|
864 | { |
---|
865 | SDL_version compile_version; |
---|
866 | SDL_version link_version; |
---|
867 | TTF_VERSION(&compile_version); |
---|
868 | link_version = *TTF_Linked_Version(); |
---|
869 | |
---|
870 | if (compile_version.major == link_version.major && |
---|
871 | compile_version.minor == link_version.minor && |
---|
872 | compile_version.patch == link_version.patch) |
---|
873 | { |
---|
874 | return true; |
---|
875 | } |
---|
876 | else |
---|
877 | { |
---|
878 | PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n", |
---|
879 | compile_version.major, |
---|
880 | compile_version.minor, |
---|
881 | compile_version.patch); |
---|
882 | |
---|
883 | PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n", |
---|
884 | link_version.major, |
---|
885 | link_version.minor, |
---|
886 | link_version.patch); |
---|
887 | return false; |
---|
888 | } |
---|
889 | } |
---|