]> git.tdb.fi Git - libs/gl.git/blob - source/render/text.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / render / text.cpp
1 #include "meshbuilder.h"
2 #include "program.h"
3 #include "renderer.h"
4 #include "technique.h"
5 #include "text.h"
6 #include "texture2d.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 Text::Text(const Font &f, const Technique *tech, Tag tex_slot):
14         ObjectInstance(object),
15         font(f),
16         mesh((TEXCOORD2, VERTEX2))
17 {
18         object.set_mesh(&mesh);
19         if(tech)
20                 set_technique(tech, tex_slot);
21 }
22
23 void Text::set_technique(const Technique *tech, Tag tex_slot)
24 {
25         if(tech)
26         {
27                 if(!tex_slot.id && tech->has_method(Tag()))
28                         if(const Program *shprog = tech->get_method(Tag()).get_shader_program())
29                         {
30                                 if(shprog->get_uniform_location(Tag("font_tex"))>=0)
31                                         tex_slot = "font_tex";
32                                 else if(shprog->get_uniform_location(Tag("color_tex"))>=0)
33                                         tex_slot = "color_tex";
34                                 else if(shprog->get_uniform_location(Tag("diffuse_map"))>=0)
35                                         tex_slot = "diffuse_map";
36                                 else if(shprog->get_uniform_location(Tag("base_color_map"))>=0)
37                                         tex_slot = "base_color_map";
38                         }
39
40                 object.set_technique(tech);
41                 texture_slot = tex_slot;
42         }
43         else
44         {
45                 object.set_technique(0);
46                 texture_slot = Tag();
47         }
48 }
49
50 void Text::set_text(const string &text, StringCodec::Decoder &dec)
51 {
52         clear();
53         width = font.get_string_width(text, dec);
54         MeshBuilder bld(mesh);
55         bld.transform(Matrix::translation(Vector3(-horz_align*width, vert_offset, 0.0f)));
56         font.build_string(text, dec, bld);
57 }
58
59 void Text::clear()
60 {
61         mesh.clear();
62         width = 0;
63 }
64
65 void Text::set_alignment(HorizontalAlign ha, VerticalAlign va)
66 {
67         float h;
68         switch(ha)
69         {
70         case LEFT: h = 0.0f; break;
71         case CENTER: h = 0.5f; break;
72         case RIGHT: h = 1.0f; break;
73         default: throw invalid_argument("Text::set_alignment");
74         }
75
76         float v;
77         switch(va)
78         {
79         case DESCENT: v = -font.get_descent(); break;
80         case BASELINE: v = 0.0f; break;
81         case MIDLINE: v = font.get_cap_height()/2; break;
82         case ASCENT: v = font.get_ascent(); break;
83         default: throw invalid_argument("Text::set_alignment");
84         }
85
86         set_alignment(h, v);
87 }
88
89 void Text::set_alignment(float h, float v)
90 {
91         if(h==horz_align && -v==vert_offset)
92                 return;
93
94         float horz_adjust = (horz_align-h)*width;
95         float vert_adjust = -v-vert_offset;
96         horz_align = h;
97         vert_offset = -v;
98
99         unsigned pos_offset = mesh.get_vertices().get_format().offset(VERTEX2);
100         unsigned n_vertices = mesh.get_n_vertices();
101         for(unsigned i=0; i<n_vertices; ++i)
102         {
103                 float *pos = reinterpret_cast<float *>(mesh.modify_vertex(i)+pos_offset);
104                 pos[0] += horz_adjust;
105                 pos[1] += vert_adjust;
106         }
107 }
108
109 void Text::setup_render(Renderer &renderer, Tag tag) const
110 {
111         ObjectInstance::setup_render(renderer, tag);
112         renderer.set_texture(texture_slot, &font.get_texture());
113 }
114
115 } // namespace GL
116 } // namespace Msp