diff options
Diffstat (limited to 'src/raycast.zig')
| -rw-r--r-- | src/raycast.zig | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/src/raycast.zig b/src/raycast.zig index 41fea5d..a5bb731 100644 --- a/src/raycast.zig +++ b/src/raycast.zig @@ -26,17 +26,6 @@ const Colour = @import("sfml").graphics.Color; const level = @import("level.zig"); const constants = @import("constants.zig"); -pub const RenderSliceFunction: type = fn ( - window: RenderWindow, - sprite: Sprite, - col: i32, // which column we're in - top: f32, // top of wall - draw_frac: f32, - length: f32, // length of slice to be drawn - texfrac: f32, - texture: u8, // which texture index -) void; - pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { const FOV: f32 = std.math.pi / 3.0; const PlanePixels = PlaneWidth * PlaneHeight; @@ -53,10 +42,10 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { vel_y: f32 = 0, acc_x: f32 = 0, acc_y: f32 = 0, - height: f32 = 1.8 / 2.5, // TODO + height: f32 = 1, // TODO z_buffer: std.BoundedArray(f32, PlanePixels), - // standing still at the given location, looking in direction ang, + // standing still at the given location, looking in direction ang pub fn new(pos_x: f32, pos_y: f32, ang: f32) !@This() { const infs = [_]f32{std.math.inf(f32)} ** PlanePixels; return Player(PlaneWidth, PlaneHeight){ @@ -90,20 +79,26 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { window: RenderWindow, walls_sprite: Sprite, objects_sprite: Sprite, - renderSlice: RenderSliceFunction, + floors_image: Image, + rendered_floors_texture: Texture, + rendered_floors_sprite: Sprite, map: level.Map, - ) void { - // reset the z_buffer + ) !void { + // First render the floors + try self.renderFloorsToTexture(floors_image, rendered_floors_texture, map); + window.draw(rendered_floors_sprite, null); + + // then reset the z_buffer var i: usize = 0; while (i < self.z_buffer.len) : (i += 1) { self.z_buffer.set(i, std.math.inf(f32)); } // then render all the walls and populate the z_buffer - self.renderWalls(window, walls_sprite, renderSlice, map); + self.renderWalls(window, walls_sprite, map); // use the z_buffer to render sprites - self.renderObjects(window, objects_sprite, renderSlice, map); + self.renderObjects(window, objects_sprite, map); } fn playerDistComp(pos: [2]f32, lhs: level.Object, rhs: level.Object) bool { @@ -119,7 +114,6 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { self: @This(), window: RenderWindow, objects_sprite: Sprite, - renderSlice: RenderSliceFunction, map: level.Map, ) void { std.sort.sort(level.Object, map.objects.items, @@ -194,7 +188,6 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { self: *@This(), window: RenderWindow, walls_sprite: Sprite, - renderSlice: RenderSliceFunction, map: level.Map, ) void { // This is a TERRIBLE hack: for whatever reason *linearly* @@ -316,7 +309,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { } } - pub fn renderFloorsToTexture( + fn renderFloorsToTexture( self: @This(), floors_image: Image, rendered_floors_texture: Texture, @@ -372,8 +365,35 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { } } } - try rendered_floors_texture.updateFromPixels(&pixels, null); } + + // TODO: is there anyway i can say fn renderSlice : RenderSliceFunction? + fn renderSlice( + window: RenderWindow, + sprite: Sprite, + col: i32, // which column + top: f32, // top of wall + total_height: f32, // how tall + draw_frac: f32, + texfrac: f32, // how far along the texture + texture: u8, // which texture index + ) void { + + // 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 * constants.TextureDim)); + const total_length = total_height * constants.VFact; + + const xpos = @intToFloat(f32, col) * constants.HFact; + const ypos = constants.ScreenHeight / 2 + (constants.PlaneHeight / 2 - top) * constants.VFact; + + const tind = @as(c_int, texture) * @floatToInt(c_int, constants.TextureDim); + const left = tind + @floatToInt(c_int, texfrac * constants.TextureDim); + + sprite.setPosition(.{ .x = xpos, .y = ypos }); + sprite.setScale(.{ .x = constants.HFact, .y = total_length / constants.TextureDim }); + sprite.setTextureRect(.{ .top = 0, .left = left, .width = 1, .height = draw_height }); + window.draw(sprite, null); + } }; } |
