hadean-zig/src/geometry/Vec2i.zig

71 lines
1.5 KiB
Zig

const Vec2i = @This();
const Recti = @import("Recti.zig");
const Vec2f = @import("Vec2f.zig");
const Layer = @import("../Layer.zig");
const Color = @import("../Color.zig");
const shaders = @import("../shaders.zig");
const std = @import("std");
const c = @cImport({
@cInclude("glad/glad.h");
@cInclude("GLFW/glfw3.h");
});
x: i32,
y: i32,
pub fn create(x: i32, y: i32) Vec2i {
return .{
.x = x,
.y = y,
};
}
pub fn scale(self: *const Vec2i, s: i32) Vec2i {
return .{
.x = self.x * s,
.y = self.y * s
};
}
pub fn to_unit_recti(self: *const Vec2i) Recti {
return Recti.from_xywh(self.x, self.y, 1, 1);
}
pub fn draw(self: *const Vec2i, radius: f32, layer: Layer, color: Color) void {
shaders.disable_textures();
const segments = 8;
c.glBegin(c.GL_POLYGON);
{
for (0..segments) |segment| {
const theta = (2.0 * std.math.pi * @as(f32, @floatFromInt(segment))) / segments;
const x = radius * std.math.cos(theta);
const y = radius * std.math.sin(theta);
c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, color.r, color.g, color.b, color.a);
c.glVertex3f(
@as(f32, @floatFromInt(self.x)) + x,
@as(f32, @floatFromInt(self.y)) + y,
@as(f32, @floatFromInt(layer.z_index))
);
}
}
c.glEnd();
}
pub fn to_vec2f(self: *const Vec2i) Vec2f {
return Vec2f {
.x = @floatFromInt(self.x),
.y = @floatFromInt(self.y),
};
}
pub const ZERO = create(0, 0);
pub const ONE = create(1, 1);
pub const NORTH = create(0, -1);
pub const SOUTH = create(0, 1);
pub const EAST = create(1, 0);
pub const WEST = create(-1, 0);