diff --git a/src/client/main.zig b/src/client/main.zig index 8b8a0af..7ab875a 100644 --- a/src/client/main.zig +++ b/src/client/main.zig @@ -29,8 +29,6 @@ var running: bool = true; var dbg_allocator = std.heap.DebugAllocator(.{}){}; -// var connection: ?std.net.Stream = undefined; - pub fn main() !void { const tracy_zone = tracy.ZoneNC(@src(), "main", 0x00_ff_00_00); defer tracy_zone.End(); @@ -79,60 +77,21 @@ pub fn main() !void { } 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"); + // const test_shader1 = try rl.LoadShader(null, "assets/test_1.frag"); + // const test_shader2 = try rl.LoadShader(null, "assets/test_2.frag"); - // font.shader = try rl.loadShader(null, "assets/text.frag"); - // const test_shader1 = try rl.loadShader(null, "assets/test_1.frag"); - // const test_shader2 = try rl.loadShader(null, "assets/test_2.frag"); + const host = enet.enet_host_create(null, 32, 2, 0, 0); + defer enet.enet_host_destroy(host); - // const img = rl.genImageColor(32, 32, .blank); - // const tx = try rl.loadTextureFromImage(img); - // rl.unloadImage(img); + var address = enet.ENetAddress{ .port = shared.protocol.SERVER_PORT }; + _ = enet.enet_address_set_host(&address, "localhost"); - // 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(.{ - // .ip = .{ .ipv4 = "127.0.0.1" }, - // .port = .{ .uint = 5000 }, - // }), - // .channel_limit = .max, - // .data = 0, - // }); - - // connect() catch |err| switch (err) { - // error.ConnectionRefused => { - // std.log.err("server refused connection", .{}); - // }, - // else => { - // std.log.err("unexpected connect error: {}", .{err}); - // return err; - // }, - // }; - - // try stdout.flush(); + const peer = enet.enet_host_connect(host, &address, 2, 0); + defer enet.enet_peer_reset(peer); var the_chunk = try shared.chunk.initChunk(allocator); defer shared.chunk.deinitChunk(&the_chunk, allocator); - // var send_buf: [1024]u8 = undefined; - // var writer = if (connection) |*conn| conn.writer(&send_buf) else return; - - // try shared.protocol.sendHello(&writer.interface, .{ .msg = "Hello from client" }); - - // var recv_buf: [1024]u8 = undefined; - // var reader = if (connection) |*conn| conn.reader(&recv_buf) else return; - // const line = try reader.interface().takeDelimiterExclusive('\n'); - // std.log.info("{s}", .{line}); - // const camera = rl.Camera{ // .fovy = 45, // .position = .{ .x = 0, .y = 0, .z = 10 }, @@ -157,7 +116,7 @@ pub fn main() !void { var old_time : f32 = @floatCast(rl.GetTime()); while (!rl.WindowShouldClose()) { - const tracy_main_zone = tracy.ZoneNC(@src(), "loop", 0x00_ff_00_00); + const tracy_main_zone = tracy.ZoneNC(@src(), "loop", 0x00_aa_33_10); defer tracy_main_zone.End(); const new_time : f32 = @floatCast(rl.GetTime()); @@ -171,27 +130,21 @@ pub fn main() !void { accumulator += frame_time * k; - // 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(); - - // var reader = bufzilla.Reader(.{}).init(slice); - - // const id = try reader.readPath("id"); - - // std.log.info("{any}", .{id}); - // }, - // }; + var event = enet.ENetEvent{}; + while (enet.enet_host_service(host, &event, 0) > 0) { + switch (event.type) { + enet.ENET_EVENT_TYPE_CONNECT => { + std.log.info("connect", .{}); + }, + enet.ENET_EVENT_TYPE_RECEIVE => { + std.log.info("receive", .{}); + }, + enet.ENET_EVENT_TYPE_DISCONNECT => { + std.log.info("disconnect", .{}); + }, + else => {} + } + } rl.PollInputEvents(); diff --git a/src/server/main.zig b/src/server/main.zig index 5ca8e76..4c83b2d 100644 --- a/src/server/main.zig +++ b/src/server/main.zig @@ -32,14 +32,14 @@ pub fn main() !void { if (enet.enet_initialize() != 0) { std.log.info("Failed to load ENet", .{}); return; - } - + } defer enet.enet_deinitialize(); const address = enet.ENetAddress{ .host = enet.ENET_HOST_ANY, .port = shared.protocol.SERVER_PORT }; - var host : ?*enet.ENetHost = null; - - host = enet.enet_host_create(&address, 32, 2, 0, 0); + const host = enet.enet_host_create(&address, 32, 2, 0, 0); + if (host == null) { + @panic("host is null"); + } defer enet.enet_host_destroy(host); var the_chunk = try shared.chunk.initChunk(allocator); @@ -62,33 +62,18 @@ pub fn main() !void { while (enet.enet_host_service(host, &event, 0) > 0) { switch (event.type) { enet.ENET_EVENT_TYPE_CONNECT => { - std.log.info("hello", .{}); + std.log.info("connect", .{}); }, enet.ENET_EVENT_TYPE_RECEIVE => { - std.log.info("hello", .{}); + std.log.info("receive", .{}); }, enet.ENET_EVENT_TYPE_DISCONNECT => { - std.log.info("hello", .{}); + std.log.info("disconnect", .{}); }, else => {} } } - // 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(); diff --git a/vendor/enet/build.zig b/vendor/enet/build.zig index ff0d440..8ba0a2a 100644 --- a/vendor/enet/build.zig +++ b/vendor/enet/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -15,6 +15,24 @@ pub fn build(b: *std.Build) void { }), }); + var flags = try std.ArrayList([]const u8).initCapacity(b.allocator, 0); + + switch (target.result.os.tag) { + .linux, .macos => { + try flags.append(b.allocator, "-DHAS_FCNTL=1"); + try flags.append(b.allocator, "-DHAS_POLL=1"); + try flags.append(b.allocator, "-DHAS_GETADDRINFO=1"); + try flags.append(b.allocator, "-DHAS_GETNAMEINFO=1"); + try flags.append(b.allocator, "-DHAS_INET_PTON=1"); + try flags.append(b.allocator, "-DHAS_INET_NTOP=1"); + try flags.append(b.allocator, "-DHAS_MSGHDR_FLAGS=1"); + }, + else => {}, + } + + try flags.append(b.allocator, "-DHAS_OFFSETOF=1"); + try flags.append(b.allocator, "-DHAS_SOCKLEN_T=1"); + lib.root_module.addCSourceFiles(.{ .root = enet_src.path(""), .files = &[_][]const u8{ @@ -28,6 +46,17 @@ pub fn build(b: *std.Build) void { "unix.c", "win32.c", }, + .flags = &[_][]const u8{ + "-DHAS_OFFSETOF=1", + "-DHAS_SOCKLEN_T=1", + "-DHAS_FCNTL=1", + "-DHAS_POLL=1", + "-DHAS_GETADDRINFO=1", + "-DHAS_GETNAMEINFO=1", + "-DHAS_INET_PTON=1", + "-DHAS_INET_NTOP=1", + "-DHAS_MSGHDR_FLAGS=1", + } });