1
+ #include < boost/algorithm/string.hpp>
1
2
#include " Text.h"
2
3
#include < boost/lexical_cast.hpp>
3
4
#include " kit/log/log.h"
@@ -16,8 +17,8 @@ Font :: Font(const std::string& fn, ICache* c)
16
17
if (split_point == string::npos)
17
18
split_point = fn.size ();
18
19
else
19
- size = boost::lexical_cast<int >(fn.substr (split_point+1 ));
20
- m_pFont = TTF_OpenFont ((fn.substr (0 ,split_point)).c_str (), size );
20
+ m_Size = boost::lexical_cast<int >(fn.substr (split_point+1 ));
21
+ m_pFont = TTF_OpenFont ((fn.substr (0 ,split_point)).c_str (), m_Size );
21
22
if (!m_pFont){
22
23
K_ERRORf (READ, " font \" %s\" " , fn);
23
24
}
@@ -70,28 +71,61 @@ Text :: Text(const std::shared_ptr<Font>& font):
70
71
71
72
Text :: ~Text ()
72
73
{
73
- if (m_pSurface){
74
- SDL_FreeSurface (m_pSurface);
75
- m_pSurface = nullptr ;
76
- }
74
+ // if(m_pSurface){
75
+ // SDL_FreeSurface(m_pSurface);
76
+ // m_pSurface = nullptr;
77
+ // }
77
78
78
79
deinit ();
79
80
}
80
81
81
82
void Text :: redraw()
82
83
{
83
- if (m_pSurface){
84
- SDL_FreeSurface (m_pSurface);
85
- m_pSurface = nullptr ;
86
- }
84
+ // if(m_pSurface){
85
+ // SDL_FreeSurface(m_pSurface);
86
+ // m_pSurface = nullptr;
87
+ // }
87
88
88
89
SDL_Color color;
89
90
color.r = Uint8 (m_Color.r () * 0xFF );
90
91
color.g = Uint8 (m_Color.g () * 0xFF );
91
92
color.b = Uint8 (m_Color.b () * 0xFF );
92
93
color.a = Uint8 (m_Color.a () * 0xFF );
93
- m_pSurface = TTF_RenderText_Solid (m_pFont->font (),m_Text.c_str (),color);
94
- assert (m_pSurface);
94
+
95
+ vector<string> lines;
96
+ boost::split (lines, m_Text, boost::is_any_of (" \n " ));
97
+
98
+ SDL_Surface* tmp = nullptr ;
99
+ SDL_Rect rect;
100
+
101
+ int width=0 ;
102
+ int lineheight=0 ;
103
+ int height=0 ;
104
+ for (int i=0 ;i<lines.size ();++i){
105
+ int sz;
106
+ TTF_SizeText (m_pFont->font (), lines[i].c_str (), &sz, &lineheight);
107
+ if (sz > width)
108
+ width = sz;
109
+ }
110
+
111
+ for (int i=0 ;i<lines.size ();++i){
112
+ auto surf = TTF_RenderText_Solid (m_pFont->font (), lines[i].c_str (), color);
113
+ if (i == 0 ){
114
+ height = (lineheight + m_pFont->size ()*m_LineSpacing) * lines.size ();
115
+ tmp = SDL_CreateRGBSurface (0 , width, height,
116
+ 32 , 0x000000ff , 0x0000ff00 , 0x00ff0000 , 0xff000000 );
117
+ rect.x = 0 ;
118
+ rect.w = width;
119
+ rect.h = height;
120
+ cout << width << endl;
121
+ cout << height << endl;
122
+ }
123
+ rect.y = i * (lineheight + m_LineSpacing*m_pFont->size ());
124
+ assert (surf);
125
+ SDL_BlitSurface (surf, NULL , tmp, &rect);
126
+ SDL_FreeSurface (surf);
127
+ }
128
+ assert (tmp);
95
129
96
130
m_pTexture = nullptr ;
97
131
@@ -100,34 +134,30 @@ void Text :: redraw()
100
134
GL_TASK_START ()
101
135
glGenTextures (1 , &m_ID);
102
136
glBindTexture (GL_TEXTURE_2D, m_ID);
103
- int mode = GL_RGB;
104
- // if(m_pSurface->format->BytesPerPixel == 4)
105
- mode = GL_RGBA;
137
+ int mode = GL_RGBA;
106
138
107
- SDL_Surface* tmp = SDL_CreateRGBSurface (0 ,m_pSurface->w , m_pSurface->h ,
108
- 32 , 0x000000ff , 0x0000ff00 , 0x00ff0000 , 0xff000000 );
109
- SDL_BlitSurface (m_pSurface, NULL , tmp, NULL );
110
139
glTexImage2D (GL_TEXTURE_2D, 0 , mode, tmp->w , tmp->h ,
111
140
0 , mode, GL_UNSIGNED_BYTE, tmp->pixels );
112
141
113
142
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114
143
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
115
- SDL_FreeSurface (tmp);
116
144
GL_TASK_END ()
117
145
146
+ SDL_FreeSurface (tmp);
147
+
118
148
m_pTexture = make_shared<Texture>(m_ID);
119
149
120
150
glm::vec2 vs,ve;
121
- if (m_Align == LEFT){
151
+ // if(m_Align == LEFT){
122
152
vs = vec2 (0 .0f , 0 .0f );
123
- ve = vec2 (m_pSurface-> w , m_pSurface-> h );
124
- }else if (m_Align == RIGHT){
125
- vs = vec2 (-m_pSurface-> w , -m_pSurface-> h );
126
- ve = vec2 (0 .0f , 0 . 0f );
127
- }else {
128
- vs = vec2 (-m_pSurface-> w / 2 .0f , -m_pSurface-> h / 2 .0f );
129
- ve = vec2 (m_pSurface-> w / 2 .0f , m_pSurface-> h / 2 . 0f );
130
- }
153
+ ve = vec2 (width, height );
154
+ // }else if(m_Align == RIGHT){
155
+ // vs = vec2(-rect.x, 0.0f );
156
+ // ve = vec2(0.0f, rect.y );
157
+ // }else{
158
+ // vs = vec2(-rect.x / 2.0f, 0 .0f);
159
+ // ve = vec2(rect.x / 2.0f, rect.y );
160
+ // }
131
161
m_pMesh = make_shared<Mesh>(
132
162
make_shared<MeshGeometry>(Prefab::quad (vs, ve)),
133
163
vector<shared_ptr<IMeshModifier>>{
0 commit comments