Debian Codegraph @

Oodle/src/CText.h

raw
1 #ifndef _CTEXT_
2 #define _CTEXT_
3
4 #include <string>
5 #include "SDL_ttf.h"
6 using namespace std;
7
8 //! Class to generate a sprite from given input text
9 class CText : public CSprite
10 {
11 public:
12 enum FontStyle
13 {
14 NONE,
15 BOLD = TTF_STYLE_BOLD,
16 ITALIC = TTF_STYLE_ITALIC,
17 UNDERLINE = TTF_STYLE_UNDERLINE
18 };
19
20 CText(const string &txt, CColor c, const string &font, int ptsize, FontStyle s = NONE)
21 : CSprite(ZERO_VECTOR, ZERO_VECTOR), ttfont(NULL), color(c)
22 {
23 if ( TTF_Init() < 0 )
24 {
25 _THROWEX(ex_sdl, "Failed to initialize the SDL_TTF library", "CText", "CText",
26 "txt = '" << txt << "', font = '" << font << "', s = " << s)
27 }
28
29 int renderstyle = static_cast <int> (s);
30
31 // Open the font file with the requested point size
32 ttfont = TTF_OpenFont(font.c_str(), ptsize);
33 if ( ttfont == NULL ) {
34 TTF_Quit();
35 _THROWEX(ex_sdl, "Failed to open font", "CText", "CText",
36 "txt = '" << txt << "', font = '" << font << "', s = " << s)
37 }
38 setText(txt, s);
39 }
40
41 void setText(const string &txt, FontStyle s = NONE)
42 {
43 setText(txt, s, color);
44 }
45
46 void setColor(CColor c)
47 {
48 color = c;
49 }
50
51 void setText(const string &txt, FontStyle s, CColor c)
52 {
53 int renderstyle = static_cast <int> (s);
54
55 TTF_SetFontStyle(ttfont, renderstyle);
56
57 CSurface *tmp = NULL;
58 try
59 {
60 tmp = new CSurface( TTF_RenderText_Blended(ttfont, txt.c_str(), c.getSDLColor()) );
61 }
62 catch (ex_illegalArguments e)
63 {
64 delete tmp;
65 TTF_CloseFont(ttfont);
66 TTF_Quit();
67 _THROWEX(ex_sdl, "Failed to render text to surface", "CText", "CText",
68 "txt = '" << txt << "', s = " << s);
69 }
70 tmp->displayFormatAlpha();
71 addAnimation("static", new CAnimation(tmp));
72 setAnimation("static");
73 }
74
75 ~CText()
76 {
77 TTF_CloseFont(ttfont);
78 TTF_Quit();
79 }
80
81 private:
82 TTF_Font* ttfont;
83 CColor color;
84 };
85
86 #endif