aboutsummaryrefslogtreecommitdiff
path: root/src/raycast.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-09-09 23:18:56 -0400
committertslil clingman <>2021-09-09 23:29:10 -0400
commitd2f6860430f93291a52d1cdaccf6831cd57be9a5 (patch)
tree27d7698d79e678cad961647ea6295da53b6a8a49 /src/raycast.zig
parentcf1eb8fc551f5f3ee3bc0054fc236ff7d201f44a (diff)
Added ceilings and pos_z to sprites (they can now be on the ceiling)
Diffstat (limited to 'src/raycast.zig')
-rw-r--r--src/raycast.zig25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/raycast.zig b/src/raycast.zig
index 2a9b0a1..2b377de 100644
--- a/src/raycast.zig
+++ b/src/raycast.zig
@@ -160,16 +160,16 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
if (left + width < 0 or left >= PlaneWidth) continue;
const height = PlaneDist * obj.height / scaled_perp_distance;
- const top = PlaneHeight / 2 + PlaneDist * (obj.height - self.height) / scaled_perp_distance;
+ const top = PlaneHeight / 2 + PlaneDist * (obj.height - self.height + obj.pos_z) / scaled_perp_distance;
// TODO: likewise?
if (top < 0 or top - height >= PlaneHeight) continue;
// Something is on the screen, let's draw it!
const start = std.math.max(0, left);
- const end = @floatToInt(i32, std.math.min(left + width, PlaneWidth));
+ const end = @floatToInt(i32, std.math.min(left + width, PlaneWidth - 1));
- var tex_frac: f32 = (start - left) / width;
+ var tex_frac: f32 = std.math.clamp((start - left) / width, 0, 1);
var col: i32 = @floatToInt(i32, start);
const tex_frac_step = 1 / width;
while (col < end) : ({
@@ -316,8 +316,13 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
}
}
- pub fn renderFloorsToTexture(self: @This(), floors_image: Image, rendered_floors_texture: Texture, map: level.Map) !void {
- var pixels = [_]Colour{Colour.Black} ** (PlaneWidth * PlaneHeight / 2);
+ pub fn renderFloorsToTexture(
+ self: @This(),
+ floors_image: Image,
+ rendered_floors_texture: Texture,
+ map: level.Map,
+ ) !void {
+ var pixels = [_]Colour{Colour.Black} ** (PlaneWidth * PlaneHeight);
// Again, another TERRIBLE hack: we do the same nasty linear
// interpolation trick and for whatever reason the floors look fine.
@@ -327,8 +332,11 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
const sin_last = std.math.sin(self.ang - 0.5 * FOV);
var row: usize = 0;
- while (row < PlaneHeight / 2) : (row += 1) {
- const row_dist = self.height * PlaneDist / @intToFloat(f32, row + 1);
+ while (row < PlaneHeight) : (row += 1) {
+ if (row == PlaneHeight / 2) continue;
+ const frow = std.math.fabs(PlaneHeight / 2 - @intToFloat(f32, row));
+ const scale = if (row < PlaneHeight / 2) constants.MAX_HEIGHT - self.height else self.height;
+ const row_dist = scale * PlaneDist / frow;
const dx_step = row_dist * (cos_last - cos_first) / PlaneWidth;
const dy_step = row_dist * (sin_last - sin_first) / PlaneWidth;
@@ -352,7 +360,8 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type {
const iy = @floatToInt(i32, sy.ipart);
if (map.inBounds(ix, iy)) {
- const tex = @as(c_uint, map.lookup(ix, iy).floor_texture);
+ const cell = map.lookup(ix, iy);
+ const tex = @as(c_uint, if (row > PlaneHeight / 2) cell.floor_texture else cell.ceiling_texture);
const toff = tex * @floatToInt(c_uint, constants.TextureDim);
const px = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sx.fpart));
const py = @floatToInt(c_uint, constants.TextureDim * std.math.fabs(sy.fpart));