just setting up the environment

This commit is contained in:
vfs committed 2026-02-01 23:06:37 +01:00
1 parent 6d69fa8898
commit 49ce31ee2e
56 files changed
+1243 -12005

No files matched your search

+6 -1
View File
@@ -5,6 +5,11 @@ pub const c = @cImport({
});
pub const enet = @cImport({
@cDefine("WIN32_LEAN_AND_MEAN", "");
@cInclude("enet/enet.h");
});
pub const rl = @cImport({
@cInclude("raylib.h");
@cInclude("rlgl.h");
@cInclude("raymath.h");
});
+40 -45
View File
@@ -1,10 +1,9 @@
const std = @import("std");
const rl = @import("raylib");
const rl = @import("c.zig").rl;
const kb = @import("kb");
const ft = @import("mach-freetype");
const hb = @import("mach-harfbuzz");
const ft = @import("freetype");
const rp = @import("stb_rect_pack");
const ztracy = @import("ztracy");
const tracy = @import("tracy");
pub var ft_lib : ft.Library = undefined;
pub var shader : rl.Shader = undefined;
@@ -39,7 +38,6 @@ pub const Font = struct {
face : ft.Face,
glyphs : std.AutoHashMap(u32, Glyph),
kbts_font : kb.kbts_font,
// texture : c.Texture2D,
texture : rl.Texture2D,
kb_file_data : []u8,
@@ -57,7 +55,7 @@ pub const Font = struct {
var face = try ft_lib.createFaceMemory(file_data, 0);
var glyphs = std.AutoHashMap(u32, Glyph).init(allocator);
try face.setCharSize(0, size * 64, 0, 96 * @as(u16, @intFromFloat(rl.getWindowScaleDPI().y)));
try face.setCharSize(0, size * 64, 0, 96 * @as(u16, @intFromFloat(rl.GetWindowScaleDPI().y)));
try face.selectCharmap(.unicode);
var rects = try std.ArrayList(rp.stbrp_rect).initCapacity(allocator, 1024);
@@ -77,7 +75,7 @@ pub const Font = struct {
.width = ATLAS_SIZE,
.height = ATLAS_SIZE,
.mipmaps = 1,
.format = rl.PixelFormat.uncompressed_grayscale,
.format = rl.PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
};
for (0..face.numGlyphs()) |index| {
@@ -85,7 +83,7 @@ pub const Font = struct {
const bmp = face.glyph().bitmap();
try rects.append(allocator, .{
.id = @intCast(index),//@intCast(face.glyph().glyphIndex()),
.id = @intCast(index),
.w = @max(1, @as(c_int, @intCast(bmp.width()))),
.h = @max(1, @as(c_int, @intCast(bmp.rows()))),
.x = 0,
@@ -123,12 +121,12 @@ pub const Font = struct {
const width : i32 = @intCast(bmp.width());
const height : i32 = @intCast(bmp.rows());
const dpi_width : f32 = @as(f32, @floatFromInt(width)) / rl.getWindowScaleDPI().x;
const dpi_height : f32 = @as(f32, @floatFromInt(height)) / rl.getWindowScaleDPI().y;
const dpi_width : f32 = @as(f32, @floatFromInt(width)) / rl.GetWindowScaleDPI().x;
const dpi_height : f32 = @as(f32, @floatFromInt(height)) / rl.GetWindowScaleDPI().y;
const bearing_x : i32 = face.glyph().bitmapLeft();
const bearing_y : i32 = face.glyph().bitmapTop();
const dpi_bearing_x : f32 = @as(f32, @floatFromInt(bearing_x)) / rl.getWindowScaleDPI().x;
const dpi_bearing_y : f32 = @as(f32, @floatFromInt(bearing_y)) / rl.getWindowScaleDPI().y;
const dpi_bearing_x : f32 = @as(f32, @floatFromInt(bearing_x)) / rl.GetWindowScaleDPI().x;
const dpi_bearing_y : f32 = @as(f32, @floatFromInt(bearing_y)) / rl.GetWindowScaleDPI().y;
const descent : i32 = height - bearing_y;
const ascent : i32 = height - descent;
const dpi_descent : f32 = dpi_height - dpi_bearing_y;
@@ -166,7 +164,7 @@ pub const Font = struct {
try glyphs.put(glyph.index, glyph);
}
const texture = try rl.loadTextureFromImage(atlas);
const texture = rl.LoadTextureFromImage(atlas);
var kbts_font = kb.kbts_FontFromMemory(kb_file_data.ptr, @intCast(kb_file_data.len), 0, null, null);
@@ -174,9 +172,6 @@ pub const Font = struct {
std.log.info("[Error] [kb_text_shape] Failed to load the font.", .{});
}
const hb_blob = hb.Blob.initFromFile(filename);
_ = hb_blob;
return .{
.face = face,
.glyphs = glyphs,
@@ -190,7 +185,7 @@ pub const Font = struct {
self.glyphs.deinit();
self.face.deinit();
kb.kbts_FreeFont(&self.kbts_font);
self.texture.unload();
rl.UnloadTexture(self.texture);
allocator.free(self.kb_file_data);
}
@@ -207,17 +202,17 @@ pub const Font = struct {
if (text.len == 0)
return;
const zone = ztracy.ZoneNC(@src(), "render_text", 0x00_ff_00_00);
const zone = tracy.ZoneNC(@src(), "render_text", 0x00_ff_00_00);
defer zone.End();
rl.beginShaderMode(shader);
rl.gl.rlSetTexture(self.texture.id);
rl.gl.rlBegin(rl.gl.rl_quads);
rl.BeginShaderMode(shader);
rl.rlSetTexture(self.texture.id);
rl.rlBegin(rl.RL_QUADS);
if (nice_background) {
self.render_text(
text,
pos.add(.{.x = 3.0, .y = -3.0}),
rl.Vector2Add(pos, .{.x = 3.0, .y = -3.0}),
window_space,
rl.Color{ .r = 0, .g = 0, .b = 0, .a = 255 },
rl.Color{ .r = 0, .g = 0, .b = 0, .a = 0 },
@@ -231,7 +226,7 @@ pub const Font = struct {
_ = background;
const font_ascent : f32 = @floatFromInt(self.face.size().metrics().ascender >> 6);
const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
const dpi_font_ascent = font_ascent / rl.GetWindowScaleDPI().y;
render_pos.y += dpi_font_ascent;
@@ -243,7 +238,7 @@ pub const Font = struct {
return;
}
const zone2 = ztracy.ZoneNC(@src(), "ShapeUtf8", 0x00_ff_00_00);
const zone2 = tracy.ZoneNC(@src(), "ShapeUtf8", 0x00_ff_00_00);
kb.kbts_ShapeBegin(Context, kb.KBTS_DIRECTION_DONT_KNOW, kb.KBTS_LANGUAGE_DONT_KNOW);
kb.kbts_ShapeUtf8(Context, text.ptr, @intCast(text.len), kb.KBTS_USER_ID_GENERATION_MODE_CODEPOINT_INDEX);
kb.kbts_ShapeEnd(Context);
@@ -254,11 +249,11 @@ pub const Font = struct {
var Run = kb.kbts_run{};
while (my_kbts_ShapeRun(Context, &Run) != 0) {
const zone3 = ztracy.ZoneNC(@src(), "shape run", 0x00_ff_00_00);
const zone3 = tracy.ZoneNC(@src(), "shape run", 0x00_ff_00_00);
defer zone3.End();
if ((Run.Flags & kb.KBTS_BREAK_FLAG_LINE_HARD) != 0) {
const font_linegap : f32 = @floatFromInt(self.face.size().metrics().height >> 6);
const dpi_font_linegap = font_linegap / rl.getWindowScaleDPI().y;
const dpi_font_linegap = font_linegap / rl.GetWindowScaleDPI().y;
cursor_y += dpi_font_linegap;
cursor_x = 0;
@@ -266,7 +261,7 @@ pub const Font = struct {
var g : [*c]kb.kbts_glyph = null;
while (kb.kbts_GlyphIteratorNext(&Run.Glyphs, &g) != 0) {
const zone4 = ztracy.ZoneNC(@src(), "glyph iterator", 0x00_ff_00_00);
const zone4 = tracy.ZoneNC(@src(), "glyph iterator", 0x00_ff_00_00);
defer zone4.End();
const RunGlyph : *kb.kbts_glyph = g.?;
const CodepointIndex : i32 = RunGlyph.UserIdOrCodepointIndex;
@@ -277,23 +272,23 @@ pub const Font = struct {
const advance_x : f32 = @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6);
const advance_y : f32 = @floatFromInt(ft.mulFix(RunGlyph.AdvanceY, @intCast(self.face.size().metrics().y_scale)) >> 6);
const dpi_advance_x = advance_x / rl.getWindowScaleDPI().x;
const dpi_advance_y = advance_y / rl.getWindowScaleDPI().y;
const dpi_advance_x = advance_x / rl.GetWindowScaleDPI().x;
const dpi_advance_y = advance_y / rl.GetWindowScaleDPI().y;
const offset_x : f32 = @floatFromInt(ft.mulFix(RunGlyph.OffsetX, @intCast(self.face.size().metrics().x_scale)) >> 6);
const offset_y : f32 = @floatFromInt(ft.mulFix(RunGlyph.OffsetY, @intCast(self.face.size().metrics().y_scale)) >> 6);
const dpi_offset_x = offset_x / rl.getWindowScaleDPI().x;
const dpi_offset_y = offset_y / rl.getWindowScaleDPI().y;
const dpi_offset_x = offset_x / rl.GetWindowScaleDPI().x;
const dpi_offset_y = offset_y / rl.GetWindowScaleDPI().y;
if (self.glyphs.getPtr(RunGlyph.Id)) |glyph| {
var v0 = rl.Vector2.zero();
var v1 = rl.Vector2.zero();
var v0 = rl.Vector2{};
var v1 = rl.Vector2{};
v0 = render_pos.add(.{ .x = cursor_x + glyph.dpi_bearing_x + dpi_offset_x,
v0 = rl.Vector2Add(render_pos, .{ .x = cursor_x + glyph.dpi_bearing_x + dpi_offset_x,
.y = cursor_y - glyph.dpi_bearing_y - dpi_offset_y });
v1 = v0.add(rl.Vector2{ .x = glyph.dpi_width, .y = glyph.dpi_height });
v1 = rl.Vector2Add(v0, rl.Vector2{ .x = glyph.dpi_width, .y = glyph.dpi_height });
const p0 : rl.Vector4 = .{ .x = v0.x, .y = v0.y, .z = 0.0, .w = 1.0 };
const p1 : rl.Vector4 = .{ .x = v1.x, .y = v1.y, .z = 0.0, .w = 1.0 };
@@ -303,13 +298,13 @@ pub const Font = struct {
const st0 = glyph.st0;
const st1 = glyph.st1;
rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a);
rl.gl.rlNormal3f(0.0, 0.0, 1.0);
rl.rlColor4ub(colour.r, colour.g, colour.b, colour.a);
rl.rlNormal3f(0.0, 0.0, 1.0);
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);
rl.rlTexCoord2f(st0.x, st0.y); rl.rlVertex2f(p0.x, p0.y);
rl.rlTexCoord2f(st0.x, st1.y); rl.rlVertex2f(p0.x, p1.y);
rl.rlTexCoord2f(st1.x, st1.y); rl.rlVertex2f(p1.x, p1.y);
rl.rlTexCoord2f(st1.x, st0.y); rl.rlVertex2f(p1.x, p0.y);
} else {
std.log.warn("kb_text_shape found the glyph, but we didn't load it from the font. index {}", .{RunGlyph.Id});
@@ -320,9 +315,9 @@ pub const Font = struct {
}
}
rl.gl.rlEnd();
rl.gl.rlSetTexture(0);
rl.endShaderMode();
rl.rlEnd();
rl.rlSetTexture(0);
rl.EndShaderMode();
}
pub fn size_row(self: *Font, str: []const u8, n: i32, max_width: f32) struct {rl.Vector2, f32, i32} {
@@ -373,7 +368,7 @@ pub const Font = struct {
};
fn my_kbts_ShapeRun(Context: ?*kb.kbts_shape_context, Run: [*c]kb.kbts_run) c_int {
const zone = ztracy.ZoneNC(@src(), "my_kbts_ShapeRun", 0x00_ff_00_00);
const zone = tracy.ZoneNC(@src(), "my_kbts_ShapeRun", 0x00_ff_00_00);
defer zone.End();
return kb.kbts_ShapeRun(Context, Run);
}
+41 -37
View File
@@ -1,15 +1,15 @@
const std = @import("std");
const zm = @import("zmath");
const rl = @import("raylib");
const bufzilla = @import("bufzilla");
const ft = @import("mach-freetype");
const ztracy = @import("ztracy");
const ft = @import("freetype");
const tracy = @import("tracy");
const kb = @import("kb");
const shared = @import("shared");
const c = @import("c.zig").c;
const enet = @import("c.zig").enet;
const rl = @import("c.zig").rl;
const client = @import("client.zig");
const entity = @import("entity.zig");
const misc = @import("misc.zig");
@@ -32,7 +32,7 @@ var dbg_allocator = std.heap.DebugAllocator(.{}){};
// var connection: ?std.net.Stream = undefined;
pub fn main() !void {
const tracy_zone = ztracy.ZoneNC(@src(), "main", 0x00_ff_00_00);
const tracy_zone = tracy.ZoneNC(@src(), "main", 0x00_ff_00_00);
defer tracy_zone.End();
const allocator = dbg_allocator.allocator();
@@ -56,20 +56,21 @@ pub fn main() !void {
defer enet.enet_deinitialize();
rl.setConfigFlags(.{ .window_highdpi = true, .vsync_hint = false });
rl.initWindow(screen_width, screen_height, "zzz");
defer rl.closeWindow();
// rl.SetConfigFlags(.{ .window_highdpi = true, .vsync_hint = false });
rl.SetConfigFlags(rl.FLAG_WINDOW_HIGHDPI);
rl.InitWindow(screen_width, screen_height, "zzz");
defer rl.CloseWindow();
font.ft_lib = try ft.Library.init();
const sizes = [_]i32{
26,
60,
};
var vollkorn: [sizes.len]font.Font = undefined;
var inconsolata: [sizes.len]font.Font = undefined;
var arabic: [sizes.len]font.Font = undefined;
// var japanese: [sizes.len]font.Font = undefined;
// var inconsolata: [sizes.len]font.Font = undefined;
// var arabic: [sizes.len]font.Font = undefined;
// // var japanese: [sizes.len]font.Font = undefined;
for (sizes, 0..) |pt, i| {
vollkorn[i] = try font.Font.init(
@@ -77,16 +78,16 @@ pub fn main() !void {
pt,
allocator
);
inconsolata[i] = try font.Font.init(
"assets/fonts/Inconsolata/static/Inconsolata-Regular.ttf",
@intCast(pt),
allocator
);
arabic[i] = try font.Font.init(
"assets/fonts/Amiri/Amiri-Regular.ttf",
@intCast(pt),
allocator
);
// inconsolata[i] = try font.Font.init(
// "assets/fonts/Inconsolata/static/Inconsolata-Regular.ttf",
// @intCast(pt),
// allocator
// );
// arabic[i] = try font.Font.init(
// "assets/fonts/Amiri/Amiri-Regular.ttf",
// @intCast(pt),
// allocator
// );
// japanese[i] = try font.Font.init(
// "assets/fonts/Noto_Sans_JP/static/NotoSansJP-Regular.ttf",
// @intCast(pt),
@@ -96,12 +97,12 @@ pub fn main() !void {
defer {
for (&vollkorn) |*f| f.deinit(allocator);
for (&inconsolata) |*f| f.deinit(allocator);
for (&arabic) |*f| f.deinit(allocator);
// for (&inconsolata) |*f| f.deinit(allocator);
// for (&arabic) |*f| f.deinit(allocator);
// for (&japanese) |*f| f.deinit(allocator);
}
font.shader = try rl.loadShader(null, "assets/text.frag");
font.shader = rl.LoadShader(null, "assets/text.frag");
// const test_shader1 = try c.LoadShader(null, "assets/test_1.frag");
// const test_shader2 = try c.LoadShader(null, "assets/test_2.frag");
@@ -181,15 +182,15 @@ pub fn main() !void {
// rl.playMusicStream(music);
// std.log.info("is music playing? {}", .{rl.isMusicStreamPlaying(music)});
var old_time : f32 = @floatCast(rl.getTime());
// var old_time : f32 = @floatCast(rl.getTime());
var old_time : f32 = @floatCast(rl.GetTime());
while (!rl.windowShouldClose()) {
const tracy_main_zone = ztracy.ZoneNC(@src(), "loop", 0x00_ff_00_00);
while (!rl.WindowShouldClose()) {
const tracy_main_zone = tracy.ZoneNC(@src(), "loop", 0x00_ff_00_00);
defer tracy_main_zone.End();
const new_time : f32 = @floatCast(rl.getTime());
// const new_time : f32 = @floatCast(rl.getTime());
const new_time : f32 = @floatCast(rl.GetTime());
var frame_time = new_time - old_time;
old_time = new_time;
@@ -223,7 +224,7 @@ pub fn main() !void {
// },
// };
rl.pollInputEvents();
rl.PollInputEvents();
// if (peer.state() == .connected) {
// const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
@@ -239,8 +240,10 @@ pub fn main() !void {
gt += dt * k;
}
rl.beginDrawing();
rl.clearBackground(.sky_blue);
const drawing_zone = tracy.ZoneNC(@src(), "Drawing", 0x00_ff_00_00);
rl.BeginDrawing();
rl.ClearBackground(rl.SKYBLUE);
// const lorem = @embedFile("embeds/lorem.txt");
@@ -248,8 +251,8 @@ pub fn main() !void {
"The night is long and cold outside.",
rl.Vector2{ .x = 0, .y = 0 },
true,
.white,
.blank,
rl.WHITE,
rl.BLANK,
false,
true
);
@@ -488,15 +491,16 @@ pub fn main() !void {
// rl.drawSphere(.{ .x = 0, .y = 0, .z = 0 }, 1, .red);
// rl.endMode3D();
rl.endDrawing();
rl.EndDrawing();
drawing_zone.End();
const swap_zone = ztracy.ZoneNC(@src(), "swapScreenBuffer", 0x00_ff_00_00);
rl.swapScreenBuffer();
const swap_zone = tracy.ZoneNC(@src(), "swapScreenBuffer", 0x00_ff_00_00);
rl.SwapScreenBuffer();
swap_zone.End();
frame += 1;
ztracy.FrameMark();
tracy.FrameMark();
}
}
+7 -17
View File
@@ -1,4 +1,4 @@
const rl = @import("raylib");
const rl = @import("c.zig").rl;
const FPS_CAPTURE_FRAMES_COUNT : i32 = 30; // 30 captures
const FPS_AVERAGE_TIME_SECONDS : f32 = 0.5; // 500 milliseconds
@@ -10,33 +10,23 @@ var average : f32 = 0;
var last : f32 = 0;
pub fn drawFPS(posX : i32, posY : i32, fpsFrame : f32) void {
var color : rl.Color = .lime; // Good FPS
var color : rl.Color = rl.LIME; // Good FPS
const fps : i32 = getFPS(fpsFrame);
if ((fps < 30) and (fps >= 15)) {color = .orange;} // Warning FPS
else if (fps < 15) color = .red; // Low FPS
if ((fps < 30) and (fps >= 15)) {color = rl.ORANGE;} // Warning FPS
else if (fps < 15) color = rl.RED; // Low FPS
rl.drawText(rl.textFormat("%2i FPS", .{fps}), posX, posY, 20, color);
rl.DrawText(rl.TextFormat("%2i FPS", fps), posX, posY, 20, color);
}
pub fn getFPS(fpsFrame : f32) i32 {
var fps : i32 = 0;
// if we reset the window, reset the FPS info
// if (frame == 0)
// {
// average = 0;
// last = 0;
// index = 0;
// for (0..FPS_CAPTURE_FRAMES_COUNT) |i| history[i] = 0;
// }
if (fpsFrame == 0) return 0;
if ((rl.getTime() - last) > FPS_STEP)
if ((rl.GetTime() - last) > FPS_STEP)
{
last = @floatCast(rl.getTime());
last = @floatCast(rl.GetTime());
index = (index + 1) % FPS_CAPTURE_FRAMES_COUNT;
average -= history[index];
history[index] = fpsFrame / FPS_CAPTURE_FRAMES_COUNT;