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, 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)); const cwin: ?*c.mfb_window = c.mfb_open_ex(title, width, height, @bitCast(u32, flags));
if (cwin) |value| { if (cwin) |value| {
const win = Window(TUserData){ .cwin = value }; const win = Window(TUserData){ .cwin = value };
assert(@bitCast(usize, win) == @ptrToInt(win.cwin)); assert(@bitCast(usize, win) == @ptrToInt(win.cwin));
return win; return win;
} else { } else {
return error.ItBroke; return OpenError.OpenFailed;
} }
} }
@ -367,12 +368,12 @@ test "set and get target FPS" {
pub const Timer = extern struct { pub const Timer = extern struct {
ctimer: *c.mfb_timer, 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(); const ctimer: ?*c.mfb_timer = c.mfb_timer_create();
if (ctimer) |value| { if (ctimer) |value| {
return Timer{ .ctimer = value }; return Timer{ .ctimer = value };
} else { } else {
return error.ItBroke; return std.mem.Allocator.Error.OutOfMemory;
} }
} }

View File

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