removed old main

This commit is contained in:
Vicente Ferrari Smith 2025-12-22 10:04:16 +01:00
parent 7d60843598
commit 430b1b97a3

View File

@ -1,227 +0,0 @@
const std = @import("std");
const zzz = @import("zzz");
const zm = @import("zmath");
const User = struct {
name: []const u8,
age: i64,
valid: bool,
};
fn Storage(comptime T: type) type {
return struct {
items: std.ArrayList(T),
pub fn init(allocator: std.mem.Allocator) !@This() {
return .{
.items = try std.ArrayList(T).initCapacity(allocator, 32),
};
}
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
self.items.deinit(allocator);
}
pub fn update(self: *@This()) void {
for (self.items.items, 0..) |*item, i| {
_ = i;
if (@hasDecl(T, "update"))
item.update();
}
}
};
}
fn spawn(chunk: anytype, comptime T: type, allocator: std.mem.Allocator, value: T) void {
@field(chunk, @typeName(T)).items.append(allocator, value) catch unreachable;
}
const EntityKinds = .{
Player,
Monster,
Projectile,
};
const Player = struct {
pos: zm.Vec,
hp: i32,
pub fn update(self: *Player) void {
std.log.info("pos=({})", .{self.pos});
}
};
const Monster = struct {
pos: zm.Vec,
hp: i32,
pub fn update(self: *Monster) void {
std.log.info("pos=({})", .{self.pos});
}
};
const Projectile = struct {
pos: zm.Vec,
vel: zm.Vec,
pub fn update(self: *Projectile) void {
self.pos = self.pos + self.vel;
std.log.info("pos=({})", .{self.pos});
}
};
fn initChunk(allocator: std.mem.Allocator) !Chunk {
var chunk: Chunk = undefined;
switch (@typeInfo(Chunk)) {
.@"struct" => |s| {
inline for (s.fields) |field| {
const StorageT = field.type;
@field(chunk, field.name) = try StorageT.init(allocator);
}
},
else => unreachable,
}
return chunk;
}
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);
}
},
else => unreachable,
}
}
fn _Chunk(comptime Types: anytype) type {
const FieldCount = Types.len;
var fields: [FieldCount]std.builtin.Type.StructField = undefined;
inline for (Types, 0..) |T, i| {
fields[i] = .{
.name = @typeName(T),
.type = Storage(T),
.default_value_ptr = null,
.is_comptime = false,
.alignment = @alignOf(Storage(T)),
};
}
return @Type(.{
.@"struct" = .{
.layout = .auto,
.fields = &fields,
.decls = &.{},
.is_tuple = false,
},
});
}
const Chunk = _Chunk(EntityKinds);
fn updateChunk(chunk: *Chunk) void {
const info = @typeInfo(Chunk);
switch (info) {
.@"struct" => |s| {
inline for (s.fields) |field| {
@field(chunk, field.name).update();
}
},
else => unreachable,
}
}
var stdout: *std.io.Writer = undefined;
pub fn main() !void {
var dbg_allocator = std.heap.DebugAllocator(.{}).init;
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;
//const j: User = .{ .name = "Jenny", .age = 34, .valid = true };
//try std.json.Stringify.value(j, .{}, stdout);
//try stdout.print("{s}", .{@typeName(Chunk)});
std.log.info("{s}", .{@typeName(Chunk)});
try stdout.flush();
const address = try std.net.Address.parseIp4("127.0.0.1", 3000);
var server = try address.listen(.{});
defer server.deinit();
var chunk = try initChunk(allocator);
defer deinitChunk(&chunk, allocator);
spawn(&chunk, Player, allocator, .{
.pos = zm.f32x4(1, 1, 0, 0),
.hp = 10,
});
spawn(&chunk, Monster, allocator, .{
.pos = zm.f32x4(1, 1, 0, 0),
.hp = 20,
});
spawn(&chunk, Projectile, allocator, .{
.pos = zm.f32x4(0, 0, 0, 0),
.vel = zm.f32x4(0.2, 0, 0, 0),
});
updateChunk(&chunk);
// spawn(&chunk, Player, .{ .pos = .{ .x = 0, .y = 0 }, .hp = 10 });
// spawn(&chunk, Monster, .{ .pos = .{ .x = 5, .y = 5 }, .hp = 20 });
// updateChunk(&chunk);
// while (true) {
// const connection = try server.accept();
// defer connection.stream.close();
// var recv_buffer: [4000]u8 = undefined;
// var send_buffer: [4000]u8 = undefined;
// var conn_reader = connection.stream.reader(&recv_buffer);
// var conn_writer = connection.stream.writer(&send_buffer);
// var http_server = std.http.Server.init(conn_reader.interface(), &conn_writer.interface);
// var req = try http_server.receiveHead();
// std.log.info("connection recieved", .{});
// try req.respond("hello!", .{});
// }
}
//fn handle_connection(connection: std.net.Server.Connection) !void {}
// test "simple test" {
// const gpa = std.testing.allocator;
// var list: std.ArrayList(i32) = .empty;
// defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
// try list.append(gpa, 42);
// try std.testing.expectEqual(@as(i32, 42), list.pop());
// }
// test "fuzz example" {
// const Context = struct {
// fn testOne(context: @This(), input: []const u8) anyerror!void {
// _ = context;
// // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
// try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
// }
// };
// try std.testing.fuzz(Context{}, Context.testOne, .{});
// }