aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-09-01 13:57:08 -0400
committertslil clingman <>2021-09-01 14:03:40 -0400
commitc97c9fc4203dc12982b590d3bfae1d419cf59fbe (patch)
treea096a8514da1d5fd25d8fade4c7322d4a5f773c2 /src/main.zig
parentdae5c3d1e9bdbabf94f79960b2fca9a5f47b7ac0 (diff)
Variable height walls!
Diffstat (limited to 'src/main.zig')
-rw-r--r--[-rwxr-xr-x]src/main.zig26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig
index e912b5f..64da425 100755..100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,4 +1,4 @@
-// ZiRC, a Zig raycaster for fun
+// ZiRC, a Zig raycaster for fun
//
// Copyright (C) 2021, tslil clingman
//
@@ -31,25 +31,33 @@ usingnamespace @import("map.zig");
const screenWidth = 1280;
const screenHeight = 960;
+// TODO: is there anyway i can say fn renderWall : RenderWallFunction?
fn renderWall(
window: sf.graphics.RenderWindow,
sprite: sf.graphics.Sprite,
col: i32, // which column
top: f32, // top of wall
- height: f32, // how tall
+ total_height: f32, // how tall
+ draw_frac: f32,
texfrac: f32, // how far along the texture
texture: u8, // which texture index
-) !void {
+) void {
const hFact = screenWidth / planeWidth;
const vFact = screenHeight / planeHeight;
+
+ // we need ceil here so that we draw always to or past the edge of the screen
+ const draw_height = @floatToInt(c_int, std.math.ceil(draw_frac * wallTextureDim));
+ const total_length = total_height * vFact;
+
const xpos = @intToFloat(f32, col) * hFact;
- const length = height * vFact;
- const starty = (screenHeight + vFact * planeHeight) / 2 - top * vFact;
+ const ypos = (screenHeight + vFact * planeHeight) / 2 - top * vFact;
+
const tind = @as(c_int, texture) * @floatToInt(c_int, 1 + wallTextureDim);
+ const left = tind + @floatToInt(c_int, texfrac * wallTextureDim);
- sprite.setPosition(.{ .x = xpos, .y = starty });
- sprite.setScale(.{ .x = hFact, .y = length / wallTextureDim });
- sprite.setTextureRect(.{ .top = 0, .left = tind + @floatToInt(c_int, texfrac * wallTextureDim), .width = 1, .height = wallTextureDim });
+ sprite.setPosition(.{ .x = xpos, .y = ypos });
+ sprite.setScale(.{ .x = hFact, .y = total_length / wallTextureDim });
+ sprite.setTextureRect(.{ .top = 0, .left = left, .width = 1, .height = draw_height });
window.draw(sprite, null);
}
@@ -165,7 +173,7 @@ pub fn main() !void {
});
window.draw(backSprite, null);
- try player.renderMapUsing(window, wallSprite, map, renderWall);
+ player.renderMapUsing(window, wallSprite, map, renderWall);
window.display();