From e95d2bd264df2682f05db4a5873594af52a7583a Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Sun, 5 Sep 2021 20:45:43 -0400 Subject: Changed map scaling, texture indexing, finished object rendering! --- floors.png | Bin 980 -> 347 bytes objects.png | Bin 302 -> 544 bytes src/main.zig | 23 ++++++++++++----------- src/map.zig | 2 +- src/raycast.zig | 23 +++++++++++++---------- walls.png | Bin 581 -> 548 bytes 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/floors.png b/floors.png index cf3892a..7351b3a 100644 Binary files a/floors.png and b/floors.png differ diff --git a/objects.png b/objects.png index bc33c52..0efff2c 100644 Binary files a/objects.png and b/objects.png differ diff --git a/src/main.zig b/src/main.zig index 8129ae2..cf3839d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -47,7 +47,7 @@ fn renderSlice( const xpos = @intToFloat(f32, col) * HFact; const ypos = ScreenHeight / 2 + (PlaneHeight / 2 - top) * VFact; - const tind = @as(c_int, texture) * @floatToInt(c_int, 1 + TextureDim); + const tind = @as(c_int, texture) * @floatToInt(c_int, TextureDim); const left = tind + @floatToInt(c_int, texfrac * TextureDim); sprite.setPosition(.{ .x = xpos, .y = ypos }); @@ -63,7 +63,7 @@ pub fn main() !void { //-------------------------------------------------------------------------- var player = try Player(PlaneWidth, PlaneHeight).new( - 15, // x coord + 7, // x coord 1.5, // y coord PI / 2.0, // angle ); @@ -71,21 +71,22 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); - var map = try Map.new(32, 32, &gpa.allocator); + var map = try Map.new(16, 16, &gpa.allocator); defer map.deinit(); var y: u32 = 0; - while (y < 32) : (y += 1) { - map.cells.items[y * 32 + 00] = Cell{ .height = MAX_HEIGHT }; - map.cells.items[y * 32 + 31] = Cell{ .height = MAX_HEIGHT, .wall_texture = 2 }; - map.cells.items[31 * 32 + y] = Cell{ .height = MAX_HEIGHT }; - map.cells.items[00 * 32 + y] = Cell{ .height = MAX_HEIGHT }; + while (y < 16) : (y += 1) { + map.cells.items[y * 16 + 00] = Cell{ .height = MAX_HEIGHT }; + map.cells.items[y * 16 + 15] = Cell{ .height = MAX_HEIGHT, .wall_texture = 2 }; + map.cells.items[15 * 16 + y] = Cell{ .height = MAX_HEIGHT }; + map.cells.items[00 * 16 + y] = Cell{ .height = MAX_HEIGHT }; } - map.cells.items[32 * 15 + 15] = Cell{ .height = MAX_HEIGHT, .wall_texture = 1 }; - map.cells.items[32 * 15 + 14] = Cell{ .height = MAX_HEIGHT / 3, .wall_texture = 1, .floor_texture = 1 }; + map.cells.items[16 * 7 + 7] = Cell{ .height = MAX_HEIGHT, .wall_texture = 1 }; + map.cells.items[16 * 7 + 6] = Cell{ .height = MAX_HEIGHT / 3, .wall_texture = 1, .floor_texture = 1 }; - try map.objects.append(Object{.pos_x = 14.5, .pos_y = 16.5, .texture = 0, .height = 1.5, .width = 1}); + try map.objects.append(Object{.pos_x = 6.5, .pos_y = 8.5, .texture = 0, .height = 1, .width = 1}); + try map.objects.append(Object{.pos_x = 4, .pos_y = 8.5, .texture = 1, .height = 1, .width = 1}); // Initialise SFML //-------------------------------------------------------------------------- diff --git a/src/map.zig b/src/map.zig index 92ca5be..b73fd36 100644 --- a/src/map.zig +++ b/src/map.zig @@ -18,7 +18,7 @@ const std = @import("std"); // Let's pretend that our units are meters -pub const MAX_HEIGHT: f32 = 2.5; +pub const MAX_HEIGHT: f32 = 1; pub const Object = struct { height : f32, diff --git a/src/raycast.zig b/src/raycast.zig index 1f1fd7f..41458c7 100644 --- a/src/raycast.zig +++ b/src/raycast.zig @@ -47,7 +47,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { acc_x: f32 = 0, acc_y: f32 = 0, fov: f32 = std.math.pi / 3.0, - height: f32 = 1.8, // TODO + height: f32 = 1.8 / 2.5, // TODO z_buffer: std.BoundedArray(f32, PlanePixels), const FOV: f32 = std.math.pi / 3.0; @@ -107,16 +107,15 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { self.renderObjects(window, objects_sprite, renderSlice, map); } - fn playerDistComp(self: @This(), lhs: Object, rhs: Object) bool { - const lx = lhs.pos_x - self.pos_x; - const ly = lhs.pos_y - self.pos_y; - const rx = rhs.pos_x - self.pos_x; - const ry = rhs.pos_y - self.pos_y; + fn playerDistComp(pos : [2]f32, lhs: Object, rhs: Object) bool { + const lx = lhs.pos_x - pos[0]; + const ly = lhs.pos_y - pos[1]; + const rx = rhs.pos_x - pos[0]; + const ry = rhs.pos_y - pos[1]; - return (lx * lx + ly * ly < rx * rx + ry * ry); + return (lx * lx + ly * ly > rx * rx + ry * ry); } - // Assumes objects are sorted by proximity! fn renderObjects( self: @This(), window: RenderWindow, @@ -124,7 +123,11 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { renderSlice: RenderSliceFunction, map: Map, ) void { - //std.sort.sort(Object, map.objects.items, {}, self.playerDistComp); + std.sort.sort(Object, map.objects.items, + // Wow, context with an arbitrary type! No macros, just + // Zig all the way down! + [2]f32{self.pos_x, self.pos_y}, + playerDistComp); const self_cos = std.math.cos(self.ang); const self_sin = std.math.sin(self.ang); @@ -349,7 +352,7 @@ pub fn Player(PlaneWidth: f32, PlaneHeight: f32) type { if (map.inBounds(ix, iy)) { const tex = @as(c_uint, map.lookup(ix, iy).floor_texture); - const toff = tex * @floatToInt(c_uint, 1 + TextureDim); + const toff = tex * @floatToInt(c_uint, TextureDim); const px = @floatToInt(c_uint, TextureDim * std.math.fabs(sx.fpart)); const py = @floatToInt(c_uint, TextureDim * std.math.fabs(sy.fpart)); diff --git a/walls.png b/walls.png index b8d0a72..fe8fe40 100644 Binary files a/walls.png and b/walls.png differ -- cgit v1.3.1