Compare commits
2 Commits
4b61aa5477
...
71893c1821
| Author | SHA1 | Date | |
|---|---|---|---|
| 71893c1821 | |||
| 494c3a3418 |
@ -104,6 +104,7 @@ pub fn build(b: *std.Build) void {
|
||||
const raylib = raylib_dep.module("raylib"); // main raylib module
|
||||
const raygui = raylib_dep.module("raygui"); // raygui module
|
||||
const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
|
||||
raylib_artifact.root_module.addCMacro("SUPPORT_CUSTOM_FRAME_CONTROL", "");
|
||||
|
||||
client.linkLibrary(raylib_artifact);
|
||||
client.root_module.addImport("raylib", raylib);
|
||||
|
||||
@ -1,13 +1,21 @@
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const znet = @import("znet");
|
||||
const rl = @import("raylib");
|
||||
const std = @import("std");
|
||||
const zm = @import("zmath");
|
||||
const znet = @import("znet");
|
||||
const rl = @import("raylib");
|
||||
const bufzilla = @import("bufzilla");
|
||||
|
||||
const shared = @import("shared");
|
||||
|
||||
const client = @import("client.zig");
|
||||
const entity = @import("entity.zig");
|
||||
const misc = @import("misc.zig");
|
||||
|
||||
const dt : f32 = 1.0 / 200.0;
|
||||
var t : f32 = 0;
|
||||
var gt : f32 = 0;
|
||||
var accumulator : f32 = 0;
|
||||
var k : f32 = 1.0;
|
||||
var frame : i32 = 0;
|
||||
|
||||
const screen_width = 640;
|
||||
const screen_height = 480;
|
||||
@ -94,7 +102,21 @@ pub fn main() !void {
|
||||
// rl.playMusicStream(music);
|
||||
std.log.info("is music playing? {}", .{rl.isMusicStreamPlaying(music)});
|
||||
|
||||
var old_time : f32 = @floatCast(rl.getTime());
|
||||
|
||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
||||
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;
|
||||
@ -109,8 +131,6 @@ pub fn main() !void {
|
||||
|
||||
const slice = data.packet.dataSlice();
|
||||
|
||||
std.log.info("{s}", .{slice});
|
||||
|
||||
var reader = bufzilla.Reader(.{}).init(slice);
|
||||
|
||||
const id = try reader.readPath("id");
|
||||
@ -119,6 +139,8 @@ pub fn main() !void {
|
||||
},
|
||||
};
|
||||
|
||||
rl.pollInputEvents();
|
||||
|
||||
if (peer.state() == .connected) {
|
||||
const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
|
||||
try peer.send(packet);
|
||||
@ -132,15 +154,36 @@ pub fn main() !void {
|
||||
|
||||
rl.updateMusicStream(music);
|
||||
|
||||
while (accumulator > dt * k) {
|
||||
// update(dt * cast(float) k);
|
||||
accumulator -= dt * k;
|
||||
gt += dt * k;
|
||||
}
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.clearBackground(.sky_blue);
|
||||
|
||||
rl.beginDrawing();
|
||||
|
||||
const connected_text = "Connected";
|
||||
const not_connected_text = "Not Connected";
|
||||
|
||||
const f = try rl.getFontDefault();
|
||||
|
||||
std.log.info("baseSize: {}, text length: {}", .{f.baseSize, rl.measureText(connected_text, f.baseSize)});
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
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);
|
||||
rl.drawFPS(0, 0);
|
||||
misc.drawFPS(0, 0, frame_time, frame);
|
||||
|
||||
elf.draw();
|
||||
|
||||
// rl.beginMode3D(camera);
|
||||
@ -150,6 +193,8 @@ pub fn main() !void {
|
||||
rl.endDrawing();
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
rl.swapScreenBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
49
src/client/misc.zig
Normal file
49
src/client/misc.zig
Normal file
@ -0,0 +1,49 @@
|
||||
const rl = @import("raylib");
|
||||
|
||||
const FPS_CAPTURE_FRAMES_COUNT : i32 = 30; // 30 captures
|
||||
const FPS_AVERAGE_TIME_SECONDS : f32 = 0.5; // 500 milliseconds
|
||||
const FPS_STEP : f32 = (FPS_AVERAGE_TIME_SECONDS / FPS_CAPTURE_FRAMES_COUNT);
|
||||
|
||||
var index : u32 = 0;
|
||||
var history : [FPS_CAPTURE_FRAMES_COUNT]f32 = .{0} ** FPS_CAPTURE_FRAMES_COUNT;
|
||||
var average : f32 = 0;
|
||||
var last : f32 = 0;
|
||||
|
||||
pub fn drawFPS(posX : i32, posY : i32, fpsFrame : f32, frame : i32) void {
|
||||
var color : rl.Color = .lime; // Good FPS
|
||||
const fps : i32 = getFPS(fpsFrame, frame);
|
||||
|
||||
if ((fps < 30) and (fps >= 15)) {color = .orange;} // Warning FPS
|
||||
else if (fps < 15) color = .red; // Low FPS
|
||||
|
||||
rl.drawText(rl.textFormat("%2i FPS", .{fps}), posX, posY, 20, color);
|
||||
}
|
||||
|
||||
pub fn getFPS(fpsFrame : f32, frame : i32) i32 {
|
||||
var fps : i32 = 0;
|
||||
|
||||
// if we reset the window, reset the FPS info
|
||||
if (frame == 0)
|
||||
{
|
||||
average = 0;
|
||||
last = 0;
|
||||
index = 0;
|
||||
|
||||
for (0..FPS_CAPTURE_FRAMES_COUNT) |i| history[i] = 0;
|
||||
}
|
||||
|
||||
if (fpsFrame == 0) return 0;
|
||||
|
||||
if ((rl.getTime() - last) > FPS_STEP)
|
||||
{
|
||||
last = @floatCast(rl.getTime());
|
||||
index = (index + 1) % FPS_CAPTURE_FRAMES_COUNT;
|
||||
average -= history[index];
|
||||
history[index] = fpsFrame / FPS_CAPTURE_FRAMES_COUNT;
|
||||
average += history[index];
|
||||
}
|
||||
|
||||
fps = @intFromFloat(@round(1.0 / average));
|
||||
|
||||
return fps;
|
||||
}
|
||||
@ -7,6 +7,13 @@ const shared = @import("shared");
|
||||
const chunk = @import("chunk.zig");
|
||||
const server = @import("server.zig");
|
||||
|
||||
const dt : f32 = 1.0 / 200.0;
|
||||
var t : f32 = 0;
|
||||
var gt : f32 = 0;
|
||||
var accumulator : f32 = 0;
|
||||
var k : f32 = 1.0;
|
||||
var frame : i32 = 0;
|
||||
|
||||
var stdout: *std.io.Writer = undefined;
|
||||
|
||||
pub fn main() !void {
|
||||
@ -43,7 +50,21 @@ pub fn main() !void {
|
||||
var the_chunk = try shared.chunk.initChunk(allocator);
|
||||
defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
||||
|
||||
var old_time = std.time.nanoTimestamp();
|
||||
|
||||
while (true) {
|
||||
const new_time = std.time.nanoTimestamp();
|
||||
var frame_time : f32 = @as(f32, @floatFromInt(new_time - old_time)) / 1_000_000_000.0;
|
||||
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 server.host.service(0)) |event| switch (event) {
|
||||
.connect => |data| {
|
||||
_ = data;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user