Make error sets explicit

This commit is contained in:
Brandon Dyck 2022-01-10 16:45:04 -07:00
parent ed2445e623
commit b734196410
2 changed files with 11 additions and 13 deletions

View File

@ -227,14 +227,15 @@ pub fn Window(comptime TUserData: type) type {
reserved: u27 = 0,
};
pub fn open(title: [*:0]const u8, width: u32, height: u32, flags: OpenFlags) !Window(TUserData) {
const OpenError = error{OpenFailed};
pub fn open(title: [*:0]const u8, width: u32, height: u32, flags: OpenFlags) OpenError!Window(TUserData) {
const cwin: ?*c.mfb_window = c.mfb_open_ex(title, width, height, @bitCast(u32, flags));
if (cwin) |value| {
const win = Window(TUserData){ .cwin = value };
assert(@bitCast(usize, win) == @ptrToInt(win.cwin));
return win;
} else {
return error.ItBroke;
return OpenError.OpenFailed;
}
}
@ -367,12 +368,12 @@ test "set and get target FPS" {
pub const Timer = extern struct {
ctimer: *c.mfb_timer,
pub fn init() !Timer {
pub fn init() std.mem.Allocator.Error!Timer {
const ctimer: ?*c.mfb_timer = c.mfb_timer_create();
if (ctimer) |value| {
return Timer{ .ctimer = value };
} else {
return error.ItBroke;
return std.mem.Allocator.Error.OutOfMemory;
}
}

View File

@ -31,7 +31,7 @@ const FCoords = struct {
const State = struct {
period: f64,
timer: *mfb.Timer,
timer: mfb.Timer,
active: bool = true,
alloc: std.mem.Allocator,
cursor_pos: Coords = undefined,
@ -45,19 +45,16 @@ const State = struct {
cheat_code: []const u8 = "rave",
cheat_code_progress: usize = 0,
pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer);
timer.* = try mfb.Timer.init();
pub fn init(alloc: std.mem.Allocator, period: f64) std.mem.Allocator.Error!State {
return State{
.period = period,
.timer = timer,
.timer = try mfb.Timer.init(),
.alloc = alloc,
};
}
pub fn deinit(self: State) void {
self.timer.*.deinit();
self.alloc.destroy(self.timer);
self.timer.deinit();
}
pub fn cheatInput(self: *State, char: u32) void {
@ -77,7 +74,7 @@ const State = struct {
pub fn render(self: State, buf: Buffer) void {
const period = if (!self.isCheating()) self.period else self.period / 10;
var bgValue = @rem(self.timer.*.now(), period) / period * 512;
var bgValue = @rem(self.timer.now(), period) / period * 512;
if (bgValue >= 256) {
bgValue = 512 - bgValue;
}
@ -112,7 +109,7 @@ const Buffer = struct {
width: u32,
height: u32,
pub fn init(alloc: std.mem.Allocator, width: u32, height: u32) !Buffer {
pub fn init(alloc: std.mem.Allocator, width: u32, height: u32) std.mem.Allocator.Error!Buffer {
const slice = try alloc.alloc(mfb.Rgb, width * height);
return Buffer{
.alloc = alloc,