X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ffont.cpp;h=3fd24d8ea0ebde029e262d137dcfaf509b0db5ee;hp=f15e372afaabfaf0946f1aa9ec6093ff80008e66;hb=HEAD;hpb=84bc56b96c21c831104a22e0cbd0f3b72ab5d8c3 diff --git a/source/font.cpp b/source/font.cpp deleted file mode 100644 index f15e372a..00000000 --- a/source/font.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include -#include "font.h" -#include "texture2d.h" - -using namespace std; - -namespace Msp { -namespace GL { - -Font::Font(): - tex(0), - own_tex(false), - default_size(1), - varray((TEXCOORD2, VERTEX2)) -{ } - -void Font::set_texture(const Texture2D &t) -{ - tex=&t; -} - -void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv) -{ - Glyph glyph; - glyph.code=code; - glyph.x1=x1; - glyph.y1=y1; - glyph.x2=x2; - glyph.y2=y2; - glyph.w=w; - glyph.h=h; - glyph.off_x=ox; - glyph.off_y=oy; - glyph.advance=adv; - glyph.index=glyphs.size(); - glyphs.insert(GlyphMap::value_type(code, glyph)); - - RefPtr va_builder=varray.modify(); - create_glyph_vertices(glyph, *va_builder); -} - -float Font::get_string_width(const string &str) const -{ - float x=0; - - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) - x+=get_glyph_advance(static_cast(*i)); - - return x; -} - -float Font::get_string_width(const wstring &str) const -{ - float x=0; - - for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i) - x+=get_glyph_advance(*i); - - return x; -} - -void Font::draw_string(const string &str) const -{ - prepare_render(); - - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) - draw_glyph(static_cast(*i)); - - glPopMatrix(); -} - -void Font::draw_string(const wstring &str) const -{ - prepare_render(); - - for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i) - draw_glyph(*i); - - glPopMatrix(); -} - -Font::~Font() -{ - if(own_tex) - delete tex; -} - -void Font::create_glyph_vertices() -{ - varray.clear(); - RefPtr va_builder=varray.modify(); - - unsigned n=0; - for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n) - { - i->second.index=n; - create_glyph_vertices(i->second, *va_builder); - } -} - -void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder) -{ - va_builder.texcoord(glyph.x1, glyph.y1); - va_builder.vertex(glyph.off_x, glyph.off_y); - va_builder.texcoord(glyph.x2, glyph.y1); - va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y); - va_builder.texcoord(glyph.x2, glyph.y2); - va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h); - va_builder.texcoord(glyph.x1, glyph.y2); - va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h); -} - -void Font::prepare_render() const -{ - tex->bind(); - varray.apply(); - glPushMatrix(); -} - -void Font::draw_glyph(wchar_t code) const -{ - GlyphMap::const_iterator i=glyphs.find(code); - if(i==glyphs.end()) - return; - - glDrawArrays(GL_QUADS, i->second.index*4, 4); - - glTranslatef(i->second.advance, 0, 0); -} - -float Font::get_glyph_advance(wchar_t code) const -{ - GlyphMap::const_iterator i=glyphs.find(code); - if(i==glyphs.end()) - return 0; - - return i->second.advance; -} - - -Font::Loader::Loader(Font &f): - font(f) -{ - add("default_size", &Font::default_size); - add("texture", &Loader::texture); - add("glyph", &Loader::glyph); -} - -Font::Loader::~Loader() -{ - font.create_glyph_vertices(); -} - -void Font::Loader::texture(const string &t) -{ - Texture2D *tex=new Texture2D; - tex->parameter(GL_GENERATE_MIPMAP, 1); - font.tex=tex; - tex->image(t); - font.own_tex=true; -} - -void Font::Loader::glyph(unsigned c) -{ - Glyph gl; - gl.code=c; - load_sub(gl); - font.glyphs.insert(GlyphMap::value_type(c, gl)); -} - - -Font::Glyph::Loader::Loader(Glyph &g): - glyph(g) -{ - add("texcoords", &Loader::texcoords); - add("size", &Loader::size); - add("offset", &Loader::offset); - add("advance", &Glyph::advance); -} - -void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2) -{ - glyph.x1=x1; - glyph.y1=y1; - glyph.x2=x2; - glyph.y2=y2; -} - -void Font::Glyph::Loader::size(float w, float h) -{ - glyph.w=w; - glyph.h=h; -} - -void Font::Glyph::Loader::offset(float x, float y) -{ - glyph.off_x=x; - glyph.off_y=y; -} - -} // namespace GL -} // namespace Msp