97 lines
3.0 KiB
Zig
97 lines
3.0 KiB
Zig
const std = @import("std");
|
|
const rl = @import("raylib");
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
pub fn vec2Round(vec2: rl.Vector2) rl.Vector2 {
|
|
return rl.Vector2{
|
|
.x = @round(vec2.x),
|
|
.y = @round(vec2.y),
|
|
};
|
|
}
|
|
|
|
pub fn rgb(r: u8, g: u8, b: u8) rl.Color {
|
|
return rl.Color.init(r, g, b, 255);
|
|
}
|
|
|
|
pub fn rgba(r: u8, g: u8, b: u8, a: f32) rl.Color {
|
|
return rl.Color.init(r, g, b, a * 255);
|
|
}
|
|
|
|
pub fn lerpColor(from: rl.Color, to: rl.Color, t: f32) rl.Color {
|
|
const r = std.math.lerp(@as(f32, @floatFromInt(from.r)), @as(f32, @floatFromInt(to.r)), t);
|
|
const g = std.math.lerp(@as(f32, @floatFromInt(from.g)), @as(f32, @floatFromInt(to.g)), t);
|
|
const b = std.math.lerp(@as(f32, @floatFromInt(from.b)), @as(f32, @floatFromInt(to.b)), t);
|
|
const a = std.math.lerp(@as(f32, @floatFromInt(from.a)), @as(f32, @floatFromInt(to.a)), t);
|
|
|
|
return rl.Color{
|
|
.r = @intFromFloat(r),
|
|
.g = @intFromFloat(g),
|
|
.b = @intFromFloat(b),
|
|
.a = @intFromFloat(a),
|
|
};
|
|
}
|
|
|
|
pub fn shiftColorInHSV(color: rl.Color, value_shift: f32) rl.Color {
|
|
if (value_shift == 0) {
|
|
return color;
|
|
}
|
|
|
|
var hsv = rl.colorToHSV(color);
|
|
hsv.z = std.math.clamp(hsv.z * (1 + value_shift), 0, 1);
|
|
var new_color = rl.colorFromHSV(hsv.x, hsv.y, hsv.z);
|
|
new_color.a = color.a;
|
|
return new_color;
|
|
}
|
|
|
|
pub fn drawUnderline(rect: rl.Rectangle, size: f32, color: rl.Color) void {
|
|
rl.drawRectangleRec(rl.Rectangle{
|
|
.x = rect.x,
|
|
.y = rect.y + rect.height - size,
|
|
.width = rect.width,
|
|
.height = size
|
|
}, color);
|
|
}
|
|
|
|
pub fn remap(comptime T: type, from_min: T, from_max: T, to_min: T, to_max: T, value: T) T {
|
|
const t = (value - from_min) / (from_max - from_min);
|
|
return std.math.lerp(to_min, to_max, t);
|
|
}
|
|
|
|
pub fn roundNearestUp(comptime T: type, value: T, multiple: T) T {
|
|
return @ceil(value / multiple) * multiple;
|
|
}
|
|
|
|
pub fn roundNearestDown(comptime T: type, value: T, multiple: T) T {
|
|
return @floor(value / multiple) * multiple;
|
|
}
|
|
|
|
pub fn roundNearestTowardZero(comptime T: type, value: T, multiple: T) T {
|
|
return @trunc(value / multiple) * multiple;
|
|
}
|
|
|
|
pub fn initBoundedStringZ(comptime BoundedString: type, text: []const u8) !BoundedString {
|
|
var bounded_string = try BoundedString.fromSlice(text);
|
|
try bounded_string.append(0);
|
|
return bounded_string;
|
|
}
|
|
|
|
pub fn getBoundedStringZ(bounded_array: anytype) [:0]u8 {
|
|
return bounded_array.buffer[0..(bounded_array.len-1) :0];
|
|
}
|
|
|
|
pub inline fn dumpErrorTrace() void {
|
|
if (@errorReturnTrace()) |trace| {
|
|
std.debug.dumpStackTrace(trace.*);
|
|
}
|
|
}
|
|
|
|
pub fn formatDuration(allocator: std.mem.Allocator, total_seconds: f64, high_accuracy: bool) ![]u8 {
|
|
const seconds = @mod(total_seconds, @as(f64, @floatFromInt(std.time.s_per_min)));
|
|
const minutes = @divFloor(total_seconds, std.time.s_per_min);
|
|
if (high_accuracy) {
|
|
return try std.fmt.allocPrint(allocator, "{d:.0}m {d:.5}s", .{ minutes, seconds });
|
|
} else {
|
|
return try std.fmt.allocPrint(allocator, "{d:.0}m {d:.3}s", .{ minutes, seconds });
|
|
}
|
|
} |