Various cleanup

I replaced the rgb function with a packed struct, formatted the code,
and updated a couple of allocator things to fit Zig v0.9.0.
This commit is contained in:
2022-01-09 11:26:21 -07:00
parent f7754c81fb
commit 293b8729c0
3 changed files with 122 additions and 86 deletions

View File

@ -6,8 +6,8 @@ const max = std.math.max;
const Width = 800;
const Height = 600;
fn grey(value: u8) u32 {
return mfb.rgb(value, value, value);
fn grey(value: u8) mfb.Rgb {
return mfb.Rgb{ .r = value, .g = value, .b = value };
}
const Coords = struct {
@ -17,45 +17,52 @@ const Coords = struct {
const State = struct {
period: f64,
timer: mfb.Timer,
timer: *mfb.Timer,
active: bool = true,
alloc: std.mem.Allocator,
pub fn init(period: f64) !State {
const timer = try mfb.Timer.init();
pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer);
timer.* = try mfb.Timer.init();
return State{
.period=period,
.timer=timer,
.period = period,
.timer = timer,
.alloc = alloc,
};
}
pub fn deinit(self: State) void {
self.timer.deinit();
self.timer.*.deinit();
self.alloc.destroy(self.timer);
}
pub fn render(self: State, buf: Buffer) void {
var bgValue = @rem(self.timer.now(), self.period) / self.period * 512;
var bgValue = @rem(self.timer.*.now(), self.period) / self.period * 512;
if (bgValue >= 256) {
bgValue = 512 - bgValue;
}
buf.drawRectangle(.{.x=0,.y=0}, .{.x=Width, .y=Height}, grey(@floatToInt(u8, bgValue)));
buf.drawRectangle(.{.x=Width/3, .y=Height/3}, .{.x=2*Width/3, .y=2*Height/3}, mfb.rgb(255,0,0));
buf.drawRectangle(.{.x=0,.y=0}, .{.x=10,.y=10}, mfb.rgb(0,0,255));
const rectColor = if (self.active) mfb.Rgb{ .r = 0, .g = 255, .b = 0 } else mfb.Rgb{ .r = 255, .g = 0, .b = 0 };
buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = Width, .y = Height }, grey(@floatToInt(u8, bgValue)));
buf.drawRectangle(.{ .x = Width / 3, .y = Height / 3 }, .{ .x = 2 * Width / 3, .y = 2 * Height / 3 }, rectColor);
buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 }, mfb.Rgb{ .r = 0, .g = 0, .b = 255 });
}
};
const Buffer = struct {
alloc: *std.mem.Allocator,
slice: []u32,
alloc: std.mem.Allocator,
slice: []mfb.Rgb,
width: u32,
height: u32,
pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) !Buffer {
const slice = try alloc.alloc(u32, width*height);
return Buffer {
.alloc=alloc,
.slice=slice,
.width=width,
.height=height,
pub fn init(alloc: std.mem.Allocator, width: u32, height: u32) !Buffer {
const slice = try alloc.alloc(mfb.Rgb, width * height);
return Buffer{
.alloc = alloc,
.slice = slice,
.width = width,
.height = height,
};
}
@ -63,27 +70,27 @@ const Buffer = struct {
self.alloc.free(self.slice);
}
pub fn setPixel(self: Buffer, coords: Coords, color: u32) void {
pub fn setPixel(self: Buffer, coords: Coords, color: mfb.Rgb) void {
const x = @intCast(u32, coords.x);
const y = @intCast(u32, coords.y);
self.slice[y*self.width + x] = color;
self.slice[y * self.width + x] = color;
}
pub fn drawRectangle(self: Buffer, a: Coords, b: Coords, color: u32) void {
pub fn drawRectangle(self: Buffer, a: Coords, b: Coords, color: mfb.Rgb) void {
var start = Coords{
.x=max(0, min(a.x, b.x)),
.y=max(0, min(a.y, b.y)),
.x = max(0, min(a.x, b.x)),
.y = max(0, min(a.y, b.y)),
};
var end = Coords{
.x=min(@intCast(i32, self.width), max(a.x, b.x)),
.y=min(@intCast(i32, self.height), max(a.y, b.y)),
.x = min(@intCast(i32, self.width), max(a.x, b.x)),
.y = min(@intCast(i32, self.height), max(a.y, b.y)),
};
var y = start.y;
while (y < end.y) {
var x = start.x;
while (x < end.x) {
self.setPixel(.{.x=x, .y=y}, color);
self.setPixel(.{ .x = x, .y = y }, color);
x += 1;
}
y += 1;
@ -91,17 +98,34 @@ const Buffer = struct {
}
};
fn handleActive(win: mfb.Window(State), isActive: bool) void {
// win.getUserData().?.*.active = isActive;
var data = win.getUserData();
std.log.info("got data", .{});
if (data) |value| {
// value.*.active = isActive;
_ = value;
}
if (isActive) {
std.log.info("activated!!!!!!!!!!!!", .{});
} else {
std.log.info("deactivated", .{});
}
}
pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = &gp_allocator.allocator;
const alloc = gp_allocator.allocator();
var state = try State.init(3);
var state = try State.init(alloc, 3);
defer state.deinit();
var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{.resizable=true}) catch unreachable;
var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{ .resizable = true, .alwaysOnTop = true }) catch unreachable;
mfb.setTargetFPS(7);
win.setUserData(&state);
win.setActiveCallback(handleActive);
var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit();
@ -109,6 +133,7 @@ pub fn main() !void {
if (aliasedState) |value| {
std.log.info("Period: {d}", .{value.*.period});
}
state.render(buf);
while (win.waitSync()) {
state.render(buf);