font rendering baby!

This commit is contained in:
Vicente Ferrari Smith 2026-01-18 00:55:32 +01:00
parent 297f56885c
commit 57a289e86d
4 changed files with 35 additions and 17 deletions

View File

@ -10,6 +10,7 @@
"program": "$ZED_WORKTREE_ROOT/zig-out/bin/client", "program": "$ZED_WORKTREE_ROOT/zig-out/bin/client",
"cwd": "$ZED_WORKTREE_ROOT/", "cwd": "$ZED_WORKTREE_ROOT/",
"build": "zig build", "build": "zig build",
"sourceLanguages": ["zig"],
}, },
{ {
"label": "Debug Zig Server", "label": "Debug Zig Server",
@ -18,6 +19,7 @@
"program": "$ZED_WORKTREE_ROOT/zig-out/bin/server", "program": "$ZED_WORKTREE_ROOT/zig-out/bin/server",
"cwd": "$ZED_WORKTREE_ROOT/", "cwd": "$ZED_WORKTREE_ROOT/",
"build": "zig build", "build": "zig build",
"sourceLanguages": ["zig"],
}, },
// { // {
// "adapter": "CodeLLDB", // "adapter": "CodeLLDB",

View File

@ -17,11 +17,11 @@ out vec4 finalColor;
void main() void main()
{ {
// // Texel color fetching from texture sampler // // Texel color fetching from texture sampler
// vec4 texelColor0 = texture(texture0, fragTexCoord); vec4 texelColor0 = texture(texture0, fragTexCoord);
// vec4 texelColor1 = texture(texture1, fragTexCoord); // vec4 texelColor1 = texture(texture1, fragTexCoord);
// float x = fract(fragTexCoord.s); // float x = fract(fragTexCoord.s);
// float final = smoothstep(divider - 0.1, divider + 0.1, x); // 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);
} }

View File

@ -6,6 +6,7 @@ const ft = @import("freetype");
const rp = @import("stb_rect_pack"); const rp = @import("stb_rect_pack");
pub var ft_lib : ft.Library = undefined; pub var ft_lib : ft.Library = undefined;
pub var shader : rl.Shader = undefined;
const ATLAS_SIZE = 4096; const ATLAS_SIZE = 4096;
@ -146,8 +147,8 @@ pub const Font = struct {
.descent = @as(i32, @intCast(face.glyph().bitmap().rows())) - face.glyph().bitmapTop(), .descent = @as(i32, @intCast(face.glyph().bitmap().rows())) - face.glyph().bitmapTop(),
.ascent = ascent, .ascent = ascent,
.advance = @intCast(face.glyph().advance().x >> 6), .advance = @intCast(face.glyph().advance().x >> 6),
.st0 = rl.Vector2.init((fx + 0.5) / fs, (fy - 0.5) / fs), .st0 = .{.x = (fx + 0.5) / fs, .y = (fy - fh) / fs},
.st1 = .{.x = (fx + fw) / fs, .y = (fy - fh) / fs}, .st1 = .{.x = (fx + fw) / fs, .y = (fy - 0.5) / fs},
}; };
try glyphs.put(glyph.index, glyph); try glyphs.put(glyph.index, glyph);
@ -230,6 +231,10 @@ pub const Font = struct {
var x_offset : f32 = 0; var x_offset : f32 = 0;
var y_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. // Layout runs naively left to right.
var Run = kb.kbts_run{}; var Run = kb.kbts_run{};
while (kb.kbts_ShapeRun(Context, &Run) != 0) { 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.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); // glyph : *Glyph = table_find_pointer(*text.font.glyphs, RunGlyph.Id);
if (self.glyphs.getPtr(RunGlyph.Id)) |glyph| { if (self.glyphs.getPtr(RunGlyph.Id)) |glyph| {
var v0 = rl.Vector2.zero(); 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)}; // 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.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.rlNormal3f(0.0, 0.0, 1.0);
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.rlTexCoord2f(st1.x, st0.y); rl.gl.rlVertex2f(p1.x, p0.y); 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.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.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.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 { } 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; 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} { pub fn size_row(self: *Font, str: []const u8, n: i32, max_width: f32) struct {rl.Vector2, f32, i32} {

View File

@ -48,7 +48,7 @@ pub fn main() !void {
var f = try font.Font.init("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 48, allocator); var f = try font.Font.init("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 48, allocator);
defer f.deinit(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 img = rl.genImageColor(32, 32, .blank);
// const tx = try rl.loadTextureFromImage(img); // const tx = try rl.loadTextureFromImage(img);
@ -176,19 +176,19 @@ pub fn main() !void {
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
rl.clearBackground(.black); rl.clearBackground(.sky_blue);
rl.beginDrawing(); rl.beginDrawing();
rl.beginShaderMode(shader); // rl.beginShaderMode(shader);
f.render_text( f.render_text(
"H", "Hello, Sa ilor!",
rl.Vector2.init(400, 400), rl.Vector2.init(400, 400),
true, true,
rl.Color.white, rl.Color.white,
rl.Color.blank, rl.Color.blank,
true, false,
true true
); );
@ -217,7 +217,7 @@ pub fn main() !void {
// } // }
// rl.gl.rlEnd(); // rl.gl.rlEnd();
rl.endShaderMode(); // rl.endShaderMode();
// f.texture.drawPro( // f.texture.drawPro(
// .{.x = 0, .y = 0, .width = 4096, .height = 4096}, // .{.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.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);
//rl.drawRectangleLines(0, 0, 100, 100, .red); //rl.drawRectangleLines(0, 0, 100, 100, .red);
misc.drawFPS(0, 0, frame_time, frame); // misc.drawFPS(0, 0, frame_time, frame);
//elf.draw(); //elf.draw();
@ -274,6 +274,8 @@ fn init() !void {
var stdout_buffer: [1024]u8 = undefined; var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
stdout = &stdout_writer.interface; stdout = &stdout_writer.interface;
// font.shader = try rl.loadShader(null, "assets/test.frag");
} }
fn connect() !void { fn connect() !void {