diff options
Diffstat (limited to 'libjin/Graphics/Font.h')
-rw-r--r-- | libjin/Graphics/Font.h | 109 |
1 files changed, 66 insertions, 43 deletions
diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h index 7fc96e2..10fd242 100644 --- a/libjin/Graphics/Font.h +++ b/libjin/Graphics/Font.h @@ -1,8 +1,10 @@ -#ifndef __JIN_FONT_H -#define __JIN_FONT_H +#ifndef __LIBJIN_FONT_H +#define __LIBJIN_FONT_H #include "../modules.h" -#if JIN_MODULES_RENDER +#if LIBJIN_MODULES_RENDER +#include <vector> +#include <map> #include "drawable.h" #include "../3rdparty/stb/stb_truetype.h" #include "../math/quad.h" @@ -12,50 +14,71 @@ namespace jin namespace graphics { /** - * Usage of stb_truetype.h here might be a little - * bit dummy. Implementation of Font is referring - * to stb_truetype.h L243~284. I basicly copy it:) + * original from love2d font and graphics modules + * the basic idea is storing glyphs in several mipmap + * http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html */ - class Font: public Drawable + + struct GlyphVertex + { + float x, y; // screen coordinates + float u, v; // texture coordinates + }; + + /* track when to change texutre binded in render array */ + /* casue switch texture is expensive */ + /* std::vector<GlyphVertex> list */ + struct GlyphArrayDrawInfo + { + GLuint texture; + int startvertex; + int vertexcount; + }; + + /* glyph texture */ + struct Glyph + { + GLuint texture; // texture where this glyph rendered + int spacing; // spacing of glyph + GlyphVertex vertices[4]; // quad of glyph render region + }; + + class Font { public: + static Font* createFont(const char* file); + static Font* createFont(const char* data, size_t size); + + void print(const char* text, int x, int y); + + private: + /* font atlas levels */ + static const int TEXTURE_SIZE_LEVELS_COUNT = 7; + static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1; + static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; + static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; + static const int SPACES_PER_TAB = 4; + + /* create a new mipmap to render glyph and push it on textures */ + bool createTexture(); + /* create a glyph for a unicode and return it */ + Glyph* addGlyph(unsigned int character); + /* find glyph by unicode */ + Glyph* findGlyph(unsigned int character); + + /* list of textures where glyphs rendered, always operate the last one */ + /* map character to its render area */ + std::vector<GLuint> textures; + std::map<unsigned int, Glyph*> glyphs; + /* mipmap size level */ + int textureLevel; + int textureWidth; + int textureHeight; - Font(); - - /** - * load ttf font data from .ttf - */ - void loadf(const char* file); - - /** - * load ttf font data from memory - */ - void loadb(const unsigned char* data); - - /** - * render text to screen - */ - void render( - const char* str, // rendered text - float x, float y, // render position - int fheight, // font size - int spacing, // font spacing - int lheight // line height - ); - - void box(const char* str, int fheight, int spacing, int lheight, int* w, int * h); - - private: - - /** - * ASCII 32(space)..126(~) is 95 glyphs - */ - stbtt_bakedchar cdata[96]; - }; -} -} +} // graphics +} // jin -#endif // JIN_MODULES_RENDER -#endif // __JIN_FONT_H
\ No newline at end of file +#endif // LIBJIN_MODULES_RENDER +#endif // __LIBJIN_FONT_H
\ No newline at end of file |