prosperon/source/engine/font.c

259 lines
6.5 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"
2023-01-03 17:13:31 -06:00
#include "stb_truetype.h"
2022-12-28 16:50:54 -06:00
#include "stb_rect_pack.h"
2022-12-29 04:26:21 -06:00
#include "stb_image_write.h"
2021-11-30 21:29:18 -06:00
static uint32_t VBO = 0;
2022-12-29 17:44:19 -06:00
static uint32_t VAO = 0;
2021-11-30 21:29:18 -06:00
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
2023-01-15 09:53:50 -06:00
unsigned char *slurp_file(const char *filename) {
2023-01-10 14:02:24 -06:00
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *slurp = malloc(fsize);
fclose(f);
return slurp;
}
char *slurp_text(const char *filename) {
2023-01-10 14:02:24 -06:00
FILE *f = fopen(filename, "r'");
if (!f) return NULL;
char *buf;
long int fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
buf = malloc(fsize+1);
rewind(f);
size_t r = fread(buf, sizeof(char), fsize, f);
buf[r] = '\0';
fclose(f);
return buf;
}
2023-01-25 21:32:58 -06:00
int slurp_write(const char *txt, const char *filename)
{
FILE *f = fopen(filename, "w");
if (!f) return 1;
fputs(txt, f);
fclose(f);
return 0;
}
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);
glGenBuffers(1, &VBO);
2022-12-29 17:44:19 -06:00
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
2023-02-03 13:41:53 -06:00
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
2022-12-29 17:44:19 -06:00
glBindVertexArray(0);
2022-08-12 14:03:56 -05:00
// Default font
2022-12-29 04:26:21 -06:00
//font = MakeFont("teenytinypixels.ttf", 30);
font = MakeFont("LessPerfectDOSVGA.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-01-19 16:43:21 -06:00
struct sFont *MakeFont(const char *fontfile, int height)
2021-11-30 21:29:18 -06:00
{
2023-01-02 07:55:26 -06:00
YughInfo("Making font %s.", fontfile);
2022-06-30 13:03:17 -05:00
2023-02-03 13:41:53 -06:00
int packsize = 128;
2022-12-29 04:26:21 -06:00
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);
2023-01-03 17:13:31 -06:00
FILE *f = fopen(fontpath, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *ttf_buffer = malloc(fsize+1);
fread(ttf_buffer, fsize, 1, f);
fclose(f);
2022-06-23 17:41:30 -05:00
2022-12-29 04:26:21 -06:00
unsigned char *bitmap = malloc(packsize*packsize);
2022-12-28 16:50:54 -06:00
stbtt_packedchar glyphs[95];
stbtt_pack_context pc;
2022-12-29 04:26:21 -06:00
stbtt_PackBegin(&pc, bitmap, packsize, packsize, 0, 1, NULL);
2022-12-28 16:50:54 -06:00
stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs);
stbtt_PackEnd(&pc);
2023-02-03 13:41:53 -06:00
stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char) * packsize);
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
2023-01-02 07:55:26 -06:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2022-12-28 16:50:54 -06:00
glGenTextures(1, &newfont->texID);
glBindTexture(GL_TEXTURE_2D, newfont->texID);
2022-12-29 04:26:21 -06:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
2023-01-03 17:13:31 -06:00
2022-12-29 04:26:21 -06:00
//glGenerateMipmap(GL_TEXTURE_2D);
2022-12-28 16:50:54 -06: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);
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
2023-01-03 17:13:31 -06:00
free(ttf_buffer);
free(bitmap);
2022-06-23 17:41:30 -05:00
for (unsigned char c = 32; c < 127; c++) {
2022-12-28 16:50:54 -06:00
stbtt_packedchar glyph = glyphs[c-32];
2022-07-01 11:14:43 -05:00
2022-12-28 16:50:54 -06:00
struct glrect r;
2022-12-29 04:26:21 -06:00
r.s0 = glyph.x0 / (float) packsize;
r.s1 = glyph.x1 / (float) packsize;
r.t0 = glyph.y0 / (float) packsize;
r.t1 = glyph.y1 / (float) packsize;
newfont->Characters[c].Advance = glyph.xadvance;
newfont->Characters[c].Size[0] = glyph.x1 - glyph.x0;
newfont->Characters[c].Size[1] = glyph.y1 - glyph.y0;
newfont->Characters[c].Bearing[0] = glyph.xoff;
newfont->Characters[c].Bearing[1] = glyph.yoff2;
2022-12-28 16:50:54 -06:00
newfont->Characters[c].rect = r;
2021-11-30 21:29:18 -06:00
}
return newfont;
}
2022-12-28 16:50:54 -06:00
static int curchar = 0;
2023-02-03 13:41:53 -06:00
static float *buffdraw;
2022-12-28 16:50:54 -06:00
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;
2022-12-29 04:26:21 -06:00
float ypos = cursor[1] - c.Bearing[1] * scale;
2022-07-01 11:14:43 -05:00
2023-02-03 13:41:53 -06:00
float verts[16] = {
2022-12-28 16:50:54 -06:00
xpos, ypos, c.rect.s0, c.rect.t1,
xpos+w, ypos, c.rect.s1, c.rect.t1,
xpos, ypos + h, c.rect.s0, c.rect.t0,
xpos + w, ypos + h, c.rect.s1, c.rect.t0
2021-11-30 21:29:18 -06:00
};
2022-07-01 11:14:43 -05:00
2023-02-03 13:41:53 -06:00
/* Check if the vertex is off screen */
2022-12-29 04:26:21 -06:00
if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height)
return;
2022-12-28 16:50:54 -06:00
glBufferSubData(GL_ARRAY_BUFFER, curchar*sizeof(verts), sizeof(verts), verts);
2021-11-30 21:29:18 -06:00
2022-12-28 16:50:54 -06:00
curchar++;
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);
2022-12-28 16:50:54 -06:00
int len = strlen(text);
2021-11-30 21:29:18 -06:00
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);
2022-12-28 16:50:54 -06:00
glBindTexture(GL_TEXTURE_2D, font->texID);
2023-02-03 13:41:53 -06:00
glBindVertexArray(VAO);
2021-11-30 21:29:18 -06:00
glBindBuffer(GL_ARRAY_BUFFER, VBO);
2022-12-28 16:50:54 -06:00
glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float), NULL, GL_STREAM_DRAW);
2022-12-29 17:44:19 -06:00
2023-02-03 13:41:53 -06:00
2021-11-30 21:29:18 -06:00
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
2022-12-28 16:50:54 -06:00
curchar = 0;
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
}
2022-12-28 16:50:54 -06:00
2023-02-03 13:41:53 -06:00
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4*curchar);
2021-11-30 21:29:18 -06:00
}