aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig2
-rw-r--r--floors.pngbin347 -> 399 bytes
-rw-r--r--objects.pngbin524 -> 628 bytes
-rw-r--r--src/level.zig3
-rw-r--r--src/main.zig7
-rw-r--r--src/raycast.zig25
-rw-r--r--src/sfml/graphics/Image.zig11
-rw-r--r--src/sfml/graphics/texture.zig11
8 files changed, 27 insertions, 32 deletions
diff --git a/build.zig b/build.zig
index af7ca00..b809432 100644
--- a/build.zig
+++ b/build.zig
@@ -8,12 +8,10 @@ pub fn build(b: *Builder) void {
const exe = b.addExecutable("zirc", "src/main.zig");
exe.linkLibC();
exe.addPackagePath("sfml", "src/sfml/sfml.zig");
- exe.addLibPath("csfml/lib/msvc/");
exe.linkSystemLibrary("csfml-graphics");
exe.linkSystemLibrary("csfml-system");
exe.linkSystemLibrary("csfml-window");
exe.linkSystemLibrary("csfml-audio");
- exe.addIncludeDir("csfml/include/");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
diff --git a/floors.png b/floors.png
index 7351b3a..19254e7 100644
--- a/floors.png
+++ b/floors.png
Binary files differ
diff --git a/objects.png b/objects.png
index 54f4d06..ee617b2 100644
--- a/objects.png
+++ b/objects.png
Binary files differ
diff --git a/src/level.zig b/src/level.zig
index 5a63ebb..610eb7b 100644
--- a/src/level.zig
+++ b/src/level.zig
@@ -23,13 +23,14 @@ pub const Object = struct {
texture : u8,
pos_x : f32,
pos_y : f32,
+ pos_z : f32 = 0,
};
pub const Cell = struct {
height: f32,
wall_texture: u8 = 0,
floor_texture: u8 = 0,
- ceiling_texture: u8 = 0,
+ ceiling_texture: u8 = 2,
const floor = Cell{ .height = 0 };
};
diff --git a/src/main.zig b/src/main.zig
index 2def0e8..8f8753d 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -85,8 +85,9 @@ pub fn main() !void {
map.cells.items[16 * 7 + 7] = level.Cell{ .height = constants.MAX_HEIGHT, .wall_texture = 1 };
map.cells.items[16 * 7 + 6] = level.Cell{ .height = constants.MAX_HEIGHT / 3, .wall_texture = 1, .floor_texture = 1 };
- try map.objects.append(level.Object{ .pos_x = 6.5, .pos_y = 8.5, .texture = 0, .height = 1, .width = 1 });
+ try map.objects.append(level.Object{ .pos_x = 5.5, .pos_y = 7.5, .pos_z = 0, .texture = 2, .height = 1, .width = 1 });
try map.objects.append(level.Object{ .pos_x = 4, .pos_y = 8.5, .texture = 1, .height = 1, .width = 1 });
+ try map.objects.append(level.Object{ .pos_x = 6.5, .pos_y = 8.5, .texture = 0, .height = 1, .width = 1 });
// Initialise SFML
//--------------------------------------------------------------------------
@@ -132,14 +133,14 @@ pub fn main() !void {
var floors_image = try sf.Image.createFromFile("floors.png");
defer floors_image.destroy();
- var rendered_floors_texture = try sf.Texture.create(.{ .x = constants.PlaneWidth, .y = constants.PlaneHeight / 2 });
+ var rendered_floors_texture = try sf.Texture.create(.{ .x = constants.PlaneWidth, .y = constants.PlaneHeight });
defer rendered_floors_texture.destroy();
rendered_floors_texture.setSmooth(false);
var rendered_floors_sprite = try sf.Sprite.createFromTexture(rendered_floors_texture);
defer rendered_floors_sprite.destroy();
- rendered_floors_sprite.setPosition(.{ .x = 0, .y = constants.ScreenHeight / 2 });
+ rendered_floors_sprite.setPosition(.{ .x = 0, .y = 0 });
rendered_floors_sprite.setScale(.{ .x = constants.HFact, .y = constants.VFact });
// Start your engines!
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));
diff --git a/src/sfml/graphics/Image.zig b/src/sfml/graphics/Image.zig
index 9fc6530..8ddf8e3 100644
--- a/src/sfml/graphics/Image.zig
+++ b/src/sfml/graphics/Image.zig
@@ -72,15 +72,8 @@ pub fn setPixel(self: Image, pixel_pos: sf.Vector2u, color: sf.Color) void {
/// Gets the size of this image
pub fn getSize(self: Image) sf.Vector2u {
- // This is a hack
- _ = sf.c.sfImage_getSize(self.ptr);
- // Register Rax holds the return val of function calls that can fit in a register
- const rax: usize = asm volatile (""
- : [ret] "={rax}" (-> usize)
- );
- var x: u32 = @truncate(u32, (rax & 0x00000000FFFFFFFF) >> 00);
- var y: u32 = @truncate(u32, (rax & 0xFFFFFFFF00000000) >> 32);
- return sf.Vector2u{ .x = x, .y = y };
+ const size = sf.c.sfImage_getSize(self.ptr);
+ return sf.Vector2u{ .x = size.x, .y = size.y };
}
/// Pointer to the csfml texture
diff --git a/src/sfml/graphics/texture.zig b/src/sfml/graphics/texture.zig
index ac4cfbb..317738a 100644
--- a/src/sfml/graphics/texture.zig
+++ b/src/sfml/graphics/texture.zig
@@ -75,15 +75,8 @@ pub const Texture = union(TextureType) {
/// Gets the size of this image
pub fn getSize(self: Self) sf.Vector2u {
- // This is a hack
- _ = sf.c.sfTexture_getSize(self.get());
- // Register Rax holds the return val of function calls that can fit in a register
- const rax: usize = asm volatile (""
- : [ret] "={rax}" (-> usize)
- );
- var x: u32 = @truncate(u32, (rax & 0x00000000FFFFFFFF) >> 00);
- var y: u32 = @truncate(u32, (rax & 0xFFFFFFFF00000000) >> 32);
- return sf.Vector2u{ .x = x, .y = y };
+ const size = sf.c.sfTexture_getSize(self.get());
+ return sf.Vector2u{ .x = size.x, .y = size.y };
}
/// Gets the pixel count of this image
pub fn getPixelCount(self: Self) usize {