aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortslil clingman <>2021-09-10 16:07:54 -0400
committertslil clingman <>2021-09-10 16:07:54 -0400
commit9a84de9f8d9b033e201d50f574e8333ec8f6d71a (patch)
tree33b2ac8bc6ebb104e9b3d9ef71ca99852dd7198d
parentf59d8c3b31781c4093aa526d7f66544d132765e9 (diff)
Re-organised code, moved all rendering into raycast.zig
-rw-r--r--src/main.zig43
-rw-r--r--src/raycast.zig64
2 files changed, 51 insertions, 56 deletions
diff --git a/src/main.zig b/src/main.zig
index 866b831..f51c3a3 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -28,34 +28,6 @@ const constants = @import("constants.zig");
const raycast = @import("raycast.zig");
const level = @import("level.zig");
-// TODO: is there anyway i can say fn renderSlice : RenderSliceFunction?
-fn renderSlice(
- window: sf.graphics.RenderWindow,
- sprite: sf.graphics.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);
-}
-
pub fn main() !void {
const PI = std.math.pi;
@@ -193,12 +165,15 @@ pub fn main() !void {
});
window.draw(back_sprite, null);
- // Then render the walls
- try player.renderFloorsToTexture(floors_image, rendered_floors_texture, map);
- window.draw(rendered_floors_sprite, null);
-
- // Finally the world
- player.renderWorld(window, walls_sprite, objects_sprite, renderSlice, map);
+ try player.renderWorld(
+ window,
+ walls_sprite,
+ objects_sprite,
+ floors_image,
+ rendered_floors_texture,
+ rendered_floors_sprite,
+ map,
+ );
window.display();
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);
+ }
};
}