Definition at line 16 of file Font.h.
Public Member Functions | |
Font (const Path &file_ttf, int height) | |
Create new font from file. | |
~Font () | |
int | getHeight () |
int | calcTextWidth (const std::string &text) |
SDL_Surface * | renderText (const std::string &text, const SDL_Color &color) const |
Render text with this color. | |
SDL_Surface * | renderTextOutlined (const std::string &text, const SDL_Color &color, int outlineWidth=1) const |
Render text with black outline around font. | |
Static Public Member Functions | |
static void | init () |
Prepare font rendering. | |
static void | shutdown () |
Deinit font subsystem. |
|
Create new font from file. Initialized TTF_Init when necessary.
Definition at line 25 of file Font.cpp. 00026 { 00027 m_ttfont = TTF_OpenFont(file_ttf.getNative().c_str(), height); 00028 if (!m_ttfont) { 00029 throw TTFException(ExInfo("OpenFont") 00030 .addInfo("file", file_ttf.getNative())); 00031 } 00032 00033 //NOTE: bg color will be set to be transparent 00034 SDL_Color bg = {10, 10, 10, 0}; 00035 m_bg = bg; 00036 }
|
|
Definition at line 38 of file Font.cpp. 00039 { 00040 TTF_CloseFont(m_ttfont); 00041 }
|
|
Definition at line 66 of file Font.cpp. 00067 { 00068 int w; 00069 TTF_SizeUTF8(m_ttfont, text.c_str(), &w, NULL); 00070 return w; 00071 }
|
|
Definition at line 26 of file Font.h. 00026 { return TTF_FontHeight(m_ttfont); }
|
|
Prepare font rendering.
Definition at line 48 of file Font.cpp. 00049 { 00050 if (TTF_Init() < 0) { 00051 throw TTFException(ExInfo("Init")); 00052 } 00053 }
|
|
Render text with this color.
Definition at line 82 of file Font.cpp. 00083 { 00084 const char *content = text.c_str(); 00085 if (text.empty()) { 00086 content = " "; 00087 LOG_WARNING(ExInfo("empty text to render") 00088 .addInfo("r", color.r) 00089 .addInfo("g", color.g) 00090 .addInfo("b", color.b)); 00091 } 00092 00093 SDL_Surface *raw_surface = TTF_RenderUTF8_Shaded(m_ttfont, content, 00094 color, m_bg); 00095 if (!raw_surface) { 00096 throw TTFException(ExInfo("RenderUTF8") 00097 .addInfo("text", text)); 00098 } 00099 00100 //NOTE: at index 0 is bg color 00101 if (SDL_SetColorKey(raw_surface, SDL_SRCCOLORKEY, 0) < 0) { 00102 throw SDLException(ExInfo("SetColorKey")); 00103 } 00104 00105 SDL_Surface *surface = SDL_DisplayFormat(raw_surface); 00106 if (!surface) { 00107 throw SDLException(ExInfo("DisplayFormat")); 00108 } 00109 SDL_FreeSurface(raw_surface); 00110 00111 return surface; 00112 }
|
|
Render text with black outline around font.
Definition at line 122 of file Font.cpp. 00124 { 00125 SDL_Surface *surface = renderText(" " + text + " ", color); 00126 SDL_Color black = {0, 0, 0, 255}; 00127 Outline outline(black, outlineWidth); 00128 00129 outline.drawOnColorKey(surface); 00130 return surface; 00131 }
|
|
Deinit font subsystem.
Definition at line 59 of file Font.cpp. 00060 { 00061 TTF_Quit(); 00062 }
|