add logging

This commit is contained in:
jonathan santis 2024-11-28 10:40:05 +01:00
parent ff1bcaa7dc
commit c3cd0f5aa9
3 changed files with 51 additions and 22 deletions

View File

@ -2,35 +2,21 @@ const std = @import("std");
const rl = @import("raylib");
const rg = @import("raygui");
const pr = @import("primitives.zig");
const plot = @import("plot.zig");
const allocator = std.heap.page_allocator;
var prng = std.rand.DefaultPrng.init(124346556);
var prng = std.rand.DefaultPrng.init(124346234556);
const rand = prng.random();
const screenWidth = 1200;
const screenHeight = 800;
fn randMinMaxFloat(min: f32, max: f32) f32 {
var r1 = rand.float(f32);
r1 = 2 * (r1 - 0.5); //-0.5..0.5
return (@abs(min) + @abs(max)) / 2 * r1;
}
test "random" {
var seed: u64 = undefined;
var r1: f32 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
prng = std.rand.DefaultPrng.init(seed);
while (true) {
r1 = randMinMaxFloat(-10, 10);
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
if (r1 > 8 or r1 < -8) {
std.debug.print("Greater:{}\n", .{r1});
}
}
}
const screenWidth = 1200;
const screenHeight = 800;
const Particle = struct {
const Self = @This();
position: pr.Vec = .{ .a = .{ .x = 0, .y = 0, .z = 0 } },
@ -193,6 +179,12 @@ pub fn main() anyerror!void {
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
const file = try plot.open("plot.dat");
try plot.log(file,"test\ntest2\n");
var plotStr : []u8 = undefined;
var plotStrBuf : [128]u8 = undefined;
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
rl.beginDrawing();
defer rl.endDrawing();
@ -211,11 +203,17 @@ pub fn main() anyerror!void {
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
}
rl.beginBlendMode(rl.BlendMode.blend_additive);
emitter.update(texture);
rl.endBlendMode();
plotStr = try std.fmt.bufPrint(&plotStrBuf,"{}, {}, {}\n",.{emitter.particles[0].acceleration.a.x,emitter.particles[0].position.a.x,emitter.particles[0].velocity.a.x});
try plot.log(file,plotStr);
emitter.update(texture);
}
plot.close(file);
}
test "simple test" {
@ -224,3 +222,18 @@ test "simple test" {
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "random" {
var seed: u64 = undefined;
var r1: f32 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
prng = std.rand.DefaultPrng.init(seed);
while (true) {
r1 = randMinMaxFloat(-10, 10);
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
if (r1 > 8 or r1 < -8) {
std.debug.print("Greater:{}\n", .{r1});
}
}
}

16
src/plot.zig Normal file
View File

@ -0,0 +1,16 @@
const std = @import("std");
pub fn log(self : std.fs.File,data : []const u8) !void
{
try self.writeAll(data);
}
pub fn open(file : []const u8) !std.fs.File
{
const fileh = try std.fs.cwd().createFile(file,.{.read = true});
return fileh;
}
pub fn close(file : std.fs.File) void
{
file.close();
}

Binary file not shown.