display progress bar

This commit is contained in:
Fabien Freling 2021-04-27 08:30:24 +02:00
parent 61627fb76c
commit 80fed125b4

View file

@ -44,6 +44,26 @@ fn rayColor(ray: Ray, world: World) Color {
return white.mul(1.0 - t).add(blue.mul(t));
}
fn setProgress(surface: *sdl.c.SDL_Surface, width: usize, height: usize, percent: f32) void {
if (percent == 1.0) {
return;
}
const progressBarHeight = 3;
const progressWidth = @floatToInt(usize, @intToFloat(f32, width) * percent);
const progressColor = Color{ .x = 1.0 };
var j: usize = 0;
while (j < progressBarHeight) {
var i: usize = 0;
while (i < progressWidth) {
sdl.setSurfacePixel(surface, i, j, progressColor);
i += 1;
}
j += 1;
}
}
pub fn main() anyerror!void {
if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
@ -90,10 +110,26 @@ pub fn main() anyerror!void {
pixel.* = Color{};
}
{
_ = sdl.c.SDL_LockSurface(surface);
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
return error.SDLUpdateWindowFailed;
}
var running = true;
var k: usize = 0;
while (running) {
var event: sdl.c.SDL_Event = undefined;
while (sdl.c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
sdl.c.SDL_QUIT => {
running = false;
},
else => {},
}
}
// Render
{
while (k < samplesPerPixel) {
_ = sdl.c.SDL_LockSurface(surface);
@ -118,28 +154,17 @@ pub fn main() anyerror!void {
}
j += 1;
}
setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
sdl.c.SDL_UnlockSurface(surface);
_ = sdl.c.SDL_UpdateWindowSurface(window);
k += 1;
}
}
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
return error.SDLUpdateWindowFailed;
}
var running = true;
while (running) {
var event: sdl.c.SDL_Event = undefined;
while (sdl.c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
sdl.c.SDL_QUIT => {
running = false;
},
else => {},
}
}
if (k >= samplesPerPixel) {
sdl.c.SDL_Delay(1000 / fps);
}
}
}