This commit is contained in:
Vicente Ferrari Smith 2026-01-29 23:00:03 +01:00
parent c41f8866d9
commit 6d69fa8898
6 changed files with 224 additions and 140 deletions

220
build.zig
View File

@ -46,94 +46,166 @@ pub fn build(b: *std.Build) void {
}); });
server.root_module.strip = true; server.root_module.strip = true;
const ztracy = b.dependency( // ztracy
"ztracy", {
.{ const ztracy = b.dependency(
.enable_ztracy = true, "ztracy",
.enable_fibers = false, .{
.on_demand = false, .enable_ztracy = true,
.enable_fibers = false,
.on_demand = false,
}
);
client.root_module.addImport("ztracy", ztracy.module("root"));
client.linkLibrary(ztracy.artifact("tracy"));
}
// mach-freetype
{
const mach_freetype = b.dependency("mach_freetype", .{
.target = target,
.optimize = optimize,
});
client.root_module.addImport("mach-freetype", mach_freetype.module("mach-freetype"));
client.root_module.addImport("mach-harfbuzz", mach_freetype.module("mach-harfbuzz"));
}
// kb_text_shape
{
client.root_module.addCSourceFile(.{
.file = b.path("vendor/kb_text_shape/kb_text_shape.h"),
.language = .c,
.flags = &.{
"-DKB_TEXT_SHAPE_IMPLEMENTATION",
"-O3", // Maximum optimization
// "-ffast-math", // Allows algebraic reorganizations (risky for precision, great for speed)
// "-fno-plt", // Reduces overhead for library calls
// "-march=native", // Use every instruction your CPU has
// "-flto", // Enable LTO for the C side specifically
},
});
const kb_text_shape = b.addTranslateC(.{
.root_source_file = b.path("vendor/kb_text_shape/kb_text_shape.h"),
.target = target,
.optimize = optimize,
});
client.root_module.addImport("kb", kb_text_shape.createModule());
}
// stb_rect_pack
{
client.root_module.addCSourceFile(.{ .file = b.path("vendor/stb/stb_rect_pack.h"), .language = .c, .flags = &.{} });
const stb_rect_pack = b.addTranslateC(.{
.root_source_file = b.path("vendor/stb/stb_rect_pack.h"),
.target = target,
.optimize = optimize,
});
client.root_module.addImport("stb_rect_pack", stb_rect_pack.createModule());
}
// zmath
{
const zmath = b.dependency(
"zmath",
.{
.target = target,
.optimize = optimize,
}
);
client.root_module.addImport("zmath", zmath.module("root"));
server.root_module.addImport("zmath", zmath.module("root"));
shared.addImport("zmath", zmath.module("root"));
}
// ENet
{
client.addCSourceFiles(.{
.root = b.path("vendor/enet"),
.files = &[_][]const u8{
"callbacks.c",
"compress.c",
"host.c",
"list.c",
"packet.c",
"peer.c",
"protocol.c",
"unix.c",
"win32.c",
},
});
if (target.result.os.tag == .windows) {
// client.root_module.linkSystemLibrary("ws2_32", .{});
// client.root_module.linkSystemLibrary("winmm", .{});
} }
);
client.root_module.addImport("ztracy", ztracy.module("root"));
client.linkLibrary(ztracy.artifact("tracy"));
const mach_freetype = b.dependency("mach_freetype", .{ client.root_module.addIncludePath(b.path("vendor/enet/include/"));
.target = target,
.optimize = optimize,
});
client.root_module.addImport("mach-freetype", mach_freetype.module("mach-freetype"));
client.root_module.addImport("mach-harfbuzz", mach_freetype.module("mach-harfbuzz"));
client.root_module.addCSourceFile(.{ server.addCSourceFiles(.{
.file = b.path("vendor/kb_text_shape/kb_text_shape.h"), .root = b.path("vendor/enet"),
.language = .c, .files = &[_][]const u8{
.flags = &.{ "callbacks.c",
"-DKB_TEXT_SHAPE_IMPLEMENTATION", "compress.c",
"-O3", // Maximum optimization "host.c",
// "-ffast-math", // Allows algebraic reorganizations (risky for precision, great for speed) "list.c",
// "-fno-plt", // Reduces overhead for library calls "packet.c",
// "-march=native", // Use every instruction your CPU has "peer.c",
// "-flto", // Enable LTO for the C side specifically "protocol.c",
}, "unix.c",
}); "win32.c",
const kb_text_shape = b.addTranslateC(.{ },
.root_source_file = b.path("vendor/kb_text_shape/kb_text_shape.h"), });
.target = target,
.optimize = optimize,
});
client.root_module.addImport("kb", kb_text_shape.createModule());
client.root_module.addCSourceFile(.{ .file = b.path("vendor/stb/stb_rect_pack.h"), .language = .c, .flags = &.{} }); if (target.result.os.tag == .windows) {
const stb_rect_pack = b.addTranslateC(.{ server.root_module.linkSystemLibrary("ws2_32", .{});
.root_source_file = b.path("vendor/stb/stb_rect_pack.h"), server.root_module.linkSystemLibrary("winmm", .{});
.target = target, }
.optimize = optimize,
});
client.root_module.addImport("stb_rect_pack", stb_rect_pack.createModule());
const zmath = b.dependency( server.root_module.addIncludePath(b.path("vendor/enet/include/"));
"zmath",
// const znet = b.dependency(
// "znet",
// .{
// .target = target,
// .optimize = optimize,
// }
// );
// client.root_module.addImport("znet", znet.module("znet"));
// server.root_module.addImport("znet", znet.module("znet"));
}
// Bufzilla
{
const bufzilla = b.dependency(
"bufzilla",
.{ .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
} });
); client.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
client.root_module.addImport("zmath", zmath.module("root")); server.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
server.root_module.addImport("zmath", zmath.module("root")); }
shared.addImport("zmath", zmath.module("root"));
const znet = b.dependency( // Raylib
"znet", {
.{ const raylib_dep = b.dependency("raylib_zig", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
} });
);
client.root_module.addImport("znet", znet.module("znet"));
server.root_module.addImport("znet", znet.module("znet"));
const bufzilla = b.dependency( const raylib = raylib_dep.module("raylib"); // main raylib module
"bufzilla", const raygui = raylib_dep.module("raygui"); // raygui module
.{ const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
.target = target, raylib_artifact.root_module.addCMacro("SUPPORT_CUSTOM_FRAME_CONTROL", "");
.optimize = optimize,
});
client.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
server.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
const raylib_dep = b.dependency("raylib_zig", .{ client.linkLibrary(raylib_artifact);
.target = target, client.root_module.addImport("raylib", raylib);
.optimize = optimize, client.root_module.addImport("raygui", raygui);
}); }
const raylib = raylib_dep.module("raylib"); // main raylib module client.root_module.link_libc = true;
const raygui = raylib_dep.module("raygui"); // raygui module server.root_module.link_libc = true;
const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
raylib_artifact.root_module.addCMacro("SUPPORT_CUSTOM_FRAME_CONTROL", "");
client.linkLibrary(raylib_artifact);
client.root_module.addImport("raylib", raylib);
client.root_module.addImport("raygui", raygui);
b.installArtifact(client); b.installArtifact(client);
b.installArtifact(server); b.installArtifact(server);

View File

@ -3,3 +3,8 @@ pub const c = @cImport({
@cInclude("raymath.h"); @cInclude("raymath.h");
@cInclude("rlgl.h"); @cInclude("rlgl.h");
}); });
pub const enet = @cImport({
@cDefine("WIN32_LEAN_AND_MEAN", "");
@cInclude("enet/enet.h");
});

View File

@ -1,6 +1,5 @@
const std = @import("std"); const std = @import("std");
const zm = @import("zmath"); const zm = @import("zmath");
const znet = @import("znet");
const rl = @import("raylib"); const rl = @import("raylib");
const bufzilla = @import("bufzilla"); const bufzilla = @import("bufzilla");
const ft = @import("mach-freetype"); const ft = @import("mach-freetype");
@ -9,6 +8,8 @@ const kb = @import("kb");
const shared = @import("shared"); const shared = @import("shared");
const c = @import("c.zig").c;
const enet = @import("c.zig").enet;
const client = @import("client.zig"); const client = @import("client.zig");
const entity = @import("entity.zig"); const entity = @import("entity.zig");
const misc = @import("misc.zig"); const misc = @import("misc.zig");
@ -48,9 +49,14 @@ pub fn main() !void {
_ = try stdout.write("Hello, Client!\n"); _ = try stdout.write("Hello, Client!\n");
try znet.init(); if (enet.enet_initialize() != 0) {
defer znet.deinit(); std.log.info("Failed to load ENet", .{});
// rl.setConfigFlags(.{ .window_highdpi = true, .vsync_hint = true }); return;
}
defer enet.enet_deinitialize();
rl.setConfigFlags(.{ .window_highdpi = true, .vsync_hint = false });
rl.initWindow(screen_width, screen_height, "zzz"); rl.initWindow(screen_width, screen_height, "zzz");
defer rl.closeWindow(); defer rl.closeWindow();
@ -107,14 +113,14 @@ pub fn main() !void {
// const tx = try rl.loadTextureFromImage(img); // const tx = try rl.loadTextureFromImage(img);
// rl.unloadImage(img); // rl.unloadImage(img);
const host = try znet.Host.init(.{ // const host = try znet.Host.init(.{
.addr = null, // .addr = null,
.peer_limit = 1, // .peer_limit = 1,
.channel_limit = .max, // .channel_limit = .max,
.incoming_bandwidth = .unlimited, // .incoming_bandwidth = .unlimited,
.outgoing_bandwidth = .unlimited, // .outgoing_bandwidth = .unlimited,
}); // });
defer host.deinit(); // defer host.deinit();
// const peer = try host.connect(.{ // const peer = try host.connect(.{
// .addr = try .init(.{ // .addr = try .init(.{
@ -195,27 +201,27 @@ pub fn main() !void {
// std.log.info("t: {}", .{t}); // std.log.info("t: {}", .{t});
while (try host.service(0)) |event| switch (event) { // while (try host.service(0)) |event| switch (event) {
.connect => |data| { // .connect => |data| {
_ = data; // _ = data;
// std.log.info("{}", .{data.peer}); // // std.log.info("{}", .{data.peer});
}, // },
.disconnect => |data| { // .disconnect => |data| {
_ = data; // _ = data;
// std.log.info("{}", .{data.peer}); // // std.log.info("{}", .{data.peer});
}, // },
.receive => |data| { // .receive => |data| {
defer data.packet.deinit(); // defer data.packet.deinit();
const slice = data.packet.dataSlice(); // const slice = data.packet.dataSlice();
var reader = bufzilla.Reader(.{}).init(slice); // var reader = bufzilla.Reader(.{}).init(slice);
const id = try reader.readPath("id"); // const id = try reader.readPath("id");
std.log.info("{any}", .{id}); // std.log.info("{any}", .{id});
}, // },
}; // };
rl.pollInputEvents(); rl.pollInputEvents();

3
src/server/c.zig Normal file
View File

@ -0,0 +1,3 @@
pub const c = @cImport({
// @cInclude("enet/enet.h");
});

View File

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const znet = @import("znet");
const bufzilla = @import("bufzilla"); const bufzilla = @import("bufzilla");
const shared = @import("shared"); const shared = @import("shared");
@ -30,14 +29,14 @@ pub fn spawn(chunk: *shared.chunk.Chunk, comptime T: type, allocator: std.mem.Al
var writer = bufzilla.Writer.init(&fixed); var writer = bufzilla.Writer.init(&fixed);
try writer.writeAny(msg); try writer.writeAny(msg);
const encoded = fixed.buffered(); // const encoded = fixed.buffered();
const packet = try znet.Packet.init(encoded, 0, .reliable); // const packet = try znet.Packet.init(encoded, 0, .reliable);
var iterator = server.host.iterPeers(); // var iterator = server.host.iterPeers();
while (iterator.next()) |peer| { // while (iterator.next()) |peer| {
if (peer.state() == .connected) { // if (peer.state() == .connected) {
try peer.send(packet); // try peer.send(packet);
} // }
} // }
} }

View File

@ -1,6 +1,5 @@
const std = @import("std"); const std = @import("std");
const zm = @import("zmath"); const zm = @import("zmath");
const znet = @import("znet");
const shared = @import("shared"); const shared = @import("shared");
@ -28,19 +27,19 @@ pub fn main() !void {
try stdout.flush(); try stdout.flush();
try znet.init(); // try znet.init();
defer znet.deinit(); // defer znet.deinit();
server.host = try znet.Host.init(.{ // server.host = try znet.Host.init(.{
.addr = try .init(.{ // .addr = try .init(.{
.ip = .any, // .ip = .any,
.port = .{ .uint = 5000 }, // .port = .{ .uint = 5000 },
}), // }),
.peer_limit = 32, // .peer_limit = 32,
.channel_limit = .max, // .channel_limit = .max,
.incoming_bandwidth = .unlimited, // .incoming_bandwidth = .unlimited,
.outgoing_bandwidth = .unlimited, // .outgoing_bandwidth = .unlimited,
}); // });
// const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT); // const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
// var tcp_server = try address.listen(.{}); // var tcp_server = try address.listen(.{});
@ -65,20 +64,20 @@ pub fn main() !void {
// std.log.info("t: {}", .{t}); // std.log.info("t: {}", .{t});
while (try server.host.service(0)) |event| switch (event) { // while (try server.host.service(0)) |event| switch (event) {
.connect => |data| { // .connect => |data| {
_ = data; // _ = data;
// std.log.info("{}", .{data.peer}); // // std.log.info("{}", .{data.peer});
}, // },
.disconnect => |data| { // .disconnect => |data| {
_ = data; // _ = data;
// std.log.info("{}", .{data.peer}); // // std.log.info("{}", .{data.peer});
}, // },
.receive => |data| { // .receive => |data| {
std.log.info("{s}", .{data.packet.dataSlice()}); // std.log.info("{s}", .{data.packet.dataSlice()});
defer data.packet.deinit(); // defer data.packet.deinit();
}, // },
}; // };
// const connection = try tcp_server.accept(); // const connection = try tcp_server.accept();
// defer connection.stream.close(); // defer connection.stream.close();