| 1 | // Copyright 2005 by Anthony Liekens [email protected] |
| 2 | |
| 3 | #include "fonts.h" |
| 4 | |
| 5 | #include "canvas.h" |
| 6 | |
| 7 | using namespace std; |
| 8 | |
| 9 | Font::Font( char* fileName, int size ) { |
| 10 | std::string s = PKG_DATA_DIR; |
| 11 | s += fileName; |
| 12 | |
| 13 | if (font = TTF_OpenFont (s.c_str(), size)) { |
| 14 | TTF_SetFontStyle( font, TTF_STYLE_NORMAL ); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | s = "data"; |
| 19 | #ifdef WINDOWS |
| 20 | s += "\\"; |
| 21 | #else |
| 22 | s += "/"; |
| 23 | #endif |
| 24 | s += fileName; |
| 25 | |
| 26 | if (font = TTF_OpenFont (s.c_str(), size)) { |
| 27 | TTF_SetFontStyle( font, TTF_STYLE_NORMAL ); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | cerr << "Could not load " << size << "pt font " << fileName << " from the following directories: " << endl |
| 32 | << PKG_DATA_DIR << endl |
| 33 | << "data" << endl; |
| 34 | exit( 1 ); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | Font::render( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) { |
| 39 | SDL_Color color = { r, g, b, a }; |
| 40 | SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color ); |
| 41 | if ( textSurface != NULL ) { |
| 42 | SDL_Rect dstrect; |
| 43 | dstrect.x = x; |
| 44 | dstrect.y = y; |
| 45 | dstrect.w = textSurface->w; |
| 46 | dstrect.h = textSurface->h; |
| 47 | Canvas::alphaBlend( textSurface, a ); |
| 48 | SDL_BlitSurface( textSurface, NULL, screen, &dstrect ); |
| 49 | SDL_FreeSurface( textSurface ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | Font::renderCenterJustified( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) { |
| 55 | SDL_Color color = { r, g, b, 0 }; |
| 56 | SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color ); |
| 57 | if ( textSurface != NULL ) { |
| 58 | SDL_Rect dstrect; |
| 59 | dstrect.x = x - textSurface->w / 2; |
| 60 | dstrect.y = y - textSurface->h / 2; |
| 61 | dstrect.w = textSurface->w; |
| 62 | dstrect.h = textSurface->h; |
| 63 | Canvas::alphaBlend( textSurface, a ); |
| 64 | SDL_BlitSurface( textSurface, NULL, screen, &dstrect ); |
| 65 | SDL_FreeSurface( textSurface ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | Font::renderRightJustified( SDL_Surface* screen, int x, int y, const char* text, Uint8 r, Uint8 g, Uint8 b, Uint8 a ) { |
| 71 | SDL_Color color = { r, g, b, 0 }; |
| 72 | SDL_Surface* textSurface = TTF_RenderText_Blended( font, text, color ); |
| 73 | if ( textSurface != NULL ) { |
| 74 | SDL_Rect dstrect; |
| 75 | dstrect.x = x - textSurface->w; |
| 76 | dstrect.y = y; |
| 77 | dstrect.w = textSurface->w; |
| 78 | dstrect.h = textSurface->h; |
| 79 | Canvas::alphaBlend( textSurface, a ); |
| 80 | SDL_BlitSurface( textSurface, NULL, screen, &dstrect ); |
| 81 | SDL_FreeSurface( textSurface ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | Font::getHeight() { |
| 87 | return TTF_FontHeight( font ); |
| 88 | } |