diff --git a/in_one_weekend/src/main.zig b/in_one_weekend/src/main.zig index 7c145ab..2277b18 100644 --- a/in_one_weekend/src/main.zig +++ b/in_one_weekend/src/main.zig @@ -128,43 +128,43 @@ pub fn main() anyerror!void { } } - // Render - { - while (k < samplesPerPixel) { - _ = sdl.c.SDL_LockSurface(surface); + if (k < samplesPerPixel) { - var j: usize = 0; - while (j < imageHeight) { - var i: usize = 0; - while (i < imageWidth) { - var imageIndex = i + j * imageWidth; - var pixelColor = &pixelAccu[imageIndex]; + // Render + _ = sdl.c.SDL_LockSurface(surface); - const u = (@intToFloat(f32, i) + prng.random.float(f32)) / @intToFloat(f32, (imageWidth - 1)); - const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1)); - const r = camera.getRay(u, v); - const sample = rayColor(r, world); - pixelColor.* = pixelColor.*.add(sample); + var j: usize = 0; + while (j < imageHeight) { + var i: usize = 0; + while (i < imageWidth) { + var imageIndex = i + j * imageWidth; + var pixelColor = &pixelAccu[imageIndex]; - const averageColor = color.averageColor(pixelColor, @intCast(i32, k)); - // SDL coordinate system is flipped compared to the raytracer - sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor); + const u = (@intToFloat(f32, i) + prng.random.float(f32)) / @intToFloat(f32, (imageWidth - 1)); + const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1)); + const r = camera.getRay(u, v); + const sample = rayColor(r, world); + pixelColor.* = pixelColor.*.add(sample); - i += 1; - } - j += 1; + const averageColor = color.averageColor(pixelColor, @intCast(i32, k)); + // SDL coordinate system is flipped compared to the raytracer + sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor); + + i += 1; } - - setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel)); - - sdl.c.SDL_UnlockSurface(surface); - _ = sdl.c.SDL_UpdateWindowSurface(window); - k += 1; + j += 1; } - } - if (k >= samplesPerPixel) { + setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel)); + + sdl.c.SDL_UnlockSurface(surface); + _ = sdl.c.SDL_UpdateWindowSurface(window); + k += 1; + + } else { + sdl.c.SDL_Delay(1000 / fps); + } } }