535 lines
18 KiB
Zig
535 lines
18 KiB
Zig
const std = @import("std");
|
|
const zm = @import("zmath");
|
|
const bufzilla = @import("bufzilla");
|
|
const ft = @import("freetype");
|
|
const tracy = @import("tracy");
|
|
const kb = @import("kb");
|
|
|
|
const shared = @import("shared");
|
|
|
|
const c = @import("c.zig").c;
|
|
const enet = @import("c.zig").enet;
|
|
const rl = @import("c.zig").rl;
|
|
const client = @import("client.zig");
|
|
const entity = @import("entity.zig");
|
|
const misc = @import("misc.zig");
|
|
const font = @import("font.zig");
|
|
|
|
const dt : f32 = 1.0 / 200.0;
|
|
var t : f32 = 0;
|
|
var gt : f32 = 0;
|
|
var accumulator : f32 = 0;
|
|
var k : f32 = 1;
|
|
var frame : i32 = 0;
|
|
|
|
const screen_width = 1280;
|
|
const screen_height = 720;
|
|
|
|
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();
|
|
|
|
const allocator = dbg_allocator.allocator();
|
|
defer {
|
|
const deinit_status = dbg_allocator.deinit();
|
|
if (deinit_status == .leak) {
|
|
@panic("LEAKED!");
|
|
} else {
|
|
// @panic("SUCCESS!");
|
|
}
|
|
}
|
|
|
|
var stdout = std.fs.File.stdout();
|
|
|
|
_ = try stdout.write("Hello, Client!\n");
|
|
|
|
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.SetConfigFlags(rl.FLAG_WINDOW_HIGHDPI);
|
|
rl.InitWindow(screen_width, screen_height, "zzz");
|
|
defer rl.CloseWindow();
|
|
|
|
font.ft_lib = try ft.Library.init();
|
|
|
|
const sizes = [_]i32{
|
|
60,
|
|
};
|
|
|
|
var vollkorn: [sizes.len]font.Font = undefined;
|
|
// var inconsolata: [sizes.len]font.Font = undefined;
|
|
// var arabic: [sizes.len]font.Font = undefined;
|
|
// // var japanese: [sizes.len]font.Font = undefined;
|
|
|
|
for (sizes, 0..) |pt, i| {
|
|
vollkorn[i] = try font.Font.init(
|
|
"assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf",
|
|
pt,
|
|
allocator
|
|
);
|
|
// inconsolata[i] = try font.Font.init(
|
|
// "assets/fonts/Inconsolata/static/Inconsolata-Regular.ttf",
|
|
// @intCast(pt),
|
|
// allocator
|
|
// );
|
|
// arabic[i] = try font.Font.init(
|
|
// "assets/fonts/Amiri/Amiri-Regular.ttf",
|
|
// @intCast(pt),
|
|
// allocator
|
|
// );
|
|
// japanese[i] = try font.Font.init(
|
|
// "assets/fonts/Noto_Sans_JP/static/NotoSansJP-Regular.ttf",
|
|
// @intCast(pt),
|
|
// allocator
|
|
// );
|
|
}
|
|
|
|
defer {
|
|
for (&vollkorn) |*f| f.deinit(allocator);
|
|
// for (&inconsolata) |*f| f.deinit(allocator);
|
|
// for (&arabic) |*f| f.deinit(allocator);
|
|
// for (&japanese) |*f| f.deinit(allocator);
|
|
}
|
|
|
|
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");
|
|
|
|
// 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 img = rl.genImageColor(32, 32, .blank);
|
|
// 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 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();
|
|
|
|
// var the_chunk = try shared.chunk.initChunk(allocator);
|
|
// defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
|
|
|
// shared.chunk.updateChunk(&the_chunk);
|
|
|
|
//var elf = entity.Elf.init();
|
|
|
|
// 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 },
|
|
// .projection = .perspective,
|
|
// .target = .{ .x = 0, .y = 0, .z = 0 },
|
|
// .up = .{ .x = 0, .y = 1, .z = 0 }
|
|
// };
|
|
|
|
// rl.initAudioDevice();
|
|
|
|
// if (rl.isAudioDeviceReady()) {
|
|
// std.log.info("audio device is ready!", .{});
|
|
// } else {
|
|
// std.log.info("audio device is NOT ready!", .{});
|
|
// }
|
|
|
|
// const music = try rl.loadMusicStream("assets/romantic-piano-431010.mp3");
|
|
// std.log.info("{}", .{rl.isMusicValid(music)});
|
|
// rl.playMusicStream(music);
|
|
// std.log.info("is music playing? {}", .{rl.isMusicStreamPlaying(music)});
|
|
|
|
// var old_time : f32 = @floatCast(rl.getTime());
|
|
var old_time : f32 = @floatCast(rl.GetTime());
|
|
|
|
while (!rl.WindowShouldClose()) {
|
|
const tracy_main_zone = tracy.ZoneNC(@src(), "loop", 0x00_ff_00_00);
|
|
defer tracy_main_zone.End();
|
|
|
|
// const new_time : f32 = @floatCast(rl.getTime());
|
|
const new_time : f32 = @floatCast(rl.GetTime());
|
|
var frame_time = new_time - old_time;
|
|
old_time = new_time;
|
|
|
|
if (frame_time > 0.25)
|
|
frame_time = 0.25;
|
|
|
|
t += frame_time;
|
|
accumulator += frame_time * k;
|
|
|
|
// 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();
|
|
|
|
// const slice = data.packet.dataSlice();
|
|
|
|
// var reader = bufzilla.Reader(.{}).init(slice);
|
|
|
|
// const id = try reader.readPath("id");
|
|
|
|
// std.log.info("{any}", .{id});
|
|
// },
|
|
// };
|
|
|
|
rl.PollInputEvents();
|
|
|
|
// if (peer.state() == .connected) {
|
|
// const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
|
|
// try peer.send(packet);
|
|
// }
|
|
// // _ = peer;
|
|
|
|
// rl.updateMusicStream(music);
|
|
|
|
while (accumulator > dt * k) {
|
|
// update(dt * cast(float) k);
|
|
accumulator -= dt * k;
|
|
gt += dt * k;
|
|
}
|
|
|
|
const drawing_zone = tracy.ZoneNC(@src(), "Drawing", 0x00_ff_00_00);
|
|
|
|
rl.BeginDrawing();
|
|
rl.ClearBackground(rl.SKYBLUE);
|
|
|
|
// const lorem = @embedFile("embeds/lorem.txt");
|
|
|
|
vollkorn[0].render_text(
|
|
"The night is long and cold outside.",
|
|
rl.Vector2{ .x = 0, .y = 0 },
|
|
true,
|
|
rl.WHITE,
|
|
rl.BLANK,
|
|
false,
|
|
true
|
|
);
|
|
|
|
// arabic[0].render_text(
|
|
// "الليل طويل وبارد في الخارج",
|
|
// rl.Vector2{ .x = 0, .y = 100},
|
|
// true,
|
|
// .white,
|
|
// .blank,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// var y : f32 = 0;
|
|
// var it = std.mem.splitScalar(u8, lorem, '\n');
|
|
// while (it.next()) |line| {
|
|
// vollkorn[0].render_text(
|
|
// line,
|
|
// rl.Vector2{ .x = 0, .y = y},
|
|
// true,
|
|
// .white,
|
|
// .blank,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// const font_linegap : f32 = @floatFromInt(vollkorn[0].face.size().metrics().height >> 6);
|
|
// const dpi_font_linegap = font_linegap / rl.getWindowScaleDPI().y;
|
|
|
|
// y += dpi_font_linegap;
|
|
// }
|
|
|
|
|
|
// for (&vollkorn) |*f| {
|
|
// f.render_text(
|
|
// "Whereas, disregard and contempt for human rights have resulted!",
|
|
// rl.Vector2{ .x = 0, .y = y},
|
|
// true,
|
|
// .white,
|
|
// .blank,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// // const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
|
|
// // const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
|
|
|
|
// // const font_descent : f32 = @floatFromInt(f.face.size().metrics().descender >> 6);
|
|
// // const dpi_font_descent = font_descent / rl.getWindowScaleDPI().y;
|
|
|
|
// const font_linegap : f32 = @floatFromInt(f.face.size().metrics().height >> 6);
|
|
// const dpi_font_linegap = font_linegap / rl.getWindowScaleDPI().y;
|
|
|
|
// y += dpi_font_linegap;
|
|
// }
|
|
|
|
// for (&inconsolata) |*f| {
|
|
// f.render_text(
|
|
// "Whereas, disregard and contempt for human rights have resulted!",
|
|
// rl.Vector2{ .x = 0, .y = y},
|
|
// true,
|
|
// .white,
|
|
// .blank,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// // const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
|
|
// // const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
|
|
|
|
// // const font_descent : f32 = @floatFromInt(f.face.size().metrics().descender >> 6);
|
|
// // const dpi_font_descent = font_descent / rl.getWindowScaleDPI().y;
|
|
|
|
// const font_linegap : f32 = @floatFromInt(f.face.size().metrics().height >> 6);
|
|
// const dpi_font_linegap = font_linegap / rl.getWindowScaleDPI().y;
|
|
|
|
// y += dpi_font_linegap;
|
|
// }
|
|
|
|
// for (&japanese) |*f| {
|
|
// f.render_text(
|
|
// "外は夜が長くて寒い",
|
|
// c.Vector2{ .x = 0, .y = y},
|
|
// true,
|
|
// c.WHITE,
|
|
// c.BLANK,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
|
|
// const dpi_font_ascent = font_ascent / c.GetWindowScaleDPI().y;
|
|
|
|
// y += dpi_font_ascent;
|
|
// }
|
|
|
|
// for (&arabic) |*f| {
|
|
|
|
|
|
// const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
|
|
// const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
|
|
|
|
// y += dpi_font_ascent;
|
|
// }
|
|
// f.render_text(
|
|
// "Whereas, disregard and contempt for human rights have resulted!",
|
|
// rl.Vector2.init(0, 0),
|
|
// true,
|
|
// rl.Color.white,
|
|
// rl.Color.blank,
|
|
// false,
|
|
// true
|
|
// );
|
|
|
|
// rl.beginShaderMode(test_shader1);
|
|
// rl.gl.rlBegin(rl.gl.rl_quads);
|
|
|
|
// {
|
|
// const topLeft : rl.Vector2 = .{
|
|
// .x = 0.0,
|
|
// .y = 0.0
|
|
// };
|
|
// const bottomRight : rl.Vector2 = .{
|
|
// .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0,
|
|
// .y = @as(f32, @floatFromInt(rl.getScreenHeight()))
|
|
// };
|
|
|
|
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
|
|
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
|
|
|
|
// }
|
|
|
|
// rl.gl.rlEnd();
|
|
// rl.endShaderMode();
|
|
|
|
// rl.beginShaderMode(test_shader2);
|
|
// rl.gl.rlBegin(rl.gl.rl_quads);
|
|
|
|
// {
|
|
// const topLeft : rl.Vector2 = .{
|
|
// .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0,
|
|
// .y = 0
|
|
// };
|
|
// const bottomRight : rl.Vector2 = .{
|
|
// .x = @floatFromInt(rl.getScreenWidth()),
|
|
// .y = @floatFromInt(rl.getScreenHeight())
|
|
// };
|
|
|
|
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
|
|
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
|
|
// }
|
|
|
|
// rl.gl.rlEnd();
|
|
// rl.endShaderMode();
|
|
|
|
// rl.beginShaderMode(test_shader2);
|
|
// rl.gl.rlBegin(rl.gl.rl_quads);
|
|
|
|
// {
|
|
// const topLeft : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = 0 };
|
|
// const bottomRight : rl.Vector2 = .{
|
|
// .x = @floatFromInt(rl.getScreenWidth()),
|
|
// .y = @floatFromInt(rl.getScreenHeight())
|
|
// };
|
|
|
|
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
|
|
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
|
|
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
|
|
// }
|
|
|
|
// rl.gl.rlEnd();
|
|
// rl.endShaderMode();
|
|
|
|
// rl.drawRectangle(400, 0, 400, 450, rl.Color{ .r = 54, .g = 54, .b = 54, .a = 255 });
|
|
|
|
|
|
// f.texture.drawPro(
|
|
// .{.x = 0, .y = 0, .width = 4096, .height = 4096},
|
|
// .{.x = 0, .y = 0, .width = 512, .height = 512 },
|
|
// .zero(), 0, .white);
|
|
|
|
// const connected_text = "Connected";
|
|
//const not_connected_text = "Not Connected";
|
|
|
|
// switch (peer.state()) {
|
|
// .connected => rl.drawText(
|
|
// connected_text,
|
|
// @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2),
|
|
// 50,
|
|
// 20,
|
|
// .white
|
|
// ),
|
|
// else => rl.drawText(
|
|
// not_connected_text,
|
|
// @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(not_connected_text, f.baseSize), 1),
|
|
// 50,
|
|
// 20,
|
|
// .white
|
|
// ),
|
|
// }
|
|
//@divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50
|
|
|
|
|
|
|
|
|
|
// const font_size : i32 = 180;
|
|
// const text_size = rl.Vector2{.x = @floatFromInt(rl.measureText(connected_text, font_size)),
|
|
// .y = font_size};//rl.measureTextEx(try rl.getFontDefault(), connected_text, font_size, font_size / 10);
|
|
// const pos = rl.Vector2{.x = 0, .y = 0};
|
|
// rl.drawText(connected_text, pos.x, pos.y, font_size, .white);
|
|
// rl.drawRectangleLines(pos.x, pos.y, @intFromFloat(text_size.x), @intFromFloat(text_size.y), .red);
|
|
// rl.drawRectangle(pos.x, pos.y, rl.getScreenWidth(), rl.getScreenHeight(), .white);
|
|
// rl.drawTexturePro(tx,
|
|
// rl.Rectangle{.x = 0, .y = 0, .width = 32, .height = 32},
|
|
// rl.Rectangle{.x = 100, .y = 100, .width = 500, .height = 500},
|
|
// .{.x = 0, .y = 0},
|
|
// 0,
|
|
// .white);
|
|
|
|
//rl.drawLineV(.{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = 0}, .{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)),
|
|
// .y = @floatFromInt(rl.getScreenHeight())}, .red);
|
|
|
|
//rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);
|
|
//rl.drawRectangleLines(0, 0, 100, 100, .red);
|
|
misc.drawFPS(0, 0, frame_time);
|
|
|
|
//elf.draw();
|
|
|
|
// rl.beginMode3D(camera);
|
|
// rl.drawSphere(.{ .x = 0, .y = 0, .z = 0 }, 1, .red);
|
|
// rl.endMode3D();
|
|
|
|
rl.EndDrawing();
|
|
drawing_zone.End();
|
|
|
|
const swap_zone = tracy.ZoneNC(@src(), "swapScreenBuffer", 0x00_ff_00_00);
|
|
rl.SwapScreenBuffer();
|
|
swap_zone.End();
|
|
|
|
frame += 1;
|
|
|
|
tracy.FrameMark();
|
|
}
|
|
}
|
|
|
|
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 {}
|
|
|
|
// 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, .{});
|
|
// }
|