Make the example more interesting
This commit is contained in:
parent
3778320a3e
commit
efba4456e7
109
src/main.zig
109
src/main.zig
@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
|
||||
const mfb = @import("minifb");
|
||||
const std = @import("std");
|
||||
const min = std.math.min;
|
||||
const max = std.math.max;
|
||||
|
||||
const Width = 800;
|
||||
const Height = 600;
|
||||
@ -9,38 +10,102 @@ fn grey(value: u8) u32 {
|
||||
return mfb.rgb(value, value, value);
|
||||
}
|
||||
|
||||
const Coords = struct {
|
||||
x: i32,
|
||||
y: i32,
|
||||
};
|
||||
|
||||
const State = struct {
|
||||
bgValue: u8,
|
||||
increasing: bool,
|
||||
};
|
||||
|
||||
fn render(buf: Buffer, state: State) void {
|
||||
buf.drawRectangle(.{.x=0,.y=0}, .{.x=Width, .y=Height}, grey(state.bgValue));
|
||||
buf.drawRectangle(.{.x=Width/3, .y=Height/3}, .{.x=2*Width/3, .y=2*Height/3}, mfb.rgb(255,0,0));
|
||||
}
|
||||
|
||||
const Buffer = struct {
|
||||
alloc: *std.mem.Allocator,
|
||||
slice: []u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
|
||||
pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) anyerror!Buffer {
|
||||
const slice = try alloc.alloc(u32, width*height);
|
||||
return Buffer {
|
||||
.alloc=alloc,
|
||||
.slice=slice,
|
||||
.width=width,
|
||||
.height=height,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Buffer) void {
|
||||
self.alloc.free(self.slice);
|
||||
}
|
||||
|
||||
pub fn setPixel(self: Buffer, coords: Coords, color: u32) void {
|
||||
const x = @intCast(u32, coords.x);
|
||||
const y = @intCast(u32, coords.y);
|
||||
self.slice[y*self.width + x] = color;
|
||||
}
|
||||
|
||||
pub fn drawRectangle(self: Buffer, a: Coords, b: Coords, color: u32) void {
|
||||
var start = Coords{
|
||||
.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)),
|
||||
};
|
||||
|
||||
var y = start.y;
|
||||
while (y < end.y) {
|
||||
var x = start.x;
|
||||
while (x < end.x) {
|
||||
self.setPixel(.{.x=x, .y=y}, color);
|
||||
x += 1;
|
||||
}
|
||||
y += 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const alloc = &gp_allocator.allocator;
|
||||
|
||||
std.log.info("All your codebase are belong to us.", .{});
|
||||
var win = mfb.Window.open("Hello minifb-zig", Width, Height) catch unreachable;
|
||||
mfb.setTargetFPS(60);
|
||||
|
||||
var color: u32 = grey(0);
|
||||
const deltaColor = grey(1);
|
||||
var increasing = true;
|
||||
var rawBuffer: [Width*Height]u32 = undefined;
|
||||
const buffer = rawBuffer[0..rawBuffer.len];
|
||||
std.mem.set(u32, buffer, color);
|
||||
var state = State {
|
||||
.increasing=true,
|
||||
.bgValue=0,
|
||||
};
|
||||
var buf = try Buffer.init(alloc, Width, Height);
|
||||
defer buf.deinit();
|
||||
|
||||
render(buf, state);
|
||||
while (win.waitSync()) {
|
||||
if (increasing) {
|
||||
if (color < grey(255)) {
|
||||
color += deltaColor;
|
||||
if (state.increasing) {
|
||||
if (state.bgValue < 255) {
|
||||
state.bgValue += 1;
|
||||
} else {
|
||||
increasing = false;
|
||||
state.increasing = false;
|
||||
}
|
||||
} else {
|
||||
if (color > grey(0)) {
|
||||
color -= deltaColor;
|
||||
if (state.bgValue > 0) {
|
||||
state.bgValue -= 1;
|
||||
} else {
|
||||
increasing = true;
|
||||
state.increasing = true;
|
||||
}
|
||||
}
|
||||
std.mem.set(u32, buffer, color);
|
||||
var i: u32 = 0;
|
||||
while (i < Width) {
|
||||
buffer[i] = mfb.rgb(255,0,0);
|
||||
i += 1;
|
||||
}
|
||||
_ = win.update(buffer) catch unreachable;
|
||||
|
||||
render(buf, state);
|
||||
// TODO Handle mfb state
|
||||
_ = win.update(buf.slice) catch unreachable;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user