added raylib, enet
This commit is contained in:
1 parent
99061337dd
commit
5dad7572b7
11 files changed
+500
-283
No files matched your search
+110
-37
@@ -1,58 +1,131 @@
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const znet = @import("znet");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const shared = @import("shared");
|
||||
|
||||
const client = @import("client.zig");
|
||||
|
||||
const screen_width = 640;
|
||||
const screen_height = 480;
|
||||
|
||||
var running: bool = true;
|
||||
|
||||
var dbg_allocator: std.heap.DebugAllocator(.{}) = undefined;
|
||||
const allocator = dbg_allocator.allocator();
|
||||
|
||||
var stdout: *std.io.Writer = undefined;
|
||||
|
||||
// var connection: ?std.net.Stream = undefined;
|
||||
|
||||
pub fn main() !void {
|
||||
std.log.info("Helllo Client!", .{});
|
||||
var dbg_allocator = std.heap.DebugAllocator(.{}).init;
|
||||
std.log.info("Hello Client!", .{});
|
||||
|
||||
try init();
|
||||
try znet.init();
|
||||
defer znet.deinit();
|
||||
rl.initWindow(1280, 720, "raylib-zig [core] example - basic window");
|
||||
defer rl.closeWindow();
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
defer _ = dbg_allocator.deinit();
|
||||
const allocator = dbg_allocator.allocator();
|
||||
|
||||
var stdout_buffer: [1024]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
stdout = &stdout_writer.interface;
|
||||
// connect() catch |err| switch (err) {
|
||||
// error.ConnectionRefused => {
|
||||
// std.log.err("server refused connection", .{});
|
||||
// },
|
||||
// else => {
|
||||
// std.log.err("unexpected connect error: {}", .{err});
|
||||
// return err;
|
||||
// },
|
||||
// };
|
||||
|
||||
std.log.info("{s}", .{@typeName(shared.chunk.Chunk)});
|
||||
|
||||
try stdout.flush();
|
||||
|
||||
const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
|
||||
|
||||
var connection = try std.net.tcpConnectToAddress(address);
|
||||
|
||||
std.log.info("Connected to server", .{});
|
||||
// try stdout.flush();
|
||||
|
||||
var the_chunk = try shared.chunk.initChunk(allocator);
|
||||
defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
|
||||
.pos = zm.f32x4(1, 1, 0, 0),
|
||||
.hp = 10,
|
||||
});
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
|
||||
.pos = zm.f32x4(1, 1, 0, 0),
|
||||
.hp = 20,
|
||||
});
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Projectile, allocator, .{
|
||||
.pos = zm.f32x4(0, 0, 0, 0),
|
||||
.vel = zm.f32x4(0.2, 0, 0, 0),
|
||||
});
|
||||
|
||||
shared.chunk.updateChunk(&the_chunk);
|
||||
|
||||
var send_buf: [1024]u8 = undefined;
|
||||
var writer = connection.writer(&send_buf);
|
||||
// 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" });
|
||||
// try shared.protocol.sendHello(&writer.interface, .{ .msg = "Hello from client" });
|
||||
|
||||
var recv_buf: [1024]u8 = undefined;
|
||||
var reader = connection.reader(&recv_buf);
|
||||
const line = try shared.protocol.recvLine(reader.interface());
|
||||
std.log.info("{s}", .{line});
|
||||
// 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});
|
||||
|
||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
||||
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| {
|
||||
std.log.info("{s}", .{data.packet.dataSlice()});
|
||||
defer data.packet.deinit();
|
||||
},
|
||||
};
|
||||
|
||||
if (peer.state() == .connected) {
|
||||
const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
|
||||
try peer.send(packet);
|
||||
}
|
||||
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
|
||||
rl.clearBackground(.sky_blue);
|
||||
|
||||
rl.drawText("Congrats! You created your first window!", 190, 200, 20, .light_gray);
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
|
||||
fn init() !void {
|
||||
dbg_allocator = std.heap.DebugAllocator(.{}).init;
|
||||
var stdout_buffer: [1024]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
stdout = &stdout_writer.interface;
|
||||
}
|
||||
|
||||
fn connect() !void {
|
||||
// const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
|
||||
|
||||
// connection = try std.net.tcpConnectToAddress(address);
|
||||
|
||||
// std.log.info("Connected to server", .{});
|
||||
}
|
||||
|
||||
//fn handle_connection(connection: std.net.Server.Connection) !void {}
|
||||
|
||||
+64
-33
@@ -1,11 +1,16 @@
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const znet = @import("znet");
|
||||
|
||||
const shared = @import("shared");
|
||||
|
||||
const chunk = @import("chunk.zig");
|
||||
const server = @import("server.zig");
|
||||
|
||||
var stdout: *std.io.Writer = undefined;
|
||||
|
||||
pub fn main() !void {
|
||||
std.log.info("Helllo Server!", .{});
|
||||
std.log.info("Hello Server!", .{});
|
||||
var dbg_allocator = std.heap.DebugAllocator(.{}).init;
|
||||
defer _ = dbg_allocator.deinit();
|
||||
const allocator = dbg_allocator.allocator();
|
||||
@@ -14,50 +19,76 @@ pub fn main() !void {
|
||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
stdout = &stdout_writer.interface;
|
||||
|
||||
std.log.info("{s}", .{@typeName(shared.chunk.Chunk)});
|
||||
|
||||
try stdout.flush();
|
||||
|
||||
const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
|
||||
var server = try address.listen(.{});
|
||||
try znet.init();
|
||||
defer znet.deinit();
|
||||
|
||||
defer server.deinit();
|
||||
const 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(.{});
|
||||
|
||||
// defer tcp_server.deinit();
|
||||
|
||||
var the_chunk = try shared.chunk.initChunk(allocator);
|
||||
defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
|
||||
.pos = zm.f32x4(1, 1, 0, 0),
|
||||
.hp = 10,
|
||||
});
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
|
||||
.pos = zm.f32x4(1, 1, 0, 0),
|
||||
.hp = 20,
|
||||
});
|
||||
|
||||
shared.chunk.spawn(&the_chunk, shared.entity.Projectile, allocator, .{
|
||||
.pos = zm.f32x4(0, 0, 0, 0),
|
||||
.vel = zm.f32x4(0.2, 0, 0, 0),
|
||||
});
|
||||
|
||||
shared.chunk.updateChunk(&the_chunk);
|
||||
|
||||
while (true) {
|
||||
const connection = try server.accept();
|
||||
defer connection.stream.close();
|
||||
while (try host.service(500)) |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();
|
||||
},
|
||||
};
|
||||
|
||||
std.log.info("Client connected", .{});
|
||||
// const connection = try tcp_server.accept();
|
||||
// defer connection.stream.close();
|
||||
|
||||
var recv_buffer: [4096]u8 = undefined;
|
||||
var send_buffer: [4096]u8 = undefined;
|
||||
var reader = connection.stream.reader(&recv_buffer);
|
||||
var writer = connection.stream.writer(&send_buffer);
|
||||
// var recv_buffer: [4096]u8 = undefined;
|
||||
// var reader = connection.stream.reader(&recv_buffer);
|
||||
|
||||
const line = try shared.protocol.recvLine(reader.interface());
|
||||
std.log.info("Received: {s}", .{line});
|
||||
// const line = try reader.interface().takeDelimiterExclusive('\n');
|
||||
// std.log.info("Received: {s}", .{line});
|
||||
|
||||
try shared.protocol.sendHello(&writer.interface, .{ .msg = "Hello from server!" });
|
||||
// var send_buffer: [4096]u8 = undefined;
|
||||
// var writer = connection.stream.writer(&send_buffer);
|
||||
// const w = &writer.interface;
|
||||
// try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" });
|
||||
|
||||
// try chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
|
||||
// .pos = zm.f32x4(1, 1, 0, 0),
|
||||
// .hp = 10,
|
||||
// }, w);
|
||||
|
||||
// try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
|
||||
// .pos = zm.f32x4(1, 1, 0, 0),
|
||||
// .hp = 20,
|
||||
// }, w);
|
||||
|
||||
// try chunk.spawn(&the_chunk, shared.entity.Projectile, allocator, .{
|
||||
// .pos = zm.f32x4(0, 0, 0, 0),
|
||||
// .vel = zm.f32x4(0.2, 0, 0, 0),
|
||||
// }, w);
|
||||
|
||||
shared.chunk.updateChunk(&the_chunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-18
@@ -2,7 +2,7 @@ const std = @import("std");
|
||||
const entity = @import("entity.zig");
|
||||
const misc = @import("misc.zig");
|
||||
|
||||
fn Storage(comptime T: type) type {
|
||||
pub fn Storage(comptime T: type) type {
|
||||
return struct {
|
||||
items: std.ArrayList(T),
|
||||
|
||||
@@ -26,10 +26,10 @@ fn Storage(comptime T: type) type {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initChunk(allocator: std.mem.Allocator) !Chunk {
|
||||
var chunk: Chunk = undefined;
|
||||
pub fn initChunk(allocator: std.mem.Allocator) !Chunk() {
|
||||
var chunk: Chunk() = undefined;
|
||||
|
||||
switch (@typeInfo(Chunk)) {
|
||||
switch (@typeInfo(Chunk())) {
|
||||
.@"struct" => |s| {
|
||||
inline for (s.fields) |field| {
|
||||
const StorageT = field.type;
|
||||
@@ -42,8 +42,8 @@ pub fn initChunk(allocator: std.mem.Allocator) !Chunk {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
pub fn deinitChunk(chunk: *Chunk, allocator: std.mem.Allocator) void {
|
||||
switch (@typeInfo(Chunk)) {
|
||||
pub fn deinitChunk(chunk: *Chunk(), allocator: std.mem.Allocator) void {
|
||||
switch (@typeInfo(Chunk())) {
|
||||
.@"struct" => |s| {
|
||||
inline for (s.fields) |field| {
|
||||
@field(chunk, field.name).deinit(allocator);
|
||||
@@ -53,12 +53,12 @@ pub fn deinitChunk(chunk: *Chunk, allocator: std.mem.Allocator) void {
|
||||
}
|
||||
}
|
||||
|
||||
fn _Chunk(comptime Types: anytype) type {
|
||||
const FieldCount = Types.len;
|
||||
pub fn Chunk() type {
|
||||
const FieldCount = entity.EntityKinds.len;
|
||||
|
||||
var fields: [FieldCount]std.builtin.Type.StructField = undefined;
|
||||
|
||||
inline for (Types, 0..) |T, i| {
|
||||
inline for (entity.EntityKinds, 0..) |T, i| {
|
||||
fields[i] = .{
|
||||
.name = misc.short_type_name(T),
|
||||
.type = Storage(T),
|
||||
@@ -78,10 +78,8 @@ fn _Chunk(comptime Types: anytype) type {
|
||||
});
|
||||
}
|
||||
|
||||
pub const Chunk = _Chunk(entity.EntityKinds);
|
||||
|
||||
pub fn updateChunk(chunk: *Chunk) void {
|
||||
const info = @typeInfo(Chunk);
|
||||
pub fn updateChunk(chunk: *Chunk()) void {
|
||||
const info = @typeInfo(Chunk());
|
||||
|
||||
switch (info) {
|
||||
.@"struct" => |s| {
|
||||
@@ -92,8 +90,3 @@ pub fn updateChunk(chunk: *Chunk) void {
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn(chunk: anytype, comptime T: type, allocator: std.mem.Allocator, value: T) void {
|
||||
std.log.info("hello!? {s}", .{@typeName(T)});
|
||||
@field(chunk, misc.short_type_name(T)).items.append(allocator, value) catch unreachable;
|
||||
}
|
||||
+45
-4
@@ -1,5 +1,9 @@
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const protocol = @import("protocol.zig");
|
||||
|
||||
pub const entity_id = u64;
|
||||
pub const INVALID_ENTITY_ID: entity_id = 0;
|
||||
|
||||
pub const EntityKinds = .{
|
||||
Player,
|
||||
@@ -8,24 +12,47 @@ pub const EntityKinds = .{
|
||||
};
|
||||
|
||||
pub const Player = struct {
|
||||
id: entity_id = INVALID_ENTITY_ID,
|
||||
pos: zm.Vec,
|
||||
hp: i32,
|
||||
|
||||
pub fn update(self: *Player) void {
|
||||
std.log.info("pos=({})", .{self.pos});
|
||||
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try protocol.writeVec4(w, self.pos);
|
||||
try protocol.writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Projectile {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try protocol.readVec4(r),
|
||||
.vel = try protocol.readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Monster = struct {
|
||||
id: entity_id = INVALID_ENTITY_ID,
|
||||
pos: zm.Vec,
|
||||
hp: i32,
|
||||
|
||||
pub fn update(self: *Monster) void {
|
||||
std.log.info("pos=({})", .{self.pos});
|
||||
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try protocol.writeVec4(w, self.pos);
|
||||
try protocol.writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Projectile {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try protocol.readVec4(r),
|
||||
.vel = try protocol.readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Projectile = struct {
|
||||
id: entity_id = INVALID_ENTITY_ID,
|
||||
pos: zm.Vec,
|
||||
vel: zm.Vec,
|
||||
|
||||
@@ -33,4 +60,18 @@ pub const Projectile = struct {
|
||||
self.pos = self.pos + self.vel;
|
||||
std.log.info("pos=({})", .{self.pos});
|
||||
}
|
||||
|
||||
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try protocol.writeVec4(w, self.pos);
|
||||
try protocol.writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Projectile {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try protocol.readVec4(r),
|
||||
.vel = try protocol.readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
+23
-3
@@ -1,7 +1,27 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn short_type_name(comptime T: type) [:0]const u8 {
|
||||
const full = @typeName(T);
|
||||
const base = full[std.mem.lastIndexOf(u8, full, ".").? + 1 ..];
|
||||
return base ++ "\x00";
|
||||
const fullTypeName = @typeName(T);
|
||||
const last_index = std.mem.lastIndexOf(u8, fullTypeName, ".");
|
||||
if (last_index) |idx| {
|
||||
return fullTypeName[idx + 1 .. :0];
|
||||
}
|
||||
return fullTypeName;
|
||||
}
|
||||
|
||||
pub fn dumpStructType(comptime T: type) void {
|
||||
const info = @typeInfo(T);
|
||||
|
||||
switch (info) {
|
||||
.@"struct" => |s| {
|
||||
std.log.info("struct {{", .{});
|
||||
inline for (s.fields) |field| {
|
||||
std.log.info(" {s}: {s}", .{ field.name, @typeName(field.type) });
|
||||
}
|
||||
std.log.info("}}", .{});
|
||||
},
|
||||
else => {
|
||||
std.log.info("{s} is not a struct", .{@typeName(T)});
|
||||
},
|
||||
}
|
||||
}
|
||||
+57
-2
@@ -1,15 +1,70 @@
|
||||
const std = @import("std");
|
||||
const entity = @import("entity.zig");
|
||||
const zm = @import("zmath");
|
||||
|
||||
pub const SERVER_PORT: u16 = 1337;
|
||||
|
||||
pub const MessageType = enum(u8) {
|
||||
spawn_entity = 1,
|
||||
// later: despawn_entity, update_entity, snapshot, etc.
|
||||
};
|
||||
|
||||
pub const SpawnEntity = struct {
|
||||
id: entity.entity_id,
|
||||
kind: EntityKind,
|
||||
payload: []const u8, // serialized entity data
|
||||
};
|
||||
|
||||
pub fn write_message(writer: *std.Io.Writer, msg_type: MessageType, payload: []const u8) !void {
|
||||
try writer.writeByte(@intFromEnum(msg_type));
|
||||
try writer.writeInt(u32, @intCast(payload.len), .little);
|
||||
try writer.writeAll(payload);
|
||||
}
|
||||
|
||||
pub fn read_message(reader: *std.Io.Reader, allocator: std.mem.Allocator) !struct {
|
||||
msg_type: MessageType,
|
||||
payload: []u8,
|
||||
} {
|
||||
const msg_type = try reader.readByte();
|
||||
const size = try reader.readInt(u32, .little);
|
||||
|
||||
const payload = try allocator.alloc(u8, size);
|
||||
errdefer allocator.free(payload);
|
||||
|
||||
try reader.readNoEof(payload);
|
||||
|
||||
return .{
|
||||
.msg_type = @enumFromInt(msg_type),
|
||||
.payload = payload,
|
||||
};
|
||||
}
|
||||
|
||||
pub const EntityKind = enum(u8) {
|
||||
Player,
|
||||
Monster,
|
||||
Projectile,
|
||||
};
|
||||
|
||||
pub const Hello = struct {
|
||||
msg: []const u8,
|
||||
};
|
||||
|
||||
pub fn sendHello(writer: *std.io.Writer, hello: Hello) !void {
|
||||
try writer.print("{s}\n", .{hello.msg});
|
||||
try writer.flush();
|
||||
}
|
||||
|
||||
pub fn recvLine(reader: *std.io.Reader) ![]u8 {
|
||||
return try reader.takeDelimiterExclusive('\n');
|
||||
fn writeVec4(w: *std.Io.Writer, v: zm.Vec4) !void {
|
||||
const a = zm.vecToArray(v);
|
||||
inline for (a) |f| {
|
||||
try w.writeFloat(f32, f, .little);
|
||||
}
|
||||
}
|
||||
|
||||
fn readVec4(r: *std.Io.Reader) !zm.Vec4 {
|
||||
var a: [4]f32 = undefined;
|
||||
inline for (&a) |*f| {
|
||||
f.* = try r.readFloat(f32, .little);
|
||||
}
|
||||
return zm.loadArr4(a);
|
||||
}
|
||||
Reference in new issue
Block a user