diff options
Diffstat (limited to 'src/libjin/Graphics/Font.cpp')
-rw-r--r-- | src/libjin/Graphics/Font.cpp | 90 |
1 files changed, 40 insertions, 50 deletions
diff --git a/src/libjin/Graphics/Font.cpp b/src/libjin/Graphics/Font.cpp index a107613..6be08e7 100644 --- a/src/libjin/Graphics/Font.cpp +++ b/src/libjin/Graphics/Font.cpp @@ -28,29 +28,26 @@ namespace graphics // bitmap for saving font data static unsigned char temp_bitmap[BITMAP_WIDTH * BITMAP_HEIGHT]; - void Font::loadf(const char* path) + void Font::loadFile(const char* path) { fread(ttf_buffer, 1, 1 << 20, fopen(path, "rb")); - - loadb(ttf_buffer); + loadMemory(ttf_buffer); } /** * load from memory */ - void Font::loadb(const unsigned char* data) + void Font::loadMemory(const unsigned char* data) { - stbtt_BakeFontBitmap(data, 0, PIXEL_HEIGHT, temp_bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, 32, 96, cdata); + if (data == nullptr) + return; + stbtt_BakeFontBitmap(data, 0, PIXEL_HEIGHT, temp_bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, 32, ASCII_CHARACTER_NUM, asciiData); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_WIDTH, - BITMAP_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); - + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_WIDTH, BITMAP_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glBindTexture(GL_TEXTURE_2D, 0); } @@ -69,16 +66,12 @@ namespace graphics return q; } - /** - * render function draw mutiple times - * in loop - */ void Font::render( - const char* text, // rendered text - float x, float y, // render position - int fheight, // font height - int spacing, // font spacing - int lheight) // line height + const char* text, + float x, float y, + int fontHeight, + int spacing, + int lineHeight) { float _x = x, _y = y; @@ -95,38 +88,37 @@ namespace graphics stbtt_aligned_quad q; // render every given character - int xc = x; - int yc = y; + float xc = x; + float yc = y; - float factor = fheight / (float)PIXEL_HEIGHT; - + float factor = fontHeight / (float)PIXEL_HEIGHT; + float texCoord[8]; + float verCoord[8]; for (int i = 0; i < len; ++i) { char c = text[i]; if (c == '\n') { xc = x; - yc += lheight; + yc += lineHeight; continue; } // only support ASCII - Quad q = getCharQuad(cdata, 512, 512, c - 32); + Quad q = getCharQuad(asciiData, 512, 512, c - 32); float s0 = q.x, s1 = q.x + q.w, t0 = q.y, t1 = q.y + q.h; // texture quad - float tc[] = { - s0, t1, - s1, t1, - s1, t0, - s0, t0 - }; + texCoord[0] = s0; texCoord[1] = t1; + texCoord[2] = s1; texCoord[3] = t1; + texCoord[4] = s1; texCoord[5] = t0; + texCoord[6] = s0; texCoord[7] = t0; // character bound box - stbtt_bakedchar box = cdata[c - 32]; + stbtt_bakedchar box = asciiData[c - 32]; float width = factor * (box.x1 - box.x0); float height = factor * (box.y1 - box.y0); @@ -137,18 +129,16 @@ namespace graphics float xadvance = factor * box.xadvance; // render quad - float vc[] = { - xc + xoffset, yc + height + yoffset, - xc + width + xoffset, yc + height + yoffset, - xc + width + xoffset, yc + yoffset, - xc + xoffset, yc + yoffset - }; + verCoord[0] = xc + xoffset; verCoord[1] = yc + height + yoffset; + verCoord[2] = xc + width + xoffset; verCoord[3] = yc + height + yoffset; + verCoord[4] = xc + width + xoffset; verCoord[5] = yc + yoffset; + verCoord[6] = xc + xoffset; verCoord[7] = yc + yoffset; // forward to next character xc += xadvance + spacing; - glTexCoordPointer(2, GL_FLOAT, 0, tc); - glVertexPointer(2, GL_FLOAT, 0, vc); + glTexCoordPointer(2, GL_FLOAT, 0, texCoord); + glVertexPointer(2, GL_FLOAT, 0, verCoord); glDrawArrays(GL_QUADS, 0, 4); } @@ -159,26 +149,26 @@ namespace graphics glDisable(GL_TEXTURE_2D); } - void Font::box(const char* str, int fheight, int spacing, int lheight, int* w, int * h) + void Font::box(const char* text, int fontHeight, int spacing, int lineHeight, int* w, int * h) { - int len = strlen(str); + int len = strlen(text); float xc = 0; - int yc = len ? lheight: 0; + int yc = len ? lineHeight : 0; int maxX = 0; - float factor = fheight / (float)PIXEL_HEIGHT; + float factor = fontHeight / (float)PIXEL_HEIGHT; - for (int i = 0; i < len; ++i) + for (int i = 0; i < len; ++i) { - char c = str[i]; + char c = text[i]; if (c == '\n') { - yc += lheight; + yc += lineHeight; xc = 0; continue; } - stbtt_bakedchar box = cdata[c - 32]; + stbtt_bakedchar box = asciiData[c - 32]; xc += factor * box.xadvance + spacing; if (xc > maxX) maxX = xc; @@ -188,7 +178,7 @@ namespace graphics *h = yc; } -} -} +} // graphics +} // jin #endif // JIN_MODULES_RENDER
\ No newline at end of file |