1 #include "meshbuilder.h"
11 Text::Text(const Font &f, const Technique *tech, const string &tex_slot):
13 mesh((TEXCOORD2, VERTEX2)),
18 object.set_mesh(&mesh);
20 set_technique(tech, tex_slot);
23 void Text::set_technique(const Technique *tech, const string &ts)
27 const char *tex_slot = ts.c_str();
28 if(!*tex_slot && tech->has_pass(Tag()))
29 if(const Program *shprog = tech->get_pass(Tag()).get_shader_program())
31 if(shprog->get_uniform_location("font_tex")>=0)
32 tex_slot = "font_tex";
33 else if(shprog->get_uniform_location("color_tex")>=0)
34 tex_slot = "color_tex";
35 else if(shprog->get_uniform_location("diffuse_map")>=0)
36 tex_slot = "diffuse_map";
37 else if(shprog->get_uniform_location("base_color_map")>=0)
38 tex_slot = "base_color_map";
44 technique.replace_texture(tex_slot, font.get_texture());
45 object.set_technique(&technique);
48 object.set_technique(tech);
51 object.set_technique(0);
54 void Text::set_text(const string &text, StringCodec::Decoder &dec)
57 width = font.get_string_width(text, dec);
58 MeshBuilder bld(mesh);
59 bld.transform(Matrix::translation(Vector3(-horz_align*width, vert_offset, 0.0f)));
60 font.build_string(text, dec, bld);
69 void Text::set_alignment(HorizontalAlign ha, VerticalAlign va)
74 case LEFT: h = 0.0f; break;
75 case CENTER: h = 0.5f; break;
76 case RIGHT: h = 1.0f; break;
77 default: throw invalid_argument("Text::set_alignment");
83 case DESCENT: v = -font.get_descent(); break;
84 case BASELINE: v = 0.0f; break;
85 case MIDLINE: v = font.get_cap_height()/2; break;
86 case ASCENT: v = font.get_ascent(); break;
87 default: throw invalid_argument("Text::set_alignment");
93 void Text::set_alignment(float h, float v)
95 if(h==horz_align && -v==vert_offset)
98 float horz_adjust = (horz_align-h)*width;
99 float vert_adjust = -v-vert_offset;
103 unsigned pos_offset = mesh.get_vertices().get_format().offset(VERTEX2);
104 unsigned n_vertices = mesh.get_n_vertices();
105 for(unsigned i=0; i<n_vertices; ++i)
107 float *pos = mesh.modify_vertex(i)+pos_offset;
108 pos[0] += horz_adjust;
109 pos[1] += vert_adjust;
113 void Text::render(Renderer &renderer, const Tag &tag) const
115 object.render(renderer, tag);