Created
November 20, 2017 15:40
-
-
Save croepha/e2af8b07e807720744bd3ced5aacc1cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void paint_text(V2<float>&cursor, V4<float>&bounds, BakedFont&font, char*input_text) { | |
glEnable(GL_TEXTURE_2D); | |
glBindTexture(GL_TEXTURE_2D, font.texture); | |
glBegin(GL_QUADS); | |
char*text_end = strlen(input_text) + input_text; | |
for(char*t = input_text; t<text_end; t++) { | |
if (*t == '\n') { | |
cursor.x = bounds.l; | |
cursor.y += font.size; | |
} else if (*t >= 32) { | |
stbtt_aligned_quad q; | |
cursor.x = (float)(u32)cursor.x; | |
cursor.y = (float)(u32)cursor.y; | |
//stbtt_GetBakedQuad(font.cdata, 512,512, *t-32, &cursor.x,&cursor.y,&q,1); | |
stbtt_GetPackedQuad(font.cdata, 1024,1024, *t-32, &cursor.x,&cursor.y,&q,1); | |
glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y1); | |
glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y1); | |
glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y0); | |
glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y0); | |
} | |
} | |
glEnd(); | |
glDisable(GL_TEXTURE_2D); | |
assert(glGetError() == GL_NO_ERROR); | |
} | |
void bake_font(BakedFont&font, float size, char*path) { | |
font.size = size; | |
u8 ttf_buffer[1<<20]; | |
auto f = fopen(path, "rb"); | |
assert(f); | |
fread(ttf_buffer, 1, 1<<20, f); | |
fclose(f); | |
u8 temp_bitmap[1024*1024]; | |
stbtt_pack_context pc; | |
stbtt_PackBegin(&pc, temp_bitmap, 1024, 1024, 0, 1, NULL); | |
stbtt_PackSetOversampling(&pc, 8, 8); | |
stbtt_pack_range ranges[] = { | |
{size, 32, NULL, 95, font.cdata, 0, 0}, // ascii, from 32 to 127 | |
//{size, 0x2200, NULL, 255, font.cdata+95, 0, 0}, // math glyphs | |
//{size, 0x1D434, NULL, 255, font.cdata+95+255, 0, 0} // fancy alphabet | |
}; | |
//static_assert (acount(font.cdata) >= 95+255+255); | |
stbtt_PackFontRanges(&pc, ttf_buffer, 0, ranges, acount(ranges)); | |
glEnable(GL_TEXTURE_2D); | |
glGenTextures(1, &font.texture); | |
glBindTexture(GL_TEXTURE_2D, font.texture); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024,1024, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glDisable(GL_TEXTURE_2D); | |
assert(glGetError() == GL_NO_ERROR); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment