+++ /dev/null
-#include <GL/gl.h>
-#include "font.h"
-#include "texture2d.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Font::Font():
- tex(0),
- own_tex(false)
-{ }
-
-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 desc, 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.descent=desc;
- glyph.advance=adv;
-}
-
-float Font::get_string_width(const string &str) const
-{
- float x=0;
-
- for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
- {
- GlyphMap::const_iterator j=glyphs.find((unsigned char)*i);
- if(j==glyphs.end())
- continue;
-
- const Glyph &glyph=j->second;
-
- x+=glyph.advance;
- }
-
- return x;
-}
-
-void Font::draw_string(const string &str) const
-{
- float x=0;
- float data[16];
-
- prepare(data);
-
- for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
- draw_glyph((unsigned char)*i, data, x);
-
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-}
-
-Font::~Font()
-{
- if(own_tex)
- delete tex;
-}
-
-void Font::prepare(float *data) const
-{
- tex->bind();
-
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- glTexCoordPointer(2, GL_FLOAT, 4*sizeof(float), data);
- glVertexPointer(2, GL_FLOAT, 4*sizeof(float), data+2);
-}
-
-void Font::draw_glyph(wchar_t code, float *data, float &x) const
-{
- GlyphMap::const_iterator i=glyphs.find(code);
- if(i==glyphs.end())
- return;
-
- const Glyph &glyph=i->second;
-
- data[0]=data[12]=glyph.x1;
- data[1]=data[5]=glyph.y1;
- data[4]=data[8]=glyph.x2;
- data[9]=data[13]=glyph.y2;
-
- data[2]=data[14]=x;
- data[3]=data[7]=glyph.descent;
- data[6]=data[10]=x+glyph.w;
- data[11]=data[15]=glyph.h+glyph.descent;
-
- glDrawArrays(GL_QUADS, 0, 4);
-
- x+=glyph.advance;
-}
-
-Font::Loader::Loader(Font &f):
- font(f)
-{
- add("texture", &Loader::texture);
- add("glyph", &Loader::glyph);
-}
-
-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("descent", &Glyph::descent);
- 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;
-}
-
-} // namespace GL
-} // namespace Msp