From 57a289e86df5ee46d04405e0cc0601e081365bd6 Mon Sep 17 00:00:00 2001 From: Vicente Ferrari Smith Date: Sun, 18 Jan 2026 00:55:32 +0100 Subject: [PATCH] font rendering baby! --- .zed/debug.json | 2 ++ assets/test.frag | 4 ++-- src/client/font.zig | 30 ++++++++++++++++++++++-------- src/client/main.zig | 16 +++++++++------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.zed/debug.json b/.zed/debug.json index 3350de0..35460a1 100644 --- a/.zed/debug.json +++ b/.zed/debug.json @@ -10,6 +10,7 @@ "program": "$ZED_WORKTREE_ROOT/zig-out/bin/client", "cwd": "$ZED_WORKTREE_ROOT/", "build": "zig build", + "sourceLanguages": ["zig"], }, { "label": "Debug Zig Server", @@ -18,6 +19,7 @@ "program": "$ZED_WORKTREE_ROOT/zig-out/bin/server", "cwd": "$ZED_WORKTREE_ROOT/", "build": "zig build", + "sourceLanguages": ["zig"], }, // { // "adapter": "CodeLLDB", diff --git a/assets/test.frag b/assets/test.frag index b73c8b8..959a76b 100644 --- a/assets/test.frag +++ b/assets/test.frag @@ -17,11 +17,11 @@ out vec4 finalColor; void main() { // // Texel color fetching from texture sampler - // vec4 texelColor0 = texture(texture0, fragTexCoord); + vec4 texelColor0 = texture(texture0, fragTexCoord); // vec4 texelColor1 = texture(texture1, fragTexCoord); // float x = fract(fragTexCoord.s); // float final = smoothstep(divider - 0.1, divider + 0.1, x); - finalColor = vec4(1, 1, 0.0, 1.0); + finalColor = vec4(1, 1, 1, texelColor0.r);//xelColor0;//vec4(1, 1, 0.0, 1.0); } diff --git a/src/client/font.zig b/src/client/font.zig index 8bfbc01..205efc5 100644 --- a/src/client/font.zig +++ b/src/client/font.zig @@ -6,6 +6,7 @@ const ft = @import("freetype"); const rp = @import("stb_rect_pack"); pub var ft_lib : ft.Library = undefined; +pub var shader : rl.Shader = undefined; const ATLAS_SIZE = 4096; @@ -146,8 +147,8 @@ pub const Font = struct { .descent = @as(i32, @intCast(face.glyph().bitmap().rows())) - face.glyph().bitmapTop(), .ascent = ascent, .advance = @intCast(face.glyph().advance().x >> 6), - .st0 = rl.Vector2.init((fx + 0.5) / fs, (fy - 0.5) / fs), - .st1 = .{.x = (fx + fw) / fs, .y = (fy - fh) / fs}, + .st0 = .{.x = (fx + 0.5) / fs, .y = (fy - fh) / fs}, + .st1 = .{.x = (fx + fw) / fs, .y = (fy - 0.5) / fs}, }; try glyphs.put(glyph.index, glyph); @@ -230,6 +231,10 @@ pub const Font = struct { var x_offset : f32 = 0; var y_offset : f32 = 0; + rl.beginShaderMode(shader); + rl.gl.rlSetTexture(self.texture.id); + rl.gl.rlBegin(rl.gl.rl_quads); + // Layout runs naively left to right. var Run = kb.kbts_run{}; while (kb.kbts_ShapeRun(Context, &Run) != 0) { @@ -246,6 +251,8 @@ pub const Font = struct { std.log.info("aah it's wrong!! idk how to handle the error rn!!", .{}); } + std.log.debug("RunGlyph: {}", .{RunGlyph}); + // glyph : *Glyph = table_find_pointer(*text.font.glyphs, RunGlyph.Id); if (self.glyphs.getPtr(RunGlyph.Id)) |glyph| { var v0 = rl.Vector2.zero(); @@ -282,18 +289,25 @@ pub const Font = struct { // t1 := t0 + .{cast(float, glyph.width / ATLAS_SIZE), cast(float, glyph.height / ATLAS_SIZE)}; // } - rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.y); - rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st1.x, st1.y); rl.gl.rlVertex2f(p1.x, p1.y); - rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st0.x, st1.y); rl.gl.rlVertex2f(p0.x, p1.y); + rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); + rl.gl.rlNormal3f(0.0, 0.0, 1.0); - // rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st1.x, st0.y); rl.gl.rlVertex2f(p1.x, p0.y); - // rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st1.x, st1.y); rl.gl.rlVertex2f(p1.x, p1.y); - // rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a); rl.gl.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.y); + rl.gl.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.y); + rl.gl.rlTexCoord2f(st0.x, st1.y); rl.gl.rlVertex2f(p0.x, p1.y); + rl.gl.rlTexCoord2f(st1.x, st1.y); rl.gl.rlVertex2f(p1.x, p1.y); + rl.gl.rlTexCoord2f(st1.x, st0.y); rl.gl.rlVertex2f(p1.x, p0.y); } else { + std.log.debug("x_offset: {}", .{x_offset}); + x_offset += @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6); + y_offset += @floatFromInt(ft.mulFix(RunGlyph.AdvanceY, @intCast(self.face.size().metrics().y_scale)) >> 6); continue; } } } + + rl.gl.rlEnd(); + rl.gl.rlSetTexture(0); + // rl.endShaderMode(); } pub fn size_row(self: *Font, str: []const u8, n: i32, max_width: f32) struct {rl.Vector2, f32, i32} { diff --git a/src/client/main.zig b/src/client/main.zig index 6bb6c76..b658b3d 100644 --- a/src/client/main.zig +++ b/src/client/main.zig @@ -48,7 +48,7 @@ pub fn main() !void { var f = try font.Font.init("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 48, allocator); defer f.deinit(allocator); - const shader = try rl.loadShader(null, "assets/test.frag"); + font.shader = try rl.loadShader(null, "assets/test.frag"); // const img = rl.genImageColor(32, 32, .blank); // const tx = try rl.loadTextureFromImage(img); @@ -176,19 +176,19 @@ pub fn main() !void { // Draw //---------------------------------------------------------------------------------- - rl.clearBackground(.black); + rl.clearBackground(.sky_blue); rl.beginDrawing(); - rl.beginShaderMode(shader); + // rl.beginShaderMode(shader); f.render_text( - "H", + "Hello, Sa ilor!", rl.Vector2.init(400, 400), true, rl.Color.white, rl.Color.blank, - true, + false, true ); @@ -217,7 +217,7 @@ pub fn main() !void { // } // rl.gl.rlEnd(); - rl.endShaderMode(); + // rl.endShaderMode(); // f.texture.drawPro( // .{.x = 0, .y = 0, .width = 4096, .height = 4096}, @@ -253,7 +253,7 @@ pub fn main() !void { //rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white); //rl.drawRectangleLines(0, 0, 100, 100, .red); - misc.drawFPS(0, 0, frame_time, frame); + // misc.drawFPS(0, 0, frame_time, frame); //elf.draw(); @@ -274,6 +274,8 @@ fn init() !void { var stdout_buffer: [1024]u8 = undefined; var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); stdout = &stdout_writer.interface; + + // font.shader = try rl.loadShader(null, "assets/test.frag"); } fn connect() !void {