3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "texture2d.h"
23 varray((TEXCOORD2, VERTEX2))
32 void Font::set_texture(const Texture2D &t)
37 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
50 glyph.index=glyphs.size();
51 glyphs.insert(GlyphMap::value_type(code, glyph));
53 RefPtr<VertexArrayBuilder> va_builder=varray.modify();
54 create_glyph_vertices(glyph, *va_builder);
57 float Font::get_string_width(const string &str) const
61 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
62 x+=get_glyph_advance(static_cast<unsigned char>(*i));
67 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
71 for(string::const_iterator i=str.begin(); i!=str.end();)
72 x+=get_glyph_advance(dec.decode_char(str, i));
77 void Font::draw_string(const string &str) const
81 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
82 draw_glyph(static_cast<unsigned char>(*i));
87 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
91 for(string::const_iterator i=str.begin(); i!=str.end();)
92 draw_glyph(dec.decode_char(str, i));
97 void Font::create_glyph_vertices()
100 RefPtr<VertexArrayBuilder> va_builder=varray.modify();
103 for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n)
106 create_glyph_vertices(i->second, *va_builder);
110 void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder)
112 va_builder.texcoord(glyph.x1, glyph.y1);
113 va_builder.vertex(glyph.off_x, glyph.off_y);
114 va_builder.texcoord(glyph.x2, glyph.y1);
115 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y);
116 va_builder.texcoord(glyph.x2, glyph.y2);
117 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
118 va_builder.texcoord(glyph.x1, glyph.y2);
119 va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h);
122 void Font::prepare_render() const
129 void Font::draw_glyph(unsigned code) const
131 GlyphMap::const_iterator i=glyphs.find(code);
135 glDrawArrays(GL_QUADS, i->second.index*4, 4);
137 glTranslatef(i->second.advance, 0, 0);
140 float Font::get_glyph_advance(unsigned code) const
142 GlyphMap::const_iterator i=glyphs.find(code);
146 return i->second.advance;
150 Font::Loader::Loader(Font &f):
153 add("default_size", &Font::default_size);
154 add("ascent", &Font::ascent);
155 add("descent", &Font::descent);
156 add("texture", &Loader::texture);
157 add("glyph", &Loader::glyph);
160 Font::Loader::~Loader()
162 font.create_glyph_vertices();
165 void Font::Loader::texture(const string &t)
167 Texture2D *tex=new Texture2D;
168 tex->parameter(GL_GENERATE_MIPMAP, 1);
174 void Font::Loader::glyph(unsigned c)
179 font.glyphs.insert(GlyphMap::value_type(c, gl));
183 Font::Glyph::Loader::Loader(Glyph &g):
186 add("texcoords", &Loader::texcoords);
187 add("size", &Glyph::w, &Glyph::h);
188 add("offset", &Glyph::off_x, &Glyph::off_y);
189 add("advance", &Glyph::advance);
192 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)