zephr: consume hover event on top-most ui elements

This commit is contained in:
2024-01-20 06:39:49 +02:00
parent 156021347f
commit 3ece69dfc6
7 changed files with 1230 additions and 1181 deletions
+155 -155
View File
@@ -45,14 +45,14 @@ font_instance_vbo : u32
init_freetype :: proc(font_path: cstring) -> i32 {
ft: FT.Library;
ft: FT.Library
if (FT.Init_FreeType(&ft) != 0) {
return -1;
return -1
}
face: FT.Face;
face: FT.Face
if (FT.New_Face(ft, font_path, 0, &face) != 0) {
return -2;
return -2
}
// sets the variable font to be bold
@@ -66,301 +66,301 @@ init_freetype :: proc(font_path: cstring) -> i32 {
/* FT_Done_MM_Var(ft, mm); */
/* } */
FT.Set_Pixel_Sizes(face, 0, FONT_PIXEL_SIZE);
FT.Set_Pixel_Sizes(face, 0, FONT_PIXEL_SIZE)
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
pen_x, pen_y: u32
/* FT_UInt glyph_idx; */
/* FT_ULong c = FT_Get_First_Char(face, &glyph_idx); */
tex_width, tex_height: u32;
tex_width, tex_height: u32
for i in 32..<128 {
if (FT.Load_Char(face, cast(u64)i, .RENDER) != 0) {
fmt.printf("[ERROR]: failed to load glyph for char '0x%x'\n", i);
fmt.eprintf("[ERROR]: failed to load glyph for char '0x%x'\n", i)
}
/* FT_Render_Glyph(face->glyph, FT_RENDER_MODE_SDF); */
tex_width += face.glyph.bitmap.width + 1;
tex_height = max(tex_height, face.glyph.bitmap.rows);
tex_width += face.glyph.bitmap.width + 1
tex_height = max(tex_height, face.glyph.bitmap.rows)
}
pixels := make([dynamic]u8, tex_width * tex_height);
pixels := make([dynamic]u8, tex_width * tex_height)
defer delete(pixels)
for i in 32..<128 {
/* while (glyph_idx) { */
if (FT.Load_Char(face, cast(u64)i, .RENDER) != 0) {
fmt.printf("[ERROR]: failed to load glyph for char '0x%x'\n", i);
fmt.eprintf("[ERROR]: failed to load glyph for char '0x%x'\n", i)
}
/* FT_Render_Glyph(face->glyph, FT_RENDER_MODE_SDF); */
bmp := &face.glyph.bitmap;
bmp := &face.glyph.bitmap
if (pen_x + bmp.width >= tex_width) {
pen_x = 0;
pen_y += cast(u32)(1 + (face.size.metrics.height >> 6));
pen_x = 0
pen_y += cast(u32)(1 + (face.size.metrics.height >> 6))
}
for row in 0..<bmp.rows {
for col in 0..<bmp.width {
x := pen_x + col;
y := pen_y + row;
pixels[y * tex_width + x] = bmp.buffer[row * cast(u32)bmp.pitch + col];
x := pen_x + col
y := pen_y + row
pixels[y * tex_width + x] = bmp.buffer[row * cast(u32)bmp.pitch + col]
}
}
atlas_x0 := cast(f32)pen_x / cast(f32)tex_width;
atlas_y0 := cast(f32)pen_y / cast(f32)tex_height;
atlas_x1 := cast(f32)(pen_x + bmp.width) / cast(f32)tex_width;
atlas_y1 := cast(f32)(pen_y + bmp.rows) / cast(f32)tex_height;
atlas_x0 := cast(f32)pen_x / cast(f32)tex_width
atlas_y0 := cast(f32)pen_y / cast(f32)tex_height
atlas_x1 := cast(f32)(pen_x + bmp.width) / cast(f32)tex_width
atlas_y1 := cast(f32)(pen_y + bmp.rows) / cast(f32)tex_height
top_left := Vec2{ atlas_x0, atlas_y1 };
top_right := Vec2{ atlas_x1, atlas_y1 };
bottom_right := Vec2{ atlas_x1, atlas_y0 };
bottom_left := Vec2{ atlas_x0, atlas_y0 };
top_left := Vec2{ atlas_x0, atlas_y1 }
top_right := Vec2{ atlas_x1, atlas_y1 }
bottom_right := Vec2{ atlas_x1, atlas_y0 }
bottom_left := Vec2{ atlas_x0, atlas_y0 }
character: Character
character.tex_coords[0] = top_left;
character.tex_coords[1] = top_right;
character.tex_coords[2] = bottom_right;
character.tex_coords[3] = bottom_left;
character.advance = cast(u32)face.glyph.advance.x;
character.size = Vec2{ cast(f32)face.glyph.bitmap.width, cast(f32)face.glyph.bitmap.rows };
character.bearing = Vec2{ cast(f32)face.glyph.bitmap_left, cast(f32)face.glyph.bitmap_top };
character.tex_coords[0] = top_left
character.tex_coords[1] = top_right
character.tex_coords[2] = bottom_right
character.tex_coords[3] = bottom_left
character.advance = cast(u32)face.glyph.advance.x
character.size = Vec2{ cast(f32)face.glyph.bitmap.width, cast(f32)face.glyph.bitmap.rows }
character.bearing = Vec2{ cast(f32)face.glyph.bitmap_left, cast(f32)face.glyph.bitmap_top }
zephr_ctx.font.characters[i] = character;
zephr_ctx.font.characters[i] = character
pen_x += bmp.width + 1;
pen_x += bmp.width + 1
/* c = FT_Get_Next_Char(face, c, &glyph_idx); */
}
texture: TextureId;
gl.GenTextures(1, &texture);
gl.BindTexture(gl.TEXTURE_2D, texture);
texture: TextureId
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, cast(i32)tex_width,
cast(i32)tex_height, 0, gl.RED, gl.UNSIGNED_BYTE,
raw_data(pixels))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
zephr_ctx.font.atlas_tex_id = texture;
zephr_ctx.font.atlas_tex_id = texture
gl.BindTexture(gl.TEXTURE_2D, 0);
gl.BindTexture(gl.TEXTURE_2D, 0)
FT.Done_Face(face);
FT.Done_FreeType(ft);
FT.Done_Face(face)
FT.Done_FreeType(ft)
return 0;
return 0
}
init_fonts :: proc(font_path: cstring) -> i32 {
font_vbo, font_ebo: u32;
font_vbo, font_ebo: u32
res := init_freetype(font_path);
res := init_freetype(font_path)
if (res != 0) {
return res;
return res
}
l_font_shader, success := create_shader("shaders/font.vert", "shaders/font.frag");
l_font_shader, success := create_shader("shaders/font.vert", "shaders/font.frag")
if (!success) {
fmt.println("[FATAL] Failed to create font shader")
return -2;
return -2
}
font_shader = l_font_shader;
font_shader = l_font_shader
gl.GenVertexArrays(1, &font_vao);
gl.GenBuffers(1, &font_vbo);
gl.GenBuffers(1, &font_instance_vbo);
gl.GenBuffers(1, &font_ebo);
gl.GenVertexArrays(1, &font_vao)
gl.GenBuffers(1, &font_vbo)
gl.GenBuffers(1, &font_instance_vbo)
gl.GenBuffers(1, &font_ebo)
quad_vertices := [4][2]f32 {
{0.0, 1.0}, // top left
{1.0, 1.0}, // top right
{1.0, 0.0}, // bottom right
{0.0, 0.0}, // bottom left
};
}
quad_indices := [6]u32 {
0, 1, 2,
2, 3, 0,
};
}
gl.BindVertexArray(font_vao);
gl.BindVertexArray(font_vao)
// font quad vbo
gl.BindBuffer(gl.ARRAY_BUFFER, font_vbo);
gl.BufferData(gl.ARRAY_BUFFER, size_of(quad_vertices), &quad_vertices, gl.STATIC_DRAW);
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 2, gl.FLOAT, gl.FALSE, 2 * size_of(f32), 0);
gl.BindBuffer(gl.ARRAY_BUFFER, font_vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(quad_vertices), &quad_vertices, gl.STATIC_DRAW)
gl.EnableVertexAttribArray(0)
gl.VertexAttribPointer(0, 2, gl.FLOAT, gl.FALSE, 2 * size_of(f32), 0)
// font instance vbo
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo);
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), 0);
gl.VertexAttribDivisor(1, 1);
gl.EnableVertexAttribArray(2);
gl.VertexAttribIPointer(2, 1, gl.INT, size_of(GlyphInstance), size_of(Vec4));
gl.VertexAttribDivisor(2, 1);
gl.EnableVertexAttribArray(3);
gl.VertexAttribPointer(3, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) + size_of(i32)));
gl.VertexAttribDivisor(3, 1);
gl.EnableVertexAttribArray(4);
gl.EnableVertexAttribArray(5);
gl.EnableVertexAttribArray(6);
gl.EnableVertexAttribArray(7);
gl.VertexAttribPointer(4, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 2 + size_of(i32)));
gl.VertexAttribPointer(5, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 3 + size_of(i32)));
gl.VertexAttribPointer(6, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 4 + size_of(i32)));
gl.VertexAttribPointer(7, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 5 + size_of(i32)));
gl.VertexAttribDivisor(4, 1);
gl.VertexAttribDivisor(5, 1);
gl.VertexAttribDivisor(6, 1);
gl.VertexAttribDivisor(7, 1);
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo)
gl.EnableVertexAttribArray(1)
gl.VertexAttribPointer(1, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), 0)
gl.VertexAttribDivisor(1, 1)
gl.EnableVertexAttribArray(2)
gl.VertexAttribIPointer(2, 1, gl.INT, size_of(GlyphInstance), size_of(Vec4))
gl.VertexAttribDivisor(2, 1)
gl.EnableVertexAttribArray(3)
gl.VertexAttribPointer(3, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) + size_of(i32)))
gl.VertexAttribDivisor(3, 1)
gl.EnableVertexAttribArray(4)
gl.EnableVertexAttribArray(5)
gl.EnableVertexAttribArray(6)
gl.EnableVertexAttribArray(7)
gl.VertexAttribPointer(4, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 2 + size_of(i32)))
gl.VertexAttribPointer(5, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 3 + size_of(i32)))
gl.VertexAttribPointer(6, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 4 + size_of(i32)))
gl.VertexAttribPointer(7, 4, gl.FLOAT, gl.FALSE, size_of(GlyphInstance), (size_of(Vec4) * 5 + size_of(i32)))
gl.VertexAttribDivisor(4, 1)
gl.VertexAttribDivisor(5, 1)
gl.VertexAttribDivisor(6, 1)
gl.VertexAttribDivisor(7, 1)
// font ebo
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, font_ebo);
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(quad_indices), &quad_indices, gl.STATIC_DRAW);
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, font_ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(quad_indices), &quad_indices, gl.STATIC_DRAW)
use_shader(font_shader);
use_shader(font_shader)
for i in 32..<128 {
for j in 0..<4 {
buf: [24]byte
text := fmt.bprintf(buf[:], "texcoords[%d]", (i - 32) * 4 + j);
set_vec2f(font_shader, cstring(raw_data(text)), zephr_ctx.font.characters[i].tex_coords[j].x, zephr_ctx.font.characters[i].tex_coords[j].y);
text := fmt.bprintf(buf[:], "texcoords[%d]", (i - 32) * 4 + j)
set_vec2f(font_shader, cstring(raw_data(text)), zephr_ctx.font.characters[i].tex_coords[j].x, zephr_ctx.font.characters[i].tex_coords[j].y)
}
}
gl.BindBuffer(gl.ARRAY_BUFFER, 0);
gl.BindVertexArray(0);
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0)
return 0;
return 0
}
calculate_text_size :: proc(text: string, font_size: u32) -> Vec2 {
scale := cast(f32)font_size / FONT_PIXEL_SIZE;
scale := cast(f32)font_size / FONT_PIXEL_SIZE
size : Vec2 = ---
w: f32 = 0;
h: f32 = 0;
max_bearing_h: f32 = 0;
w: f32 = 0
h: f32 = 0
max_bearing_h: f32 = 0
// NOTE: I don't like looping through the characters twice, but it's fine for now
for i in 0..<len(text) {
ch := zephr_ctx.font.characters[text[i]];
max_bearing_h = max(max_bearing_h, ch.bearing.y);
ch := zephr_ctx.font.characters[text[i]]
max_bearing_h = max(max_bearing_h, ch.bearing.y)
}
for i in 0..<len(text) {
ch := zephr_ctx.font.characters[text[i]];
w += cast(f32)ch.advance / 64;
ch := zephr_ctx.font.characters[text[i]]
w += cast(f32)ch.advance / 64
// remove bearing of first character
if (i == 0 && len(text) > 1) {
w -= ch.bearing.x;
w -= ch.bearing.x
}
// remove the trailing width of the last character
if (i == len(text) - 1) {
w -= ((cast(f32)ch.advance / 64) - (ch.bearing.x + ch.size.x));
w -= ((cast(f32)ch.advance / 64) - (ch.bearing.x + ch.size.x))
}
// if we only have one character in the text, then remove the bearing width
if (len(text) == 1) {
w -= (ch.bearing.x);
w -= (ch.bearing.x)
}
h = max(h, max_bearing_h - ch.bearing.y + ch.size.y);
h = max(h, max_bearing_h - ch.bearing.y + ch.size.y)
if (text[i] == '\n') {
w = 0;
h += max_bearing_h;
w = 0
h += max_bearing_h
}
}
size.x = w * scale;
size.y = h * scale;
size.x = w * scale
size.y = h * scale
return size;
return size
}
draw_text :: proc(text: string, font_size: u32, constraints: UiConstraints, color: Color, alignment: Alignment) {
glyph_instance_list := get_glyph_instance_list_from_text(text, font_size, constraints, color, alignment);
glyph_instance_list := get_glyph_instance_list_from_text(text, font_size, constraints, color, alignment)
gl.ActiveTexture(gl.TEXTURE0);
gl.BindTexture(gl.TEXTURE_2D, zephr_ctx.font.atlas_tex_id);
gl.BindVertexArray(font_vao);
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, zephr_ctx.font.atlas_tex_id)
gl.BindVertexArray(font_vao)
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo);
gl.BufferData(gl.ARRAY_BUFFER, size_of(GlyphInstance) * len(glyph_instance_list), raw_data(glyph_instance_list), gl.DYNAMIC_DRAW);
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(GlyphInstance) * len(glyph_instance_list), raw_data(glyph_instance_list), gl.DYNAMIC_DRAW)
gl.DrawElementsInstanced(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil, cast(i32)len(glyph_instance_list))
gl.BindVertexArray(0);
gl.BindTexture(gl.TEXTURE_2D, 0);
gl.BindVertexArray(0)
gl.BindTexture(gl.TEXTURE_2D, 0)
delete(glyph_instance_list)
}
draw_text_batch :: proc(batch: ^GlyphInstanceList) {
gl.ActiveTexture(gl.TEXTURE0);
gl.BindTexture(gl.TEXTURE_2D, zephr_ctx.font.atlas_tex_id);
gl.BindVertexArray(font_vao);
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, zephr_ctx.font.atlas_tex_id)
gl.BindVertexArray(font_vao)
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo);
gl.BufferData(gl.ARRAY_BUFFER, size_of(GlyphInstance) * len(batch), raw_data(batch^), gl.DYNAMIC_DRAW);
gl.BindBuffer(gl.ARRAY_BUFFER, font_instance_vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(GlyphInstance) * len(batch), raw_data(batch^), gl.DYNAMIC_DRAW)
gl.DrawElementsInstanced(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil, cast(i32)len(batch))
gl.BindVertexArray(0);
gl.BindTexture(gl.TEXTURE_2D, 0);
gl.BindVertexArray(0)
gl.BindTexture(gl.TEXTURE_2D, 0)
delete(batch^)
}
get_glyph_instance_list_from_text :: proc(text: string, font_size: u32, constraints: UiConstraints, color: Color, alignment: Alignment) -> GlyphInstanceList {
constraints := constraints
use_shader(font_shader);
text_color := Vec4{ cast(f32)color.r / 255, cast(f32)color.g / 255, cast(f32)color.b / 255, cast(f32)color.a / 255 };
use_shader(font_shader)
text_color := Vec4{ cast(f32)color.r / 255, cast(f32)color.g / 255, cast(f32)color.b / 255, cast(f32)color.a / 255 }
set_mat4f(font_shader, "projection", zephr_ctx.projection)
rect: Rect
apply_constraints(&constraints, &rect.pos, &rect.size);
apply_constraints(&constraints, &rect.pos, &rect.size)
text_size := calculate_text_size(text, FONT_PIXEL_SIZE);
font_scale := cast(f32)font_size / FONT_PIXEL_SIZE * rect.size.x;
text_size := calculate_text_size(text, FONT_PIXEL_SIZE)
font_scale := cast(f32)font_size / FONT_PIXEL_SIZE * rect.size.x
apply_alignment(alignment, &constraints, Vec2{ text_size.x * font_scale, text_size.y * font_scale }, &rect.pos);
apply_alignment(alignment, &constraints, Vec2{ text_size.x * font_scale, text_size.y * font_scale }, &rect.pos)
model := identity()
scale(&model, Vec3{font_scale, font_scale, 1});
scale(&model, Vec3{font_scale, font_scale, 1})
// rotate around the center point of the text
translate(&model, Vec3{-text_size.x * font_scale / 2, -text_size.y * font_scale / 2, 0});
rotate(&model, constraints.rotation, Vec3{0, 0, 1});
translate(&model, Vec3{text_size.x *font_scale / 2, text_size.y * font_scale / 2, 0});
translate(&model, Vec3{-text_size.x * font_scale / 2, -text_size.y * font_scale / 2, 0})
rotate(&model, constraints.rotation, Vec3{0, 0, 1})
translate(&model, Vec3{text_size.x *font_scale / 2, text_size.y * font_scale / 2, 0})
translate(&model, Vec3{rect.pos.x, rect.pos.y, 0});
translate(&model, Vec3{rect.pos.x, rect.pos.y, 0})
max_bearing_h: f32 = 0;
max_bearing_h: f32 = 0
for i in 0..<len(text) {
ch := zephr_ctx.font.characters[text[i]];
max_bearing_h = max(max_bearing_h, ch.bearing.y);
ch := zephr_ctx.font.characters[text[i]]
max_bearing_h = max(max_bearing_h, ch.bearing.y)
}
first_char_bearing_w := zephr_ctx.font.characters[text[0]].bearing.x;
first_char_bearing_w := zephr_ctx.font.characters[text[0]].bearing.x
glyph_instance_list: GlyphInstanceList
reserve(&glyph_instance_list, 16)
@@ -369,21 +369,21 @@ get_glyph_instance_list_from_text :: proc(text: string, font_size: u32, constrai
// scale up or down the model matrix to get the desired font size.
// this way everything works out fine and we get to transform the text using the
// model matrix
c := 0;
x: u32 = 0;
y: f32 = 0;
c := 0
x: u32 = 0
y: f32 = 0
for c != len(text) {
ch := zephr_ctx.font.characters[text[c]]
// subtract the bearing width of the first character to remove the extra space
// at the start of the text and move every char to the left by that width
xpos := (cast(f32)x + (ch.bearing.x - first_char_bearing_w));
ypos := y + (text_size.y - ch.bearing.y - (text_size.y - max_bearing_h));
xpos := (cast(f32)x + (ch.bearing.x - first_char_bearing_w))
ypos := y + (text_size.y - ch.bearing.y - (text_size.y - max_bearing_h))
if (text[c] == '\n') {
x = 0;
y += max_bearing_h + (36 * LINE_HEIGHT);
c += 1;
continue;
x = 0
y += max_bearing_h + (36 * LINE_HEIGHT)
c += 1
continue
}
test_model := Mat4 {
@@ -402,18 +402,18 @@ get_glyph_instance_list_from_text :: proc(text: string, font_size: u32, constrai
append(&glyph_instance_list, instance)
x += (ch.advance >> 6);
c += 1;
x += (ch.advance >> 6)
c += 1
}
return glyph_instance_list;
return glyph_instance_list
}
add_text_instance :: proc(batch: ^GlyphInstanceList, text: string, font_size: u32, constraints: UiConstraints, color: Color, alignment: Alignment) {
glyph_instance_list := get_glyph_instance_list_from_text(text, font_size, constraints, color, alignment);
glyph_instance_list := get_glyph_instance_list_from_text(text, font_size, constraints, color, alignment)
append(batch, ..glyph_instance_list[:])
delete(glyph_instance_list);
delete(glyph_instance_list)
}