prosperon/source/engine/font.c

234 lines
6.1 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "font.h"
2022-02-06 10:14:57 -06:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include <shader.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
2022-06-21 15:21:00 -05:00
#include <stdlib.h>
2022-06-26 08:36:38 -05:00
#include <window.h>
#include "log.h"
2022-06-21 15:21:00 -05:00
2022-12-22 16:58:06 -06:00
#include "openglrender.h"
2021-11-30 21:29:18 -06:00
#include <stb_truetype.h>
static uint32_t VBO = 0;
static uint32_t VAO = 0;
2022-06-21 15:21:00 -05:00
unsigned char ttf_buffer[1<<25];
2021-11-30 21:29:18 -06:00
unsigned char temp_bitmap[512 * 512];
2022-01-31 17:04:59 -06:00
struct sFont *font;
2022-11-19 17:13:57 -06:00
static struct shader *shader;
2022-01-19 16:43:21 -06:00
2022-11-19 17:13:57 -06:00
void font_init(struct shader *textshader) {
2022-06-30 10:31:23 -05:00
shader = textshader;
2022-06-30 13:03:17 -05:00
shader_use(shader);
// configure VAO/VBO for texture quads
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
2022-08-12 14:03:56 -05:00
// Default font
2022-12-23 13:48:29 -06:00
font = MakeFont("teenytinypixels.ttf", 16);
2022-01-31 17:04:59 -06:00
}
void font_frame(struct window *w) {
2022-06-26 08:36:38 -05:00
shader_use(shader);
}
2022-06-30 13:03:17 -05:00
// Height in pixels
2022-01-19 16:43:21 -06:00
struct sFont *MakeFont(const char *fontfile, int height)
2021-11-30 21:29:18 -06:00
{
2022-06-30 13:03:17 -05:00
shader_use(shader);
2022-01-19 16:43:21 -06:00
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
2021-11-30 21:29:18 -06:00
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
2022-06-23 17:41:30 -05:00
fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb"));
2021-11-30 21:29:18 -06:00
2022-06-30 13:03:17 -05:00
stbtt_fontinfo fontinfo;
2022-06-23 17:41:30 -05:00
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) {
YughError("Failed to make font %s", fontfile);
2022-06-23 17:41:30 -05:00
}
2022-06-23 16:15:43 -05:00
2022-06-30 13:03:17 -05:00
float scale = stbtt_ScaleForPixelHeight(&fontinfo, height);
2022-06-21 15:21:00 -05:00
2022-06-23 17:41:30 -05:00
int ascent, descent, linegap;
2022-06-23 16:15:43 -05:00
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
2022-06-30 13:03:17 -05:00
2022-06-23 16:15:43 -05:00
ascent = roundf(ascent*scale);
2022-06-30 13:03:17 -05:00
descent = roundf(descent*scale);
2021-11-30 21:29:18 -06:00
2022-06-23 17:41:30 -05:00
2021-11-30 21:29:18 -06:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2022-06-23 17:41:30 -05:00
for (unsigned char c = 32; c < 127; c++) {
2021-11-30 21:29:18 -06:00
unsigned char *bitmap;
2022-06-30 13:03:17 -05:00
int advance, lsb, w, h, x0, y0;
2021-11-30 21:29:18 -06:00
stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb);
2022-07-01 11:14:43 -05:00
2022-06-30 13:03:17 -05:00
bitmap = stbtt_GetCodepointBitmap(&fontinfo, scale, scale, c, &w, &h, &x0, &y0);
2021-11-30 21:29:18 -06:00
GLuint ftexture;
glGenTextures(1, &ftexture);
glBindTexture(GL_TEXTURE_2D, ftexture);
2022-06-23 17:41:30 -05:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
2021-11-30 21:29:18 -06:00
2022-07-01 11:14:43 -05:00
glGenerateMipmap(GL_TEXTURE_2D);
2022-06-23 17:41:30 -05:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2022-12-20 19:34:22 -06:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2021-11-30 21:29:18 -06:00
2022-01-31 17:04:59 -06:00
newfont->Characters[c].TextureID = ftexture;
2022-06-30 13:03:17 -05:00
newfont->Characters[c].Advance = advance * scale;
2022-01-31 17:04:59 -06:00
newfont->Characters[c].Size[0] = w;
newfont->Characters[c].Size[1] = h;
2022-06-30 13:03:17 -05:00
newfont->Characters[c].Bearing[0] = x0;
2022-07-01 11:14:43 -05:00
newfont->Characters[c].Bearing[1] = y0*-1;
2021-11-30 21:29:18 -06:00
}
return newfont;
}
2022-11-19 17:13:57 -06:00
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3])
2021-11-30 21:29:18 -06:00
{
float w = c.Size[0] * scale;
float h = c.Size[1] * scale;
2022-07-01 11:14:43 -05:00
float xpos = cursor[0] + c.Bearing[0] * scale;
float ypos = cursor[1] + (c.Bearing[1] * scale) - h;
2021-11-30 21:29:18 -06:00
float verts[4 * 4] = {
2022-06-30 16:38:51 -05:00
xpos, ypos, 0.f, 0.f,
xpos+w, ypos, 1.f, 0.f,
xpos, ypos + h, 0.f, 1.f,
2022-07-01 11:14:43 -05:00
xpos + w, ypos + h, 1.f, 1.f
2021-11-30 21:29:18 -06:00
};
2022-12-20 19:34:22 -06:00
glBindTexture(GL_TEXTURE_2D, c.TextureID);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2022-06-30 10:31:23 -05:00
////// Outline calculation
2021-11-30 21:29:18 -06:00
// float outlineWidth = 1.1;
// float ow = c.Size[0] * scale * outlineWidth;
// float oh = c.Size[1] * scale * outlineWidth;
// float oxpos = cursor[0] + c.Bearing[0] * scale * outlineWidth - ((ow-w)/2);
// float oypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale * outlineWidth - ((oh-h)/2);
// float overts[4*4] = {
// oxpos, oypos + oh, 0.f, 0.f,
// oxpos, oypos, 0.f, 1.f,
// oxpos + ow, oypos + oh, 1.f, 0.f,
// oxpos + ow, oypos, 1.f, 1.f
// };
2022-06-30 10:31:23 -05:00
/////////// Shadow calculation
2022-07-01 11:14:43 -05:00
2022-12-20 19:34:22 -06:00
/*
2021-11-30 21:29:18 -06:00
float shadowOffset = 6.f;
2022-06-30 10:31:23 -05:00
float sxpos = cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset);
float sypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - (scale * shadowOffset);
2021-11-30 21:29:18 -06:00
float sverts[4 * 4] = {
2022-07-01 11:14:43 -05:00
sxpos, sypos, 0.f, 0.f,
sxpos+w, sypos, 1.f, 0.f,
sxpos, sypos + h, 0.f, 1.f,
sxpos + w, sypos+h, 1.f, 1.f
2021-11-30 21:29:18 -06:00
};
2022-07-01 11:14:43 -05:00
2021-11-30 21:29:18 -06:00
2022-12-20 19:34:22 -06:00
2021-11-30 21:29:18 -06:00
//// Shadow pass
2022-07-01 11:14:43 -05:00
2021-11-30 21:29:18 -06:00
float black[3] = { 0, 0, 0 };
shader_setvec3(shader, "textColor", black);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(sverts), sverts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2022-07-01 11:14:43 -05:00
2022-12-20 19:34:22 -06:00
*/
2021-11-30 21:29:18 -06:00
}
2022-01-31 17:04:59 -06:00
void text_settype(struct sFont *mfont)
2022-01-19 16:43:21 -06:00
{
font = mfont;
2022-01-31 17:04:59 -06:00
}
2022-01-19 16:43:21 -06:00
void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw)
2021-11-30 21:29:18 -06:00
{
2022-12-20 19:34:22 -06:00
shader_use(shader);
2021-11-30 21:29:18 -06:00
shader_setvec3(shader, "textColor", color);
mfloat_t cursor[2] = { 0.f };
2022-12-24 13:18:06 -06:00
cursor[0] = pos[0];
cursor[1] = pos[1];
2021-11-30 21:29:18 -06:00
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
2022-02-06 10:14:57 -06:00
const unsigned char *line, *wordstart;
line = (unsigned char*)text;
2022-01-31 17:04:59 -06:00
while (*line != '\0') {
switch (*line) {
case '\n':
cursor[1] -= scale * font->height;
line++;
break;
case ' ':
sdrawCharacter(font->Characters[*line], cursor, scale, shader, color);
cursor[0] += font->Characters[*line].Advance * scale;
line++;
break;
default:
wordstart = line;
int wordWidth = 0;
while (!isspace(*line) && *line != '\0') {
wordWidth += font->Characters[*line].Advance * scale;
line++;
}
if (lw > 0 && (cursor[0] + wordWidth - pos[0]) >= lw) {
cursor[0] = pos[0];
cursor[1] -= scale * font->height;
}
while (wordstart < line) {
sdrawCharacter(font->Characters[*wordstart], cursor, scale, shader, color);
cursor[0] += font->Characters[*wordstart].Advance * scale;
wordstart++;
}
}
2021-11-30 21:29:18 -06:00
}
}