From 1a00dd2342292e545d3aa26c2c0eadc80caedf5c Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 12 Oct 2021 11:35:17 -0400 Subject: No need to recalculate the index inside each loop Here's hoping Zig is smart enough (TM) to constant-ify all of these @floatToInt(type, PlaneXXX) values. --- src/main.zig | 2 +- src/render.zig | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 1211529..01a021c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -71,7 +71,7 @@ pub fn main() !void { .lower_texture = 1, .upper_texture = 1, .floor_texture = 1, - .ceiling_texture = 1, + .ceiling_texture = 2, .draw_down = true, }; diff --git a/src/render.zig b/src/render.zig index ae9983c..00e65fd 100644 --- a/src/render.zig +++ b/src/render.zig @@ -329,22 +329,22 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { const constrained_top = std.math.floor(std.math.min(top_of_floor, lowest_drawn)); const proj_height = cell.floor_height * nominal_length; const constrained_bottom = std.math.max(highest_drawn, top_of_floor - proj_height); - const stop = @floatToInt(usize, PlaneHeight - constrained_bottom); - var pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; + const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; var texel_y: f32 = std.math.max((top_of_floor - constrained_top) / nominal_length, 0); // now we have what we need to draw the face, // and update the z-buffer. Note that it doesn't // matter which way we write to the z_buffer // here, walls are vertical! - while (pix_y < stop) : ({ - pix_y += 1; + while (pix_index < stop) : ({ + pix_index += @floatToInt(usize, PlaneWidth); texel_y += inv_nom_len; }) { const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); const texel = walls_image.getPixel(.{ .x = t_lower_off + texstrip, .y = ty }); - const pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; pixels[pix_index] = texel; self.z_buffer[pix_index] = distance; } @@ -355,22 +355,18 @@ pub fn Renderer(PlaneWidth: f32, PlaneHeight: f32) type { const proj_default_end = PlaneHeight / 2 + PlaneDist * (level.Cell.DEFAULT_HEIGHT - pheight) / distance; const constrained_top = std.math.min(lowest_drawn, proj_default_end); const constrained_bottom = std.math.max(bottom_of_ceiling, highest_drawn); - const stop = @floatToInt(i32, PlaneHeight - constrained_bottom); + const stop = @floatToInt(usize, PlaneWidth * (PlaneHeight - constrained_bottom)) + col; const t_upper_off = cell.upper_texture * td; - var pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); - // TODO: Decide if textures should be pinned at - // the top or bottom. Currently they are pinned - // at the top. For bottom this would read: - // constants.TextureDim - (constrained_top - constrained_bottom) / nominal_length - var texel_y: f32 = (proj_default_end - constrained_top) / nominal_length; - while (pix_y < stop) : ({ - pix_y += 1; + const pix_y = @floatToInt(usize, std.math.ceil(std.math.max(PlaneHeight - constrained_top - 1, 0))); + var pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; + var texel_y: f32 = constants.TextureDim - (constrained_top - constrained_bottom) / nominal_length; + while (pix_index < stop) : ({ + pix_index += @floatToInt(usize, PlaneWidth); texel_y += inv_nom_len; }) { const ty = @floatToInt(c_uint, std.math.modf(texel_y).fpart * constants.TextureDim); const texel = walls_image.getPixel(.{ .x = t_upper_off + texstrip, .y = ty }); - const pix_index = pix_y * @floatToInt(usize, PlaneWidth) + col; pixels[pix_index] = texel; self.z_buffer[pix_index] = distance; } -- cgit v1.3.1