less crashes and pretty colors

This commit is contained in:
Dario48 2026-04-19 20:21:36 +02:00
commit 3a376e8954

View file

@ -85,11 +85,12 @@ pub fn main(init: std.process.Init) !void {
for (grid.arr, 0..) |line, y| { for (grid.arr, 0..) |line, y| {
for (line, 0..) |cell, x| { for (line, 0..) |cell, x| {
texture[x + screenWidth * y] = if (cell.alive) texture[x + screenWidth * y] = if (cell.alive)
.white raylib.Color.init(@truncate((255 * y) / screenHeight), @truncate((255 * x) / screenWidth), 0, 255)
else else
.black; .black;
} }
} }
raylib.updateTexture(screenTexture, @ptrCast(texture)); raylib.updateTexture(screenTexture, @ptrCast(texture));
raylib.drawTexture(screenTexture, 0, 0, .white); raylib.drawTexture(screenTexture, 0, 0, .white);
@ -108,18 +109,24 @@ pub fn main(init: std.process.Init) !void {
raylib.drawCircleV(IndicatorPosition, IndicatorSize, .alpha(.gray, 0.5)); raylib.drawCircleV(IndicatorPosition, IndicatorSize, .alpha(.gray, 0.5));
if (raylib.isMouseButtonDown(.left)) { if (raylib.isMouseButtonDown(.left)) {
if (placeChace > 170) placeChace -= 1; if (placeChace > 170) placeChace -= 1;
if (circleAsyncArray) |_| {} else circleAsyncArray = try init.gpa.alloc(std.Io.Future(void), (@as(usize, @trunc(IndicatorPosition.y)) + @as(usize, @trunc(IndicatorSize)) - (@as(usize, @trunc(IndicatorPosition.y)) - @as(usize, @trunc(IndicatorSize))))); const xstart: usize = @intCast(@max(0, @as(i32, @trunc(IndicatorPosition.x - IndicatorSize))));
for (@as(usize, @trunc(IndicatorPosition.x)) - @as(usize, @trunc(IndicatorSize))..@as(usize, @trunc(IndicatorPosition.x)) + @as(usize, @trunc(IndicatorSize)), 0..) |x, i| { const xend: usize = @intCast(@max(0, @as(i32, @trunc(IndicatorPosition.x + IndicatorSize))));
circleAsyncArray.?[i] = init.io.async((struct { if (xstart < xend) {
pub fn l(IP: raylib.Vector2, IS: f32, fx: usize, rand: *std.Random.DefaultPrng, gr: sand.Grid, pc: u8) void { if (circleAsyncArray) |_| {} else circleAsyncArray = try init.gpa.alloc(std.Io.Future(void), xend - xstart);
for (@as(usize, @trunc(IP.y)) - @as(usize, @trunc(IS))..@as(usize, @trunc(IP.y)) + @as(usize, @trunc(IS))) |y| { for (xstart..xend, 0..) |x, i| {
const dx = @as(f32, @floatFromInt(fx)) - IP.x; circleAsyncArray.?[i] = init.io.async((struct {
const dy = @as(f32, @floatFromInt(y)) - IP.y; pub fn l(IP: raylib.Vector2, IS: f32, fx: usize, rand: *std.Random.DefaultPrng, gr: sand.Grid, pc: u8) void {
if (fx >= screenWidth or fx < 0 or y >= screenHeight or y < 0) {} else if (pow(f32, dx, 2) + pow(f32, dy, 2) <= pow(f32, IS, 2) and rand.random().int(u8) > pc) const ystart: usize = @intCast(@max(0, @as(i32, @trunc(IP.y - IS))));
gr.arr[y][fx].alive = true; const yend: usize = @intCast(@max(0, @as(i32, @trunc(IP.y + IS))));
for (ystart..yend) |y| {
const dx = @as(f32, @floatFromInt(fx)) - IP.x;
const dy = @as(f32, @floatFromInt(y)) - IP.y;
if (fx >= screenWidth or y >= screenHeight) {} else if (pow(f32, dx, 2) + pow(f32, dy, 2) <= pow(f32, IS, 2) and rand.random().int(u8) > pc)
gr.arr[y][fx].alive = true;
}
} }
} }).l, .{ IndicatorPosition, IndicatorSize, x, &random, grid, placeChace });
}).l, .{ IndicatorPosition, IndicatorSize, x, &random, grid, placeChace }); }
} }
} }
if (circleAsyncArray) |arr| { if (circleAsyncArray) |arr| {