display progress bar
This commit is contained in:
parent
61627fb76c
commit
80fed125b4
|
@ -44,6 +44,26 @@ fn rayColor(ray: Ray, world: World) Color {
|
||||||
return white.mul(1.0 - t).add(blue.mul(t));
|
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 {
|
pub fn main() anyerror!void {
|
||||||
if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
|
if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
|
||||||
std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
|
std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
|
||||||
|
@ -90,10 +110,26 @@ pub fn main() anyerror!void {
|
||||||
pixel.* = Color{};
|
pixel.* = Color{};
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
|
||||||
_ = sdl.c.SDL_LockSurface(surface);
|
std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
|
||||||
|
return error.SDLUpdateWindowFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
var running = true;
|
||||||
var k: usize = 0;
|
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) {
|
while (k < samplesPerPixel) {
|
||||||
_ = sdl.c.SDL_LockSurface(surface);
|
_ = sdl.c.SDL_LockSurface(surface);
|
||||||
|
|
||||||
|
@ -118,28 +154,17 @@ pub fn main() anyerror!void {
|
||||||
}
|
}
|
||||||
j += 1;
|
j += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
|
||||||
|
|
||||||
sdl.c.SDL_UnlockSurface(surface);
|
sdl.c.SDL_UnlockSurface(surface);
|
||||||
_ = sdl.c.SDL_UpdateWindowSurface(window);
|
_ = sdl.c.SDL_UpdateWindowSurface(window);
|
||||||
k += 1;
|
k += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
|
if (k >= samplesPerPixel) {
|
||||||
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 => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sdl.c.SDL_Delay(1000 / fps);
|
sdl.c.SDL_Delay(1000 / fps);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue