diff --git a/src/main.zig b/src/main.zig index b61b8d9..2e8a778 100644 --- a/src/main.zig +++ b/src/main.zig @@ -85,11 +85,12 @@ pub fn main(init: std.process.Init) !void { for (grid.arr, 0..) |line, y| { for (line, 0..) |cell, x| { texture[x + screenWidth * y] = if (cell.alive) - .white + raylib.Color.init(@truncate((255 * y) / screenHeight), @truncate((255 * x) / screenWidth), 0, 255) else .black; } } + raylib.updateTexture(screenTexture, @ptrCast(texture)); 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)); if (raylib.isMouseButtonDown(.left)) { 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))))); - for (@as(usize, @trunc(IndicatorPosition.x)) - @as(usize, @trunc(IndicatorSize))..@as(usize, @trunc(IndicatorPosition.x)) + @as(usize, @trunc(IndicatorSize)), 0..) |x, i| { - circleAsyncArray.?[i] = init.io.async((struct { - pub fn l(IP: raylib.Vector2, IS: f32, fx: usize, rand: *std.Random.DefaultPrng, gr: sand.Grid, pc: u8) void { - for (@as(usize, @trunc(IP.y)) - @as(usize, @trunc(IS))..@as(usize, @trunc(IP.y)) + @as(usize, @trunc(IS))) |y| { - const dx = @as(f32, @floatFromInt(fx)) - IP.x; - const dy = @as(f32, @floatFromInt(y)) - IP.y; - 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) - gr.arr[y][fx].alive = true; + const xstart: usize = @intCast(@max(0, @as(i32, @trunc(IndicatorPosition.x - IndicatorSize)))); + const xend: usize = @intCast(@max(0, @as(i32, @trunc(IndicatorPosition.x + IndicatorSize)))); + if (xstart < xend) { + if (circleAsyncArray) |_| {} else circleAsyncArray = try init.gpa.alloc(std.Io.Future(void), xend - xstart); + for (xstart..xend, 0..) |x, i| { + circleAsyncArray.?[i] = init.io.async((struct { + pub fn l(IP: raylib.Vector2, IS: f32, fx: usize, rand: *std.Random.DefaultPrng, gr: sand.Grid, pc: u8) void { + const ystart: usize = @intCast(@max(0, @as(i32, @trunc(IP.y - IS)))); + 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| {