Because the title states, I’ve not too long ago written a fundamental utility that utitlizes stb_truetype.h in addition to OpenGL to write down a specified line of textual content to a bitmap buffer (named: u8* bitmap) after which OpenGL will then render that bitmap to the display.
My query is, is there a greater solution to render traces of textual content than this? Is there a greater solution to render out textual content than what I’m at the moment doing? Is that this environment friendly or affordable for a bigger scale undertaking?
Thanks upfront to your useful strategies!
P.S. Please chorus from telling me to change from legacy opengl, this was only a fast and soiled write up, I’ll change to “trendy” opengl in future iterations.
Here’s a image of the results of my code: https://imgur.com/a/TGFS2Ex
Code:
u8* fontBuffer;
u32 ftex;
int x = 0;
int y = 0;
u32 DTB_INIT(dtbPlatform* platform)
{
dtbgl_init();
lengthy dimension;
FILE* fontFile = fopen("c:/home windows/fonts/occasions.ttf", "rb");
fseek(fontFile, 0, SEEK_END);
dimension = ftell(fontFile);
fseek(fontFile, 0, SEEK_SET);
fontBuffer = (u8*)malloc(dimension);
fread(fontBuffer, dimension, 1, fontFile);
fclose(fontFile);
// NOTE(Dillon): Put together the font
stbtt_fontinfo data;
if(!stbtt_InitFont(&data, fontBuffer, 0))
{
assert(!"Didn't init the font");
}
int bitmapWidth = 512;
int bitmapHeight = 128;
int lineHeight = 64;
// NOTE(Dillon): Allocate the bitmap
u8* bitmap = (u8*)calloc(bitmapWidth * bitmapHeight, sizeof(u8));
// NOTE(Dillon): Calculate font scaling
float scale = stbtt_ScaleForPixelHeight(&data, lineHeight);
char* textual content = "Font rendering take a look at";
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&data, &ascent, &descent, &lineGap);
ascent *= scale;
descent *= scale;
for(int i = 0; i < strlen(textual content); i++)
{
// NOTE(Dillon): Calculate how large the particular character is
int ax;
int lsb;
stbtt_GetCodepointHMetrics(&data, textual content[i], &ax, &lsb);
// NOTE(Dillon): Get bounding field for the particular character
int c_x1, c_y1, c_x2, c_y2;
stbtt_GetCodepointBitmapBox(&data, textual content[i], scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
// NOTE(Dillon): Compute the heights a.okay.a the y
int y = ascent + c_y1;
// NOTE(Dillon): Write the character to the bitmap
int byteOffset = x + (lsb * scale) + (y * bitmapWidth);
stbtt_MakeCodepointBitmap(&data, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, bitmapWidth, scale, scale, textual content[i]);
// NOTE(Dillon): Advance the characters
x += ax * scale;
// NOTE(Dillon): Kerning?
int kern;
kern = stbtt_GetCodepointKernAdvance(&data, textual content[i], textual content[i + 1]);
x += kern * scale;
}
glGenTextures(1, &ftex);
glBindTexture(GL_TEXTURE_2D, ftex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmapWidth, bitmapHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
return 0;
}
void DTB_LOOP(dtbPlatform* platform, float deltaTime)
GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 800 - 1, 0, 600 -1);
// NOTE(Dillon): Render right here
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex2f(400, 300);
glTexCoord2f(1, 1); glVertex2f(600, 300);
glTexCoord2f(1, 0); glVertex2f(600, 500);
glTexCoord2f(0, 0); glVertex2f(400, 500);
glEnd();