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

View File

@ -46,6 +46,8 @@ pub fn build(b: *std.Build) void {
});
server.root_module.strip = true;
// ztracy
{
const ztracy = b.dependency(
"ztracy",
.{
@ -56,14 +58,20 @@ pub fn build(b: *std.Build) void {
);
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,
@ -82,7 +90,10 @@ pub fn build(b: *std.Build) void {
.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"),
@ -90,7 +101,10 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
client.root_module.addImport("stb_rect_pack", stb_rect_pack.createModule());
}
// zmath
{
const zmath = b.dependency(
"zmath",
.{
@ -101,17 +115,68 @@ pub fn build(b: *std.Build) void {
client.root_module.addImport("zmath", zmath.module("root"));
server.root_module.addImport("zmath", zmath.module("root"));
shared.addImport("zmath", zmath.module("root"));
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"));
// 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.addIncludePath(b.path("vendor/enet/include/"));
server.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) {
server.root_module.linkSystemLibrary("ws2_32", .{});
server.root_module.linkSystemLibrary("winmm", .{});
}
server.root_module.addIncludePath(b.path("vendor/enet/include/"));
// 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",
.{
@ -120,7 +185,10 @@ pub fn build(b: *std.Build) void {
});
client.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
server.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
}
// Raylib
{
const raylib_dep = b.dependency("raylib_zig", .{
.target = target,
.optimize = optimize,
@ -134,6 +202,10 @@ pub fn build(b: *std.Build) void {
client.linkLibrary(raylib_artifact);
client.root_module.addImport("raylib", raylib);
client.root_module.addImport("raygui", raygui);
}
client.root_module.link_libc = true;
server.root_module.link_libc = true;
b.installArtifact(client);
b.installArtifact(server);

View File

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

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 znet = @import("znet");
const bufzilla = @import("bufzilla");
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);
try writer.writeAny(msg);
const encoded = fixed.buffered();
// const encoded = fixed.buffered();
const packet = try znet.Packet.init(encoded, 0, .reliable);
var iterator = server.host.iterPeers();
// const packet = try znet.Packet.init(encoded, 0, .reliable);
// var iterator = server.host.iterPeers();
while (iterator.next()) |peer| {
if (peer.state() == .connected) {
try peer.send(packet);
}
}
// while (iterator.next()) |peer| {
// if (peer.state() == .connected) {
// try peer.send(packet);
// }
// }
}

View File

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