bit streaming

This commit is contained in:
Vicente Ferrari Smith 2026-02-05 12:32:01 +01:00
parent 5864401129
commit 9a5c15e47e
4 changed files with 45 additions and 11 deletions

View File

@ -132,10 +132,39 @@ pub fn main() !void {
}
fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.ENetPeer, the_chunk: *shared.chunk.Chunk) !void {
var aw = try std.io.Writer.Allocating.initCapacity(allocator, 128);
defer aw.deinit();
var aw = try std.ArrayList(u8).initCapacity(allocator, 128);
defer aw.deinit(allocator);
// var writer = bufzilla.Writer.init(&aw.writer);
_ = peer;
// var w = bufzilla.Writer.init(&aw.writer);
var writer = shared.bits.BitWriter.init(&aw);
// Write 3 bits (101)
try writer.write(allocator, @as(u3, 0x5));
// Write 1 bit (true = 1)
try writer.write(allocator, true);
// Write 4 bits (0000)
try writer.write(allocator, @as(u3, 0x1));
try writer.write(allocator, @as(u4, 0xE));
try writer.write(allocator, @as(u32, 0xFFFFFFFA));
var reader = shared.bits.BitReader.init(writer.written());
const _1 : u3 = reader.read(u3);
const _2 : bool = reader.read(bool);
const _3 : u3 = reader.read(u3);
const _4 : u4 = reader.read(u4);
const _5 : u32 = reader.read(u32);
std.log.info("_1: {}", .{_1});
std.log.info("_1: {}", .{_2});
std.log.info("_1: {}", .{_3});
std.log.info("_1: {}", .{_4});
std.log.info("_1: {}", .{_5});
const fields = @typeInfo(shared.chunk.Chunk).@"struct".fields;
@ -149,13 +178,15 @@ fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.EN
// const msg = shared.protocol.makeSpawnMessage(T, entity);
// w.write(entity);
std.log.info("{}", .{entity});
// try writer.writeAny(msg);
}
}
const encoded = aw.written();
// const encoded = aw.written();
// std.log.info("{d} bytes: {s}", .{encoded.len, encoded});
@ -167,13 +198,13 @@ fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.EN
// std.log.info("{s}\n", .{fixed2.buffered()});
const packet = enet.enet_packet_create(encoded.ptr, encoded.len, enet.ENET_PACKET_FLAG_RELIABLE);
// const packet = enet.enet_packet_create(encoded.ptr, encoded.len, enet.ENET_PACKET_FLAG_RELIABLE);
// enet.enet_host_broadcast(host, 0, packet);
_ = host;
if (enet.enet_peer_send(peer, 0, packet) != 0) {
std.log.err("Could not send packet to peer.", .{});
}
// if (enet.enet_peer_send(peer, 0, packet) != 0) {
// std.log.err("Could not send packet to peer.", .{});
// }
}
//fn handle_connection(connection: std.net.Server.Connection) !void {}

View File

@ -3,6 +3,7 @@ const zm = @import("zmath");
// const bufzilla = @import("bufzilla");
const entity = @import("entity.zig");
const bits = @import("bits.zig");
pub const SERVER_PORT: u16 = 1337;

View File

@ -1,4 +1,5 @@
pub const entity = @import("entity.zig");
pub const chunk = @import("chunk.zig");
pub const misc = @import("misc.zig");
pub const entity = @import("entity.zig");
pub const chunk = @import("chunk.zig");
pub const misc = @import("misc.zig");
pub const protocol = @import("protocol.zig");
pub const bits = @import("bits.zig");

View File

@ -1,5 +1,6 @@
.{
.name = .enet,
.fingerprint = 0x201714c0933cd1fc,
.version = "0.1.0",
.minimum_zig_version = "0.15.2",
.paths = .{