diff options
Diffstat (limited to 'src/libjin/Graphics/Font.h')
-rw-r--r-- | src/libjin/Graphics/Font.h | 79 |
1 files changed, 58 insertions, 21 deletions
diff --git a/src/libjin/Graphics/Font.h b/src/libjin/Graphics/Font.h index 0ab482d..10fd242 100644 --- a/src/libjin/Graphics/Font.h +++ b/src/libjin/Graphics/Font.h @@ -3,6 +3,8 @@ #include "../modules.h" #if LIBJIN_MODULES_RENDER +#include <vector> +#include <map> #include "drawable.h" #include "../3rdparty/stb/stb_truetype.h" #include "../math/quad.h" @@ -11,33 +13,68 @@ namespace jin { namespace graphics { + /** + * 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: - Font * createFont(const char* file); - Font* createFont(const char* data, size_t size); + static Font* createFont(const char* file); + static Font* createFont(const char* data, size_t size); - void box(const char* text, int fontHeight, int spacing, int lineHeight, int* w, int * h); + void print(const char* text, int x, int y); private: - /* ASCII 32(space)..126(~) 95 glyphs */ - static const int ASCII_CHARACTER_NUM = 96; - - Font(); - - void loadFile(const char* file); - void loadMemory(const unsigned char* data); - void render( - const char* text, // rendered text - float x, float y, // render position - int fondSize, // font size - int spacing, // font spacing - int lineHeight // line height - ); - - stbtt_bakedchar asciiData[ASCII_CHARACTER_NUM]; - + /* 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; + }; } // graphics |