Cleaner module format.

This commit is contained in:
KikooDX 2021-01-26 14:10:29 +01:00
parent 98eb7ad4e7
commit c07858308a
3 changed files with 55 additions and 52 deletions

View File

@ -1,25 +1,28 @@
//! Level structure, grid containing tile data.
const std = @import("std");
const expect = std.testing.expect;
pub const Level = struct {
width: u16,
height: u16,
buffer: []u16,
const Self = @This();
pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Level {
var level = Level{
.width = width,
.height = height,
.buffer = undefined,
};
level.buffer = try allocator.alloc(u16, width * height);
return level;
}
const cell_t = u16;
pub fn deinit(self: *Level, allocator: *std.mem.Allocator) void {
allocator.free(self.buffer);
}
};
width: u16,
height: u16,
buffer: []cell_t,
pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Self {
var level = Self{
.width = width,
.height = height,
.buffer = undefined,
};
level.buffer = try allocator.alloc(cell_t, width * height);
return level;
}
pub fn deinit(self: *Self, allocator: *std.mem.Allocator) void {
allocator.free(self.buffer);
}
test "create level buffer" {
// Create allocator.
@ -31,9 +34,9 @@ test "create level buffer" {
const allocator = &gpa.allocator;
// Initialize level struct (twice 'cause why not?).
var level: Level = try Level.init(allocator, 64, 32);
var level: Self = try Self.init(allocator, 64, 32);
level.deinit(allocator);
level = try Level.init(allocator, 64, 64);
level = try Self.init(allocator, 64, 64);
defer level.deinit(allocator);
level.buffer[128] = 32;

View File

@ -1,6 +1,6 @@
const std = @import("std");
const assert = std.debug.assert;
const Level = @import("level.zig").Level;
const Level = @import("level.zig");
const ray = @cImport({
@cInclude("raylib.h");
});

View File

@ -2,55 +2,55 @@
//! vectors with integer precision.
const expect = @import("std").testing.expect;
pub const Vec2 = struct {
x: i32,
y: i32,
const Self = @This();
pub fn init(x: i32, y: i32) Vec2 {
return Vec2 {
.x = x,
.y = y,
};
}
x: i32,
y: i32,
pub fn add(self: Vec2, other: Vec2) Vec2 {
return Vec2 {
.x = self.x + other.x,
.y = self.y + other.y,
};
}
pub fn init(x: i32, y: i32) Self {
return Self {
.x = x,
.y = y,
};
}
pub fn sub(self: Vec2, other: Vec2) Vec2 {
return Vec2 {
.x = self.x - other.x,
.y = self.y - other.y,
};
}
};
pub fn add(self: Self, other: Self) Self {
return Self {
.x = self.x + other.x,
.y = self.y + other.y,
};
}
test "create Vec2 w/ positive values" {
const vector = Vec2.init(15984, 95715);
pub fn sub(self: Self, other: Self) Self {
return Self {
.x = self.x - other.x,
.y = self.y - other.y,
};
}
test "create Self w/ positive values" {
const vector = Self.init(15984, 95715);
expect(vector.x == 15984);
expect(vector.y == 95715);
}
test "create Vec2 w/ negative values" {
const vector = Vec2.init(-51428, -56123);
test "create Self w/ negative values" {
const vector = Self.init(-51428, -56123);
expect(vector.x == -51428);
expect(vector.y == -56123);
}
test "add two Vec2 using dot syntax" {
const vector_1 = Vec2.init(6712, 981);
const vector_2 = Vec2.init(-1823, 12);
test "add two Self using dot syntax" {
const vector_1 = Self.init(6712, 981);
const vector_2 = Self.init(-1823, 12);
const vector_sum = vector_1.add(vector_2);
expect(vector_sum.x == 4889);
expect(vector_sum.y == 993);
}
test "calculate difference between two Vec2 using dot syntax" {
const vector_1 = Vec2.init(6712, 981);
const vector_2 = Vec2.init(-1823, 12);
test "calculate difference between two Self using dot syntax" {
const vector_1 = Self.init(6712, 981);
const vector_2 = Self.init(-1823, 12);
const vector_diff = vector_1.sub(vector_2);
expect(vector_diff.x == 8535);
expect(vector_diff.y == 969);